我需要检测用户是否滚动到页面底部。如果它们在页面底部,当我在底部添加新内容时,我会自动将它们滚动到新底部。如果它们不在底部,则它们正在阅读页面上较高的先前内容,因此我不想自动滚动它们,因为它们想留在原处。
如何检测用户是否滚动到页面底部,或者他们是否在页面上滚动得更高?
window.onscroll = function(ev) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
// you're at the bottom of the page
}
};
看演示
更新了所有主要浏览器支持的代码(包括 IE10 和 IE11)
window.onscroll = function(ev) {
if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight) {
alert("you're at the bottom of the page");
}
};
当前接受的答案的问题是 window.scrollY
在 IE 中不可用。
以下是 mdn 关于 scrollY 的引述:
为了跨浏览器兼容性,请使用 window.pageYOffset 而不是 window.scrollY。
还有一个工作片段:
window.onscroll = function(ev) { if ((window.innerHeight + window.pageYOffset ) >= document.body.offsetHeight) { alert("你在页面底部"); } };
<
< br>
mac的注意事项
根据@Raphaël 的评论,由于偏移量小,mac 中存在问题。以下更新的条件有效:
(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2
我没有机会进一步测试它,如果有人可以评论这个特定问题,那就太好了。
(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2
(window.innerHeight + Math.ceil(window.pageYOffset + 1)) >= document.body.offsetHeight
。
接受的答案对我不起作用。这做到了:
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
,它的支持稍好一些。
我正在寻找答案,但没有找到确切的答案。这是一个纯 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);
((document.documentElement && document.documentElement.scrollHeight) || document.body.scrollHeight)
而不仅仅是 document.body.scrollHeight
来解决 html/body 设置为 height:100% 的情况
document.body.scrollTop
进行无意义的斗争。谢谢。
这有效
window.onscroll = function() {
// @var int totalPageHeight
var totalPageHeight = document.body.scrollHeight;
// @var int scrollPoint
var scrollPoint = window.scrollY + window.innerHeight;
// check if we hit the bottom of the page
if(scrollPoint >= totalPageHeight)
{
console.log("at the bottom");
}
}
如果您希望支持旧版浏览器 (IE9),请将 window.scrollY 替换为 window.pageYOffset
如果您在某个容器 <div id="wrapper">
上设置 height: 100%
,则以下代码有效(在 Chrome 中测试):
var wrapper = document.getElementById('wrapper');
wrapper.onscroll = function (evt) {
if (wrapper.scrollTop + window.innerHeight >= wrapper.scrollHeight) {
console.log('reached bottom!');
}
}
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
,所以当浏览器为您提供信息时,您可能是一个像素短。例如:在页面底部我们有
真 window.innerHeight = 10.2
真 window.pageYOffset = 5.4
真 document.body.offsetHeight = 15.6
然后我们的计算变为:10 + 5.4 >= 16 这是错误的
要解决此问题,我们可以对 pageYOffset
值执行 Math.ceil
。
希望有帮助。
我刚开始看这个,这里的答案对我有帮助,所以谢谢你。我已经扩展了一点,以便代码一直安全地回到 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>
如果您对其他方法不满意,请尝试此方法。
window.onscroll = function() { 常量差异 = document.documentElement.scrollHeight - window.innerHeight;常量滚动位置 = document.documentElement.scrollTop; if (difference - scrollposition <= 2) { alert("页面底部!"); } }
$(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");
}
}
如果你喜欢 jquery
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() >= $(document).height()) {
// doSomethingHere();
}
});
const handleScroll = () => {
if (Math.round(window.scrollY + window.innerHeight) >= Math.round(document.body.scrollHeight)) {
onScroll();
}
};
这段代码在 Firefox 和 IE 中也适用于我。
新的解决方案。
一个问题源于缺少标准的主滚动元素。最近实施的 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});
令人惊讶的是,没有一个解决方案对我有用。我认为这是因为我的 css
搞砸了,而 body
在使用 height: 100%
时没有环绕所有内容(还不知道为什么)。然而,在寻找解决方案时,我想出了一些很好的东西......基本上相同,但也许值得一看 - 我是编程新手,如果它的速度同样慢,支持较少或类似的东西,我很抱歉那...
window.onscroll = function(evt) {
var check = (Element.getBoundingClientRect().bottom - window.innerHeight <= 0) ? true : false;
if (check) { console.log("You're at the bottom!"); }
};
使用嵌入了功能代码段的 defaultView
和 documentElement
:
常量 { defaultView } = 文档;常量 { 文档元素 } = 文档; const handler = evt => requestAnimationFrame(() => { const hitBottom = (() => (defaultView.innerHeight + defaultView.pageYOffset) >= documentElement.offsetHeight)(); hitBottom ? console.log('yep') : console.log('nope') }); document.addEventListener('scroll', handler);
向下滚动
如上所述,代码可能不适用于所有设备和浏览器。以下是经过测试的工作代码,将与所有浏览器(Chrome、IE、Edge、Firefox 和 Safari)中的所有主要设备(iPhone、Android、PC)兼容。
window.onscroll = function(ev) { var pageHeight = Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight ); if ((window.innerHeight + window.scrollY) >= pageHeight) { console.log("你在页面底部。"); } };
继续滚动页面直到结束...
您可以检查窗口高度和滚动顶部的组合结果是否大于主体
if (window.innerHeight + window.scrollY >= document.body.scrollHeight) {}
我发现两个对我有用的解决方案:
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')
}
})
使用 vanilla javascript 的最简单方法
container.addEventListener('scroll', (e) => {
var element = e.target;
if (element.scrollHeight - element.scrollTop - element.clientHeight <= 0) {
console.log('scrolled to bottom');
}
});
我只是在我的页面底部放置一个小图像,加载 =“lazy”,这样浏览器只会在用户向下滚动时加载它。然后该图像触发一个返回真实图像的 php 计数器脚本
<img loading="lazy" src="zaehler_seitenende.php?rand=<?=rand(1,1000);?>">
<?php
@header("Location: https://domain.de/4trpx.gif");
我必须想出一种方法(在 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));
顺便说一句,在执行此代码段之前,窗口已最大化。
不定期副业成功案例分享
document.documentElement.scrollTop
而不是window.scrollY
。不确定 IE 的哪个版本(如果有)支持window.scrollY
。