首页 > 其他 > 详细

实现锚点的带动画效果---不是突然跳转到指定地方

时间:2019-11-26 16:43:42      阅读:122      评论:0      收藏:0      [点我收藏+]

html代码:

<div class="header">
  <img class="logo" src="./images/logo@2x.png" />
  <ul class="nav-list">
      <!-- href作锚点用,id是JS时用,必须是a便签的时候来进行锚链接-->
      <li class="sel"><a href="#home" id="nav-home">首页</a></li>
      <li><a href="#service" id="nav-ser">服务优势</a></li>
      <li><a href="#about" id="nav-about">关于我们</a></li>
      <li class="last"><a href="#contact" id="nav-con">联系我们</a></li>
  </ul>
</div>
<div class="banner"></div>
<div class="content" id="service">

</div>

JS代码:

$(function () {
  $("#nav-ser,#nav-about,#nav-con,#nav-home").click(function () {
    if (location.pathname.replace(/^\//, ‘‘) == this.pathname.replace(/^\//, ‘‘) && location.hostname == this.hostname) {
      var $target = $(this.hash);
      $target = $target.length && $target || $([name= + this.hash.slice(1) + ]);
      if ($target.length) {
        var targetOffset = $target.offset().top;
        $(html,body).animate({ scrollTop: targetOffset }, 800);
        return false;
      }
    }
  });
});

 

实现锚点的带动画效果---不是突然跳转到指定地方

原文:https://www.cnblogs.com/tanweiwei/p/11935737.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!