Javascript 滚动位置等于divs内容高度时的jQuery警报
我希望在div的滚动位置等于div内内容的高度时显示警告消息。我已经编写了一些jQuery,相信我很快就能做到这一点 然而,有些东西不见了。忽略稍后将使用的滚动条div。谢谢 $document.readyfunction{ var box1_高度=$box1.height; var scroll_pos=$box1.scrollTop; 如果框1高度==滚动位置{ 它在工作; } }; * { 保证金:0; 填充:0; } 身体{ 溢出:隐藏; } 背景资料1{ 背景图像:urlhttp://placehold.it/350x150; 背景重复:无重复; 背景尺寸:封面; 背景位置:中心; 位置:相对位置; } .卷轴{ 宽度:100%; 高度:100vh; 溢出:滚动; } .内容{ 宽度:80%; 高度:80vh; 保证金:10vh自动; 背景色:rgba0,0,0,0.6; 溢出:滚动; 框大小:边框框; } 内容在这里Javascript 滚动位置等于divs内容高度时的jQuery警报,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我希望在div的滚动位置等于div内内容的高度时显示警告消息。我已经编写了一些jQuery,相信我很快就能做到这一点 然而,有些东西不见了。忽略稍后将使用的滚动条div。谢谢 $document.readyfunction{ var box1_高度=$box1.height; var scroll_pos=$box1.scrollTop; 如果框1高度==滚动位置{ 它在工作; } }; * { 保证金:0; 填充:0; } 身体{ 溢出:隐藏; } 背景资料1{ 背景图像:urlhttp:/
您需要执行以下操作来检测此问题:
var container = $("#box1") //Fetch the container element once, so we don't squander performance on re-fetches
var box1_height = container.height(); //Get the visible height of the container (not the actual height with scroll)
var scroll_height = container[0].scrollHeight; //Get the content height of the container
var scroll_pos = container.scrollTop(); //Get the top location of the scrollbar in that container
if(scroll_height == box1_height + scroll_pos){ //Check if we're exactly at the bottom
alert("It's working");
}
说明:
这是因为您知道滚动条的顶部值,但要检测您是否在容器底部,您实际上需要知道滚动条的底部值。这是通过以下方式实现的:
scroll_height == box1_height + scroll_pos
如果我理解正确,如果滚动位置位于内容的末尾,您希望触发某些功能?是的,最终结果将触发滚动动画到下一节。您需要在代码中添加滚动事件处理程序,当前仅在页面加载后执行:好的,谢谢。这让我以后省去了很多头疼的事。如果我要求你在每段代码的旁边加上注释来解释它到底在说什么,那会不会太过分了?我不熟悉jQuery。谢谢你,伙计!太好了,谢谢。最后一个问题,容器[0]后面的括号做什么?容器是数组的jQuery对象修改实例。当我们在[0]处取消索引时,最终得到的是原始html元素。然后我们可以访问它的滚动高度。另一种编写方法是container.prop'scrollHeight'