[英]Smooth scrolling not working jQuery
使用https://css-tricks.com/snippets/jquery/smooth-scrolling/中的代码,对于我这类人来说,无法弄清楚为什么它不起作用。
脚本:
$('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;
}
}
});
有什么想法吗? 该站点跳到div容器,但没有动画。
看到这个答案: https : //stackoverflow.com/a/2234749/606104
我在Safari和Chrome(Mac)中遇到了这个问题,发现.scrollTop可以在$(“ body”)上工作,但不能在$(“ html,body”)上工作
要使其在Safari中起作用,请将您的脚本更改为:
$('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) {
var bodyElement = $("html,body");
if ($.browser.safari) {
bodyElement = $("body")
}
bodyElement.animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.