Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检测滚动方向时出错_Javascript_Jquery - Fatal编程技术网

Javascript 检测滚动方向时出错

Javascript 检测滚动方向时出错,javascript,jquery,Javascript,Jquery,从-,的这个问题开始,我写了这个代码 var lastS=$(window).scrollTop(); function whatHappened() { var currentS=$(window).scrollTop(); console.log(lastS,currentS); if(currentS>lastS) { lastS = currentS; return "Scroll down"; } e

从-,的这个问题开始,我写了这个代码

var lastS=$(window).scrollTop();

function whatHappened()
{
    var currentS=$(window).scrollTop();
    console.log(lastS,currentS);

    if(currentS>lastS)
    {
       lastS = currentS;
       return "Scroll down";
    }
    else
    {
        lastS = currentS;
        return "Scroll up";
    }
}

$(window).on("mousewheel",function(){
    console.log(whatHappened());
});
这似乎很好,但有一个小问题。如果我向下滚动两次以上,然后向上滚动,它首先显示向下滚动,然后向上滚动。反之亦然。 发生了什么? 请帮忙。提前谢谢


JS Fiddle:

鼠标滚轮事件是非标准的-在您的情况下,它可能在滚动信息更新之前被触发,因此您总是在“上一个”滚动高度上操作。

好的,但是我应该使用什么替代方法?.scroll(),就像您所述的示例一样