使用jQuery实现浏览器滚动条返回顶部功能

<!DOCTYPE html>
<html ng-app="app">
<head>

  <meta charset="utf-8"/>
  <title ng-bind="title">使用jQuery实现返回顶部功能</title>
  <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
  <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>

  <style>
    body,html{height:100%}
    footer,header{flex:1}
    header.navbar-top{text-align: center;height:30px!important;min-height:40px;background:#205081;color:#fff;font-size:14px!important;line-height:40px;z-index:9999}
    header.navbar-top a{color: #fff;}
    .example{height: 1800px;width: 500px;background-color: pink;margin:auto;margin-top: 40px;}

    .margin-lg{margin:250px 0}
    .fixed-tools{position:fixed;right:40px;bottom:100px;z-index:1000;text-align:center}
    .fixed-tools i{margin:0!important;font-size:22px;line-height:1}
    .fixed-tools li.item{margin-bottom:10px!important}
    .fixed-tools li a{padding:13px 12px!important;width:46px;height:46px;border-radius:50%!important;background:#f1f1f1}
    .fixed-tools li a:hover{background-color:#7cc923!important;color:#fff!important}
    .fixed-tools li a:hover i{color:#fff!important}
    .hover-fixed{position:absolute;top:0;right:100%;display:none;width:150px;height:100%;background-color:#7cc923;color:#fff}
    .hover-gototop{position:absolute;right:0;bottom:-46px;display:none;background:#f1f1f1}

  </style>
  <script>
    $(function(){
        // 为窗口的scroll事件绑定处理函数
      $(window).scroll(function() {
        // 获取窗口的滚动条的垂直位置
        var s = $(window).scrollTop();
        // 当窗口的滚动条的垂直位置大于页面的最小高度时,让返回顶部元素渐现,否则渐隐
        if (s > 300) {
          $(".hover-gototop").fadeIn(100);
        } else {
          $(".hover-gototop").fadeOut(200);
        };
      });

      // 点击返回顶部,也可返回指定位置
      $(".hover-gototop").on('click', function() {
        $('html,body').animate({
          scrollTop: 0
        }, 700);
      });
    })
  </script>
</head>
<body>
<header class="navbar-top navbar-fixed-top">
  <a href="#">使用jQuery实现返回顶部功能</a>
</header>
<section class="example">
内容区
</section>
<ul class="nav fixed-tools">
  <li class="item">
    <a class="text-muted" title="联系方式">Tel</a>
  </li>
  <li>
    <div id="backTop">
      <a class="hover-gototop" title="返回顶部">↑</a>
    </div>
  </li>
</ul>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

smart_dream

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值