Javascript 使用scrollTop()从浏览器窗口底部检测30%
我的代码可以像下面这样检测浏览器窗口底部,然后运行Javascript 使用scrollTop()从浏览器窗口底部检测30%,javascript,jquery,Javascript,Jquery,我的代码可以像下面这样检测浏览器窗口底部,然后运行last_msg_funition(): $(window).scroll(function(){ if ($(window).scrollTop() == $(document).height() - $(window).height()){ last_msg_funtion(); } }); 我的问题是用户需要向下滚动到底部(页脚),然后last_msg_funtion()将运行,但我想调整检测可能从底部3
last_msg_funition()代码>:
$(window).scroll(function(){
if ($(window).scrollTop() == $(document).height() - $(window).height()){
last_msg_funtion();
}
});
我的问题是用户需要向下滚动到底部(页脚),然后last_msg_funtion()代码>将运行,但我想调整检测可能从底部30%左右
见下图:
我的网站:
完整代码:请尝试以下操作:
$(window).scroll(function(){
if ($(window).scrollTop() == ($(document).height() * 0.7) - $(window).height()){
last_msg_funtion();
}
});
注意*0.7
表示当滚动条沿页面向下70%时,即从底部向下30%时,该功能将启动。请尝试以下操作:
$(window).scroll(function(){
if ($(window).scrollTop() >= ($(document).height() * 0.7) - $(window).height()){
last_msg_funtion();
}
});
在if条件下,必须使用=
如果使用==
,则仅当滚动70%时,last_msg_Function()才会调用
如果您使用=
,则last_msg_Function()将仅在滚动大于70%时调用。我猜您的代码是正确的,但idk为什么在实现后不工作,只是为了进一步说明,这是因为当javascript对此进行检测时,它不会以1的平滑增量进行,因此,如果你检查一个精确的值,它可能会被击中,也可能不会被击中。