[英]How to handle two scroll events in a webpage
two js function loads on scroll.滚动加载两个 js function。 Say there is two onscroll events to handle
假设有两个 onscroll 事件要处理
window.onscroll = function(e) {
var footerHeight = 500;
if ((window.innerHeight + window.scrollY + footerHeight) >= document.body.offsetHeight) {
if (branchLoaded == false) {
loadBranch1();
};
}
};
window.onscroll = function(e) {
var footerHeight = 500;
if ((window.innerHeight + window.scrollY + footerHeight) >= document.body.offsetHeight) {
if (branchLoaded == false) {
loadBranch2();
};
}
};
In your code, the second event handler is overriding the first one.在您的代码中,第二个事件处理程序将覆盖第一个事件处理程序。
This is similar to <element onload="function(){}" onload="function(){}">
.这类似于
<element onload="function(){}" onload="function(){}">
。 In it, Only the latter one will execute.在其中,只有后一个会执行。
window.onscroll = function(){console.log(1)} window.onscroll = function(){console.log(2)}
<div>Test<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>Bottom</div>
You need addEventListener
to set up multiple Event Listener.您需要
addEventListener
来设置多个事件监听器。
window.addEventListener('scroll',function(){console.log(1)}); window.addEventListener('scroll',function(){console.log(2)});
<div>Test<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>Bottom</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.