我正在制作一个使用 html 元素作为障碍的拼图平台游戏。有些级别我想从文档的底部开始。所以我尝试使用
$(document).ready(function() {
$(document).scrollTop($(document).height() - $(window).height());
}
和
$(document).ready(function() {
window.scrollTo(0, document.body.scrollHeight);
}
它们在 Firefox 中都工作得很好,但在 Chrome 中都不工作。我只能在为滚动设置动画时让它工作,这对我想做的事情不起作用。
编辑:我没有使用 jQuery.toScroll() 我尝试了 vanilla toScroll 和 jQuery.scrollTop()
最佳答案
浏览器以不同方式将滚动条附加到页面。使用 $('html,body').scrollTop()。您还应该使用 $(window).innerHeight()。像这样...
$('html,body').scrollTop($(document).height() - $(window).innerHeight());
//Or to smooth scroll it
$('html,body').animate({
scrollTop: $(document).height() - $(window).innerHeight()
}, 1000);
注意:如果文档比窗口短,$(document).height() 将返回与 $(window).innerHeight() 相同的值。
关于javascript - scrollTo() 和 jquery scrollTop() 在 Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55554745/