0

我是新手,我正在尝试在我的一页滚动网站中进行平滑滚动。我从这里找到了这段代码

$(function () {
    $('a[href*=#]:not([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) {
                $('html,body').animate({
                    scrollTop: target.offset().top
                }, 1000);
                return false;
            }
        }
    });
}); 

它完美地工作,问题是我不明白它!有人可以为我解释一下吗?

PS - 我不知道这对于stackoverflow是否是一个有效的问题,如果它不只是告诉我,我会删除这篇文章。

谢谢

4

1 回答 1

2
$(function() {
//^^^^ by this code start on dcocument load
$('a[href*=#]:not([href=#])').click(function() {
 //^^^^ set selector  (anchor tag) 
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) {
  // ^^^^check target length is exist in target hash coming if this exist then go in if othervise not
    $('html,body').animate({
     //^^^^ here html and body animate 
      scrollTop: target.offset().top
     //^^^^ get target scroll top position and move html,body scroll to this position
    }, 1000);
    return false;
  }
}
});
});
于 2013-11-07T11:13:16.670 回答