Javascript 在窗口滚动功能上设置高度边距
当在页面上滚动时,尝试获取警报已达到该页面末尾上方500px。(跳过页脚以自动加载新内容。) 在越位方面做了很多尝试,但都没有成功 这是我使用的函数,但如果我理解正确,需要将其调整为-500Javascript 在窗口滚动功能上设置高度边距,javascript,jquery,Javascript,Jquery,当在页面上滚动时,尝试获取警报已达到该页面末尾上方500px。(跳过页脚以自动加载新内容。) 在越位方面做了很多尝试,但都没有成功 这是我使用的函数,但如果我理解正确,需要将其调整为-500 $(window).scroll(function() { if($(window).scrollTop() == $(document).height() - $(window).height()) { alert('more please...'); } 感谢您的帮助 我想你就是在找这
$(window).scroll(function() {
if($(window).scrollTop() == $(document).height() - $(window).height()) {
alert('more please...');
}
感谢您的帮助 我想你就是在找这个
$(window).scroll(function() {
if($(document).scrollTop() == $(document).height() - 500) {
alert('more please...');
}
}
您需要一个小公式来查看是否已滚动到保存区域:
(SaveHeight + ScrollTop) > (ContentHeight - WindowHeight)
设置保存区域的高度:
const SaveHeight = 500;
使用以下选择器获取值:
let ScrollTop = $(document).scrollTop();
let ContentHeight = $("#content").height(); // or $(document).height()
let WindowHeight = $(window).height();
总而言之:
//配置
常数SaveHeight=500;
//设置指示器
$(“#指示器”)。高度(保存高度);
//滚动事件
$(文档).on(“滚动”,函数(){
//获取价值
ScrollTop=Math.round($(document.ScrollTop());
ContentHeight=Math.round($(“#content”).height();
WindowHeight=Math.round($(window.height());
//展示真实的价值观
scrollis=保存高度+ScrollTop;
scrollwas=内容高度-窗口高度;
calculation=“(“+SaveHeight+”+“+”+ScrollTop+”>(“+ContentHeight+”-“+WindowHeight+”)
“+scrollis+”>“+scrollwas+”;
$(“#计算”).html(计算);
//对价值观作出反应
如果(保存高度+滚动顶部>内容高度-窗口高度){
$(“#content”).css(“背景”,“#ffeee”);
}
否则{
$(“#content”).css(“背景”,“#ffffff”);
}
});代码>
#内容{高度:2000px;位置:相对;}
#指示器{高度:100vh;背景:红色;宽度:10px;位置:绝对;底部:0px;}
#计算{位置:固定;顶部:10px;右侧:10px;文本对齐:中心;}
向下滚动
谢谢@sefzig!我必须首先完成这一切。非常感谢您在这里的努力,效果非常好,并感谢您的明确评论!我很高兴这有帮助,@KJS:)