jquery实现页面回到顶部TOP(回滚到顶部)

JQuery 66 0 2024-04-05

jquery实现页面回到顶部TOP(回滚到顶部)

JQ实现在网页页面较长时回到顶部

HTML代码:

<div id="back-to-top">TOP</div>

//样式
        #back-to-top {
            position: fixed;
            bottom: 20px;
            right: 20px;
            display: none;
            z-index: 99;
            background-color: #EEE;
            padding: 5px;
            border: #DDD 1px solid;
            border-radius: 4px;
            cursor: pointer;
        }

JS代码:

$(document).ready(function(){
  // 监听滚动事件
  $(window).scroll(function(){
    // 当向下滚动超过100px时显示按钮
    if ($(this).scrollTop() > 100) {
      $('#back-to-top').fadeIn();
    } else {
      $('#back-to-top').fadeOut();
    }
  });
  
  // 当点击按钮时回到顶部
  $('#back-to-top').click(function(){
    $('body,html').animate({scrollTop:0},800);
  });
});

上一篇:修改jquery默认$符号

下一篇:没有了

讨论数量:0

请先登录再发表讨论。 2024-04-30

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链