如何使用jQuery实现这一功能?
jQuery并不是必需的。我从谷歌搜索到的大多数顶部结果都给出了这个答案:
window.scrollTo(0, document.body.scrollHeight);
如果您有嵌套元素,文档可能无法滚动。在这种情况下,您需要针对可滚动的元素并使用其滚动高度。
nestedElement.scrollTo(0, nestedElement.scrollHeight);
这里有一些额外的资源供您参考:
element.scrollTop = element.scrollHeight
。 - EsamoobjectSelector.scrollTo({ top: objectSelector.scrollHeight })
。理解一下,objectSelector
是通过 document.getElementById
返回的元素。附注:在 scrollTo
方法选项中添加 behavior: 'smooth'
可以设置一个预定义的滚动动画。 - KBeDev滚动整个页面到底部:
const scrollingElement = (document.scrollingElement || document.body);
scrollingElement.scrollTop = scrollingElement.scrollHeight;
您可以在此处查看演示。
要将特定元素滚动到底部:
const scrollToBottom = (id) => {
const element = document.getElementById(id);
element.scrollTop = element.scrollHeight;
}
这是演示。
它的工作原理如下:
参考:scrollTop、scrollHeight、clientHeight
更新: Chrome(61+)和Firefox的最新版本不支持滚动body,请参见:https://dev.opera.com/articles/fixing-the-scrolltop-bug/
纯JS实现:
element.scrollIntoView(false);
https://developer.mozilla.org/zh-CN/docs/Web/API/element.scrollIntoView
element.scrollIntoView({behavior: "smooth"});
。 - blalond您可以使用这个功能以动画格式向下滑动页面。
$('html,body').animate({scrollTop: document.body.scrollHeight},"fast");
一行代码以平滑的方式滚动到底部
window.scrollTo({ left: 0, top: document.body.scrollHeight, behavior: "smooth" });
要向上滚动,只需将top
设置为0
下面应该是跨浏览器的解决方案。它已在Chrome,Firefox,Safari和IE11上进行了测试。
window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight);
在Firefox浏览器中,至少对于版本37.0.2,window.scrollTo(0,document.body.scrollHeight);
无法正常工作。
一种有趣的仅使用CSS的替代方案:
display: flex;
flex-direction: column-reverse;
/* ...probably usually along with: */
overflow-y: scroll; /* or hidden or auto */
height: 100px; /* or whatever */
虽然它不是百分之百可靠的, 但我在几个情况下都发现它很有用。
文档: flex
, flex-direction
, overflow-y
var i=0, foo='Lorem Ipsum & foo in bar or blah ! on and'.split(' ');
setInterval(function(){demo.innerHTML+=foo[i++%foo.length]+' '},200)
#demo{ display:flex;
flex-direction:column-reverse;
overflow-y:scroll;
width:150px;
height:150px;
border:3px solid black; }
body{ font-family:arial,sans-serif;
font-size:15px; }
Autoscrolling demo:🐾
<div id='demo'></div>
有时网页会在滚动到底部时继续延伸(例如在社交网络中),为了向下滚动到页面的最底部,我使用以下脚本:
var scrollInterval = setInterval(function() {
document.documentElement.scrollTop = document.documentElement.scrollHeight;
}, 50);
如果您在浏览器的JavaScript控制台中,停止滚动可能会很有用,因此请添加:
var stopScroll = function() { clearInterval(scrollInterval); };
然后使用stopScroll();
。
如果您需要滚动到特定的元素,请使用:
var element = document.querySelector(".element-selector");
element.scrollIntoView();
或者是用于自动滚动到特定元素(或停止页面滚动间隔)的通用脚本:
var notChangedStepsCount = 0;
var scrollInterval = setInterval(function() {
var element = document.querySelector(".element-selector");
if (element) {
// element found
clearInterval(scrollInterval);
element.scrollIntoView();
} else if((document.documentElement.scrollTop + window.innerHeight) != document.documentElement.scrollHeight) {
// no element -> scrolling
notChangedStepsCount = 0;
document.documentElement.scrollTop = document.documentElement.scrollHeight;
} else if (notChangedStepsCount > 20) {
// no more space to scroll
clearInterval(scrollInterval);
} else {
// waiting for possible extension (autoload) of the page
notChangedStepsCount++;
}
}, 50);
您可以在需要调用它的任何地方使用此函数:
function scroll_to(div){
if (div.scrollTop < div.scrollHeight - div.clientHeight)
div.scrollTop += 10; // move down
}
document.getElementById('copyright').scrollTop += 10
不起作用...仍然是零... - Kyle Baker你也可以使用动画来实现这个效果,非常简单
$('html, body').animate({
scrollTop: $('footer').offset().top
//scrollTop: $('#your-id').offset().top
//scrollTop: $('.your-class').offset().top
}, 'slow');
希望有所帮助,谢谢