点击锚点平滑滚动效果

获取a的href的锚点连接target,通过
 
  
targetOffset=$(target).offset().top; 获取该锚点到浏览器顶部的距离,然后给body添加动画
 
  
$('html,body').animate({
        scrollTop: targetOffset
    },
    1000);
全部代码:
/*锚点动效*/
$('a[href*=#],area[href*=#]').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
            },
            1000);
         return false;
      }
   }
});

你可能感兴趣的:(jquery)