我想知道如何区分两个不同的事件“滚起”和“滚下”。Function1应在rollup上执行,Function2应在rolldown上执行。我刚刚看到了onmousewheel和onscroll事件,这两个事件都在上滚/下滚中调用。
回答我的问题:
function up(){ alert("you rolled up with mouse wheel")};
function down() { alert("you rolled down with mouse wheel")}; 如何避免上述函数同时执行?
发布于 2018-03-31 22:15:22
如果你想使用jQuery,你可以选择这个答案:
How can I determine the direction of a jQuery scroll event?
var lastScrollTop = 0;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
// downscroll code
} else {
// upscroll code
}
lastScrollTop = st;
});基本上,您存储最后一个滚动值,并将其与新的值进行比较。还有一个很酷的代码笔示例,其中包含一个delta值,用于配置解决方案的敏感性:https://codepen.io/josiahruddell/pen/piFfq
https://stackoverflow.com/questions/49588621
复制相似问题