我是新手,我正在尝试在我的单页滚动网站中进行平滑滚动。 我从here
找到了这段代码$(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;
}
}
});
});
它完美无缺,问题是我不明白! 有人可以帮我解释一下吗?
P.S。 - 我不知道这是否是stackoverflow的有效问题,如果它不只是告诉我,我删除了这篇文章。
由于
答案 0 :(得分: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;
}
}
});
});