锚点定位及返回顶部

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <script src="jquery.1.9.1.min.js"></script>
</head>
<body>
<div class="header">
  <div class="header-width">
    <ul class="nav fr" >
      <li class="list list2">
        <a href="javascript:void(0)">头部</a>
      </li>
      <li class="list list2">
        <a href="javascript:void(0)" to="main-one">锚点1</a>
      </li>

      <li class="list list3">
        <a href="javascript:void(0)" to="main-two">锚点2</a>
      </li>
      <li class="list list4"">
        <a href="javascript:void(0)" to="main-three">锚点3</a>
      </li>
    </ul>
  </div>
  <div style="height: 800px" id="main-one">main-one</div>
  <div style="height: 800px" id="main-two">main-two</div>
  <div style="height: 1200px" id="main-three">main-three</div>
  <div class="gototop" style="position: fixed;right: 100px;bottom: 100px">返回顶部</div>
</div>
<script>
  $(function () {
    // 锚点定位
    $('.header .list a').click(function () {
      var id = $(this).attr('to');
      var top = $("#" + id).offset().top ;
      $("html, body").animate({
        scrollTop: top
      }, { duration: 500, easing: "swing" });
      return false;
    });

    // 滚动事件
    $(window).scroll(function(){
      var windowH= $(window).height();
      var scrollTop = $(window).scrollTop();
      // 导航栏
      if(scrollTop>300){
        $('.gototop').css("display","block");
      }else{$('.gototop').css("display","none");}

    });
    // 点击返回顶部
    $('.gototop').click(function () {
      $("html, body").animate({scrollTop: 0}, 500);
    });
  })
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值