我有一个设置了overflow
为scroll
的div
层。
当滚动到div
的底部时,我想要运行一个函数。
我有一个设置了overflow
为scroll
的div
层。
当滚动到div
的底部时,我想要运行一个函数。
原先被接受的回答存在根本性错误,因此已被删除。正确的答案是:
function scrolled(e) {
if (myDiv.offsetHeight + myDiv.scrollTop >= myDiv.scrollHeight) {
scrolledToBottom(e);
}
}
我在Firefox、Chrome和Opera中测试过了,它可以用。
window.location.href = 'http://newurl.com';
或 window.open('http://newurl.com');
。 - BjornmyDiv.offsetHeight + myDiv.scrollTop >= myDiv.scrollHeight
总是返回 false。这种情况下,请使用 Math.round(myDiv.offsetHeight + myDiv.scrollTop) >= myDiv.scrollHeight
。 - cguyif ((window.innerHeight + window.scrollY) >= document.body.offsetHeight)
{
//your code here
}
我也搜索了它,即使查看了所有评论和更多信息,这是检查是否到达底部的解决方案。
我无法让上述两个答案起作用,所以这里有第三个选项适用于我!(这与jQuery一起使用)
if (($(window).innerHeight() + $(window).scrollTop()) >= $("body").height()) {
//do stuff
}
好的,这里有一个好的适当的解决方案。
你有一个带有id="myDiv"
的Div调用。
所以函数如下。
function GetScrollerEndPoint()
{
var scrollHeight = $("#myDiv").prop('scrollHeight');
var divHeight = $("#myDiv").height();
var scrollerEndPoint = scrollHeight - divHeight;
var divScrollerTop = $("#myDiv").scrollTop();
if(divScrollerTop === scrollerEndPoint)
{
//Your Code
//The Div scroller has reached the bottom
}
}
$(window).scroll(function() {
buffer = 40 // # of pixels from bottom of scroll to fire your function. Can be 0
if ($(".myDiv").prop('scrollHeight') - $(".myDiv").scrollTop() <= $(".myDiv").height() + buffer ) {
doThing();
}
});
必须使用jQuery 1.6或更高版本
我基于Bjorn Tipling的答案创建了一个基于事件的解决方案:
(function(doc){
'use strict';
window.onscroll = function (event) {
if (isEndOfElement(doc.body)){
sendNewEvent('end-of-page-reached');
}
};
function isEndOfElement(element){
//visible height + pixel scrolled = total height
return element.offsetHeight + element.scrollTop >= element.scrollHeight;
}
function sendNewEvent(eventName){
var event = doc.createEvent('Event');
event.initEvent(eventName, true, true);
doc.dispatchEvent(event);
}
}(document));
您可以这样使用该事件:
document.addEventListener('end-of-page-reached', function(){
console.log('you reached the end of the page');
});
html, body {
height: 100%;
}
这是一个演示链接,可以展示相关的IT技术。请点击链接查看详情。现在有一个实验性的onscrollend
事件https://developer.mozilla.org/en-US/docs/Web/API/Document/scrollend_event
目前只在Firefox 109+中工作,如果其他浏览器跟上会很好。
有一个polyfill可以解决这个问题https://github.com/argyleink/scrollyfills 使用方法如下:
import "scrollyfills";
...
scrollContainer.addEventListener(
"scrollend",
(ev) => { console.log('scroll END') }
);
我找到了一个可行的替代方案。
这些答案都不适用于我(当前在FireFox 22.0中测试),经过大量研究,我发现了一个似乎更加简洁明了的解决方案。
实施的解决方案:
function IsScrollbarAtBottom() {
var documentHeight = $(document).height();
var scrollDifference = $(window).height() + $(window).scrollTop();
return (documentHeight == scrollDifference);
}
敬礼
function scrolled(event) {
const container = event.target.body
const {clientHeight, scrollHeight, scrollY: scrollTop} = container
if (clientHeight + scrollY >= scrollHeight) {
scrolledToBottom(event);
}
}
//attaches the "scroll" event
$(window).scroll(function (e) {
var target = e.currentTarget,
scrollTop = target.scrollTop || window.pageYOffset,
scrollHeight = target.scrollHeight || document.body.scrollHeight;
if (scrollHeight - scrollTop === $(target).innerHeight()) {
console.log("► End of scroll");
}
});