具有顶部功能的jQuery问题

时间:2019-07-19 12:54:42

标签: javascript jquery

我已经使用jQuery创建了Back to Top函数。

滚动到顶部有效,但我似乎无法弄清楚如何隐藏它,仅在说scrollTop() > 300时出现。我创建了一个函数来解决这个问题,但不幸的是没有运气。

这里是jsfiddle https://jsfiddle.net/pvan_ren1/st3mdp6a/10/

的链接
//This is the function that's supposed to take care of the hide and reveal of toTop button.
$(window).scroll(function() {
    if ($(window).scrollTop() > 300) {
        btn.addClass('show');
    } else {
        btn.removeClass('show');
    }
});

3 个答案:

答案 0 :(得分:2)

默认情况下,您应该编辑CSS以隐藏“返回首页”按钮,然后在添加show类时显示它。

#toTop {
    display: none;
    background-color: #FF9800;
    width: 50px;
    height: 50px;
    text-align: center;
    border-radius: 4px;
    margin: 30px;
    position: fixed;
    bottom: 30px;
    right: 30px;
    transition: background-color .3s;
    z-index: 1000;
}
#toTop.show {
    display: inline-block;
}

jQuery(document).ready(function() {
    var btn = $('#toTop');

    $(window).scroll(function() {
        if ($(window).scrollTop() > 300) {
            btn.addClass('show');
        } else {
            btn.removeClass('show');
        }
    });

    btn.on('click', function(e) {
        e.preventDefault();
        $('html, body').animate({
          scrollTop: 0
        }, 700);
    });
});
.sectionA {
  width: 100%;
  height: 600px;
  background-color: pink;
}

.sectionB {
  width: 100%;
  height: 600px;
  background-color: green;
}

.sectionC {
  width: 100%;
  height: 600px;
  background-color: purple;
}

.sectionD {
  width: 100%;
  height: 600px;
  background-color: orange;
}

#toTop {
    display: none;
    background-color: #FF9800;
    width: 50px;
    height: 50px;
    text-align: center;
    border-radius: 4px;
    margin: 30px;
    position: fixed;
    bottom: 30px;
    right: 30px;
    transition: background-color .3s;
    z-index: 1000;
}
#toTop.show {
    display: inline-block;
}
#toTop:hover {
    cursor: pointer;
    background-color: #333;
}
#toTop:active {
    background-color: #555;
}
#toTop::after {
    content: "\f077";
    font-family: FontAwesome;
    font-weight: normal;
    font-style: normal;
    font-size: 2em;
    line-height: 50px;
    color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<p style="position:fixed">Scroll Down and use the toTop button</p>
<br>
<p style="position:fixed">
At the top, the "Back to Top" button does not show.
<b>It works!</b>
</p>
<section class="sectionA">

</section>
<section class="sectionB">

</section>
<section class="sectionC">

</section>
<section class="sectionD">

</section>
</body>
<a id="toTop"></a>

答案 1 :(得分:1)

您可以使用.hide();.show();隐藏和显示您的scrollTop。即:

jQuery(document).ready(function() {
    var btn = $('#toTop');
    //hiding button
     btn.hide();
    $(window).scroll(function() {
        if ($(window).scrollTop() > 300) {
            //show button
            btn.show();
        } else {
        //hide button
            btn.hide();
        }
    });

    btn.on('click', function(e) {
        e.preventDefault();
        $('html, body').animate({scrollTop:0},
        700);
    });
});
.sectionA {
  width: 100%;
  height: 600px;
  background-color: pink;
}

.sectionB {
  width: 100%;
  height: 600px;
  background-color: green;
}

.sectionC {
  width: 100%;
  height: 600px;
  background-color: purple;
}

.sectionD {
  width: 100%;
  height: 600px;
  background-color: orange;
}

#toTop {
    display: inline-block;
    background-color: #FF9800;
    width: 50px;
    height: 50px;
    text-align: center;
    border-radius: 4px;
    margin: 30px;
    position: fixed;
    bottom: 30px;
    right: 30px;
    transition: background-color .3s;
    z-index: 1000;
}
#toTop:hover {
    cursor: pointer;
    background-color: #333;
}
#toTop:active {
    background-color: #555;
}
#toTop::after {
    content: "\f077";
    font-family: FontAwesome;
    font-weight: normal;
    font-style: normal;
    font-size: 2em;
    line-height: 50px;
    color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
  <head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
  </head>
    <body>
    <p style="position:fixed">Scroll Down and use the toTop button</p>
    <br>
    <p style="position:fixed">
    Notice how at the top it still shows, well that's bullshit. Help me fix it.
    </p>
    <section class="sectionA">
    
    </section>
    <section class="sectionB">
      
    </section>
    <section class="sectionC">
      
    </section>
    <section class="sectionD">
      
    </section>
    </body>
    <a id="toTop"></a>
</html>

答案 2 :(得分:0)

在进行任何操作之前,请将按钮的样式设置为display: none,然后下面的代码应为您完成此操作:

 $(window).scroll(function() {
        if ($(window).scrollTop() > 300) {
           $('#toTop').css('display','inline-block');
        } else {
             $('#toTop').css('display','none');
        }
    });