我需要检测用户是否滚动到页面底部。如果它们位于页面底部,当我向底部添加新内容时,我会自动将它们滚动到新的底部。如果他们不在底部,他们正在阅读页面上较高的先前内容,所以我不想自动滚动它们,因为他们想要留在原地。
如何检测用户是否滚动到页面底部或是否在页面上滚动更高?
答案 0 :(得分:204)
window.onscroll = function(ev) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
// you're at the bottom of the page
}
};
答案 1 :(得分:82)
window.onscroll = function(ev) {
if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight) {
alert("you're at the bottom of the page");
}
};
当前接受的答案存在的问题是IE中没有window.scrollY
。
以下是mdn关于向上滚动的引用:
对于跨浏览器兼容性,请使用window.pageYOffset而不是window.scrollY。
一个工作片段:
window.onscroll = function(ev) {
if ((window.innerHeight + window.pageYOffset ) >= document.body.offsetHeight) {
alert("you're at the bottom of the page");
}
};
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
根据@Raphaël的评论,由于偏移很小,因此mac存在问题 以下更新的条件有效:
(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2
我没有机会进一步测试,如果有人可以评论这个特定的问题,那就太棒了。
答案 2 :(得分:39)
接受的答案对我不起作用。这样做了:
window.onscroll = function(ev) {
if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
// you're at the bottom of the page
console.log("Bottom of page");
}
};
如果您希望支持较旧的浏览器(IE9),请使用稍微更好支持的别名window.pageYOffset
。
答案 3 :(得分:19)
我正在寻找答案,但却找不到答案。这是一个纯粹的JavaScript解决方案,可以在回答这个问题时使用最新的Firefox,IE和Chrome:
// document.body.scrollTop alone should do the job but that actually works only in case of Chrome.
// With IE and Firefox it also works sometimes (seemingly with very simple pages where you have
// only a <pre> or something like that) but I don't know when. This hack seems to work always.
var scrollTop = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
// Grodriguez's fix for scrollHeight:
// accounting for cases where html/body are set to height:100%
var scrollHeight = (document.documentElement && document.documentElement.scrollHeight) || document.body.scrollHeight;
// >= is needed because if the horizontal scrollbar is visible then window.innerHeight includes
// it and in that case the left side of the equation is somewhat greater.
var scrolledToBottom = (scrollTop + window.innerHeight) >= scrollHeight;
// As a bonus: how to scroll to the bottom programmatically by keeping the horizontal scrollpos:
// Since window.innerHeight includes the height of the horizontal scrollbar when it is visible
// the correct vertical scrollTop would be
// scrollHeight-window.innerHeight+sizeof(horizontal_scrollbar)
// Since we don't know the visibility/size of the horizontal scrollbar
// we scroll to scrollHeight that exceeds the value of the
// desired scrollTop but it seems to scroll to the bottom with all browsers
// without problems even when the horizontal scrollbar is visible.
var scrollLeft = (document.documentElement && document.documentElement.scrollLeft) || document.body.scrollLeft;
window.scrollTo(scrollLeft, scrollHeight);
答案 4 :(得分:8)
这有效
window.onscroll = function() {
var scrollHeight, totalHeight;
scrollHeight = document.body.scrollHeight;
totalHeight = window.scrollY + window.innerHeight;
if(totalHeight >= scrollHeight)
{
console.log("at the bottom");
}
}
答案 5 :(得分:4)
如果您在某个容器height: 100%
上设置<div id="wrapper">
,则以下代码有效(在Chrome中测试):
var wrapper = document.getElementById('wrapper');
wrapper.onscroll = function (evt) {
if (wrapper.scrollTop + window.innerHeight >= wrapper.scrollHeight) {
console.log('reached bottom!');
}
}
答案 6 :(得分:4)
我刚开始看这个,这里的答案对我有所帮助,所以谢谢你。 我已经扩展了一点,所以代码一直安全回到IE7:
希望这证明对某人有用。
<!DOCTYPE html>
<html>
<head>
<style>
div {
height: 100px;
border-bottom: 1px solid #ddd;
}
div:nth-child(even) {
background: #CCC
}
div:nth-child(odd) {
background: #FFF
}
</style>
</head>
<body>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</body>
<script type="text/javascript">
console.log("Doc Height = " + document.body.offsetHeight);
console.log("win Height = " + document.documentElement.clientHeight);
window.onscroll = function (ev) {
var docHeight = document.body.offsetHeight;
docHeight = docHeight == undefined ? window.document.documentElement.scrollHeight : docHeight;
var winheight = window.innerHeight;
winheight = winheight == undefined ? document.documentElement.clientHeight : winheight;
var scrollpoint = window.scrollY;
scrollpoint = scrollpoint == undefined ? window.document.documentElement.scrollTop : scrollpoint;
if ((scrollpoint + winheight) >= docHeight) {
alert("you're at the bottom");
}
};
</script>
</html>
答案 7 :(得分:3)
window.onscroll = function(ev) {
if ((window.innerHeight + Math.ceil(window.pageYOffset)) >= document.body.offsetHeight) {
alert("you're at the bottom of the page");
}
};
此答案将修复边缘情况,这是因为pageYOffset
为double
而innerHeight
和offsetHeight
为long
,因此当浏览器为您提供信息,你可能是一个像素短。
例如:在页面底部我们有
true window.innerHeight = 10.2
true window.pageYOffset = 5.4
true document.body.offsetHeight = 15.6
我们的计算结果如下: 10 + 5.4&gt; = 16, false
要解决此问题,我们可以对Math.ceil
值进行pageYOffset
。
希望有所帮助。
答案 8 :(得分:2)
令人惊讶的是,这些解决方案都不适合我。我认为这是因为我的css
搞砸了,body
在使用height: 100%
时并没有包含所有内容(不知道为什么然而)。然而,在寻找解决方案时,我已经提出了一些好的东西......基本相同,但也许它值得一看 - 我是编程的新手,所以很抱歉,如果它&#39; s做得同样慢一点,支持不太好或类似......
window.onscroll = function(evt) {
var check = (Element.getBoundingClientRect().bottom - window.innerHeight <= 0) ? true : false;
if (check) { console.log("You're at the bottom!"); }
};
答案 9 :(得分:2)
您可以查看jquery的无限滚动:
http://www.infinite-scroll.com/infinite-scroll-jquery-plugin/
这听起来像是你要求的,假设你愿意使用jquery库而不是希望使用严格的纯JS方法。
答案 10 :(得分:2)
如果您喜欢jquery
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() >= $(document).height()) {
// doSomethingHere();
}
});
答案 11 :(得分:2)
新的解决方案。
一个问题源于缺乏标准的主滚动元素。最近实现的 document.scrollingElement 可用于尝试克服此问题。以下是具有回退功能的跨浏览器解决方案:
function atEnd() {
var c = [document.scrollingElement.scrollHeight, document.body.scrollHeight, document.body.offsetHeight].sort(function(a,b){return b-a}) // select longest candidate for scrollable length
return (window.innerHeight + window.scrollY + 2 >= c[0]) // compare with scroll position + some give
}
function scrolling() {
if (atEnd())
//do something
}
window.addEventListener('scroll', scrolling, {passive: true});
答案 12 :(得分:1)
$(document).ready(function(){
$('.NameOfYourDiv').on('scroll',chk_scroll);
});
function chk_scroll(e)
{
var elem = $(e.currentTarget);
if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight())
{
alert("scrolled to the bottom");
}
}
答案 13 :(得分:1)
使用defaultView
和documentElement
并嵌入功能代码段:
const { defaultView } = document;
const { documentElement } = document;
const handler = evt => requestAnimationFrame(() => {
const hitBottom = (() => (defaultView.innerHeight + defaultView.pageYOffset) >= documentElement.offsetHeight)();
hitBottom
? console.log('yep')
: console.log('nope')
});
document.addEventListener('scroll', handler);
<pre style="height:110vh;background-color:fuchsia">scroll down</pre>
答案 14 :(得分:1)
const handleScroll = () => {
if (Math.round(window.scrollY + window.innerHeight) >= Math.round(document.body.scrollHeight)) {
onScroll();
}
};
此代码也适用于Firefox和IE。
答案 15 :(得分:1)
您可以检查窗口的高度和滚动顶部的总和是否大于主体的总和
if (window.innerHeight + window.scrollY >= document.body.scrollHeight) {}
答案 16 :(得分:0)
我不得不想出一种方法(使用Java)来系统地向下滚动,以寻找我不知道正确的XPath的组件(长话短说,所以请一起玩)。正如我刚才所说,我需要在寻找组件时向下滚动,并在找到该组件或到达页面底部时停止。
以下代码段控制向下滚动到页面底部:
JavascriptExecutor js = (JavascriptExecutor) driver;
boolean found = false;
long currOffset = 0;
long oldOffset = 0;
do
{
oldOffset = currOffset;
// LOOP to seek the component using several xpath regexes removed
js.executeScript("window.scrollBy(0, 100)");
currOffset = (Long)js.executeScript("var offset = window.window.pageYOffset; return offset;");
} while (!found && (currOffset != oldOffset));
顺便说一句,在执行此代码段之前,窗口已最大化。
答案 17 :(得分:0)
接受的答案对我不起作用。这样做了:
const element = document.createElement('div');
document.body.appendChild(element);
document.addEventListener('scroll', () => {
const viewportHeight = window.innerHeight;
const distance = element.getBoundingClientRect().top;
if (Math.floor(distance) <= viewportHeight) {
console.log('yep')
} else {
console.log('nope')
}
})
答案 18 :(得分:0)
我发现对我有用的两个解决方案:
window.addEventListener('scroll', function(e) {
if (
window.innerHeight + document.documentElement.scrollTop ===
document.documentElement.offsetHeight
) {
console.log('You are at the bottom')
}
})
另一个:
window.addEventListener('scroll', function(e) {
if (
window.innerHeight + window.pageYOffset ===
document.documentElement.offsetHeight
) {
console.log('You are at the bottom')
}
})
答案 19 :(得分:0)
如果您没有其他人的运气,请尝试此方法。
window.onscroll = function()
{
var difference = document.documentElement.scrollHeight - window.innerHeight;
var scrollposition = document.documentElement.scrollTop;
if (difference - scrollposition <= 2)
{
alert("Bottom of Page!");
}
}