【发布时间】:2015-06-26 01:51:06
【问题描述】:
我想在我的页面上有平滑的滚动效果。并且 iv 找到了这段代码
jQuery(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;
}
}
});
});
这没问题,但不适用于我的“转到顶部”链接。所以我用教程检查其他页面并这样做:
jQuery(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;
}
}
});
$('a.go-top').click(function() {
$('html, body').animate({scrollTop:0}, 'slow');
return false;
});
});
现在一切正常。但我对jQuery一无所知,你能告诉我这是正确的代码吗,或者如果可以,我应该在这里改变什么?谢谢!
【问题讨论】:
-
我觉得这个问题应该发到Code Review
-
@ArturFilipiak 要成为代码审查的主题,问题必须由代码的作者或维护者提出。您不能只要求对您发现的随机代码进行代码审查。
-
@ArturFilipiak 一定是作者本人。 Artur,看看this。页面底部附近有一个小清单 - 如果您以“是”回答所有问题,它应该在 CodeReview 上。其中一个问题要求作者提出
-
或维护者。哎呀。