我使用下面的代码在一段时间后自动滚动到一个 id。
setTimeout( function() {
$('html, body').animate({
'scrollTop': $('#about').offset().top
}, 2000);
}, 3000);
但是,我见过很多不同的方法来完成这个简单的任务。
问最好的方法是什么可能太客观了,但我需要知道我的代码是否做错了。似乎在当前版本的 Chrome、FF 和 IE 中运行良好
提前致谢!
最佳答案
对于动画,您应该使用延迟,并且不需要在单个字符串对象键周围使用引号:
$('html, body').delay(3000).animate({
scrollTop: $('#about').offset().top
}, 2000);
The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.
回答你的问题,不,你没有做错什么。
关于javascript - 经过一段时间间隔后滚动到 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17331802/