And*_*rew 165 javascript
我需要检测用户是否滚动到页面底部.如果它们位于页面底部,当我向底部添加新内容时,我会自动将它们滚动到新的底部.如果他们不在底部,他们正在阅读页面上较高的先前内容,所以我不想自动滚动它们,因为他们想要留在原地.
如何检测用户是否滚动到页面底部或是否在页面上滚动更高?
mVC*_*Chr 231
window.onscroll = function(ev) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
// you're at the bottom of the page
}
};
Run Code Online (Sandbox Code Playgroud)
Dek*_*kel 94
window.onscroll = function(ev) {
if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight) {
alert("you're at the bottom of the page");
}
};
Run Code Online (Sandbox Code Playgroud)
当前接受的答案的问题是window.scrollY在IE中不可用.
下面是一个报价MDN关于scrollY:
对于跨浏览器兼容性,请使用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");
}
};Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
根据@Raphaël的评论,由于偏移很小,因此mac存在问题.
以下更新条件有效:
(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2
Run Code Online (Sandbox Code Playgroud)
我没有机会进一步测试,如果有人可以对这个具体问题发表意见,那就太棒了.
Rya*_*ell 45
接受的答案对我不起作用.这样做:
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");
}
};
Run Code Online (Sandbox Code Playgroud)
如果您希望支持旧浏览器(IE9),请使用window.pageYOffset稍微更好支持的别名.
pas*_*sti 21
我正在寻找答案,但还没有找到答案.这是一个纯粹的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);
Run Code Online (Sandbox Code Playgroud)
Ife*_*adi 10
这有效
window.onscroll = function() {
var scrollHeight, totalHeight;
scrollHeight = document.body.scrollHeight;
totalHeight = window.scrollY + window.innerHeight;
if(totalHeight >= scrollHeight)
{
console.log("at the bottom");
}
}
Run Code Online (Sandbox Code Playgroud)
如果您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!');
}
}
Run Code Online (Sandbox Code Playgroud)
window.onscroll = function(ev) {
if ((window.innerHeight + Math.ceil(window.pageYOffset)) >= document.body.offsetHeight) {
alert("you're at the bottom of the page");
}
};
Run Code Online (Sandbox Code Playgroud)
这个答案将修复边缘情况,这是因为pageYOffsetis doublewhileinnerHeight和 offsetHeightare long,所以当浏览器给你信息时,你可能是一个像素短。例如:在页面底部我们有
真的 window.innerHeight = 10.2
真的 window.pageYOffset = 5.4
真的 document.body.offsetHeight = 15.6
我们的计算结果变为:10 + 5.4 >= 16 这是错误的
为了解决这个问题,我们可以Math.ceil对pageYOffset值进行处理。
希望有帮助。
小智 6
如果您对其他方法都不感兴趣,请尝试此方法。
window.onscroll = function() {
const difference = document.documentElement.scrollHeight - window.innerHeight;
const scrollposition = document.documentElement.scrollTop;
if (difference - scrollposition <= 2) {
alert("Bottom of Page!");
}
}Run Code Online (Sandbox Code Playgroud)
我刚刚开始研究这个,这里的答案对我有帮助,所以谢谢你。我进行了一些扩展,以便代码在回到 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>
Run Code Online (Sandbox Code Playgroud)