我想要平滑滚动到内部链接。在 Firefox 中工作正常,但在 Chrome 中我收到错误“body.scrollTop 在严格模式下已弃用”
我的代码是
$(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;
}
}
});
});
然而,问题是在我改变这个之后
$('html,body').animate({scrollTop: target.offset().top}, 1000);
到
$('html').animate({scrollTop: target.offset().top}, 1000);
链接根本不起作用,并且控制台中没有错误/警告。
最佳答案
保留'html,body'
以定位更多浏览器。
只要 Chrome 显示“已弃用”警告(不是错误),它仍然会响应 body.scrollTop
并且应该像您期望的那样滚动。
我认为造成这种困惑的原因是 Chrome 32 会警告已弃用的功能,但 Chrome 33 仅使用已弃用的功能且没有任何警告。
关于javascript - body.scrollTop 在严格模式下已弃用。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21886131/