禁用预测滚动 - 鼠标滚轮 (OnScroll) 事件触发过于频繁(触摸板)

Disable Predictive Scrolling - Mousewheel (OnScroll) Event fires too often (Touchpad)

我正在执行JavascriptonScroll。 我的代码适用于任何普通的电脑鼠标,但是当我使用笔记本的触摸板时,我遇到以下情况:

我从移动触摸设备知道这种行为。该功能称为 "Predictive Touch" - 如果您的手指移动在抬起之前有足够的加速度,则滚动会继续。

我认为触摸板驱动程序正在设置此 "smooth scrolling" 行为。

为了调试这个案例,我使用了以下代码:

/* Handle Mouse-Wheel Scrolling */
var lastChange = +new Date();
$(window).bind('mousewheel',    function(e){
    console.log("mw");
    if(+new Date() - lastChange > 1000){
        console.log("mw allowed");
        if(e.originalEvent.wheelDelta > 0)  {/*go to previous*/}
        else{   /*go to next*/}
        lastChange = +new Date();
    }
return false;});

这是一个简单的代码,每秒 "allows" 一次鼠标滚动事件。

如果我进行快速触摸板滚动,mousewheel 事件将被触发约 300 次。一秒条件是让 3 个事件发生。我的手指在触摸板上停留的时间远不到一秒钟。

通过这个测试,我发现 mousewheel 事件仍然被触发(几乎连续 3 秒),即使我的手指已经离开触摸板。

是否有 Javascript 函数或变通方法/技巧/技巧来避免这种行为?

可能是触摸板的 onTouchEnd 事件?

编辑:这似乎不适用于触控板。一旦它们得到广泛支持,就可以使用 Touch Events, specifically the Touch End 事件来实现。通过跟踪手指何时离开触控板,您可以防止页面在该特定点滚动。

https://jsfiddle.net/gLkkb5z0/3/

(function(){

    var special = jQuery.event.special,
        uid1 = 'D' + (+new Date()),
        uid2 = 'D' + (+new Date() + 1);

    special.scrollstart = {
        setup: function() {

            var timer,
                handler =  function(evt) {

                    var _self = this,
                        _args = arguments;

                    if (timer) {
                        clearTimeout(timer);
                    } else {
                        evt.type = 'scrollstart';
                        jQuery.event.handle.apply(_self, _args);
                    }

                    timer = setTimeout( function(){
                        timer = null;
                    }, special.scrollstop.latency);

                };

            jQuery(this).bind('scroll', handler).data(uid1, handler);

        },
        teardown: function(){
            jQuery(this).unbind( 'scroll', jQuery(this).data(uid1) );
        }
    };

    special.scrollstop = {
        latency: 300,
        setup: function() {

            var timer,
                    handler = function(evt) {

                    var _self = this,
                        _args = arguments;

                    if (timer) {
                        clearTimeout(timer);
                    }

                    timer = setTimeout( function(){

                        timer = null;
                        evt.type = 'scrollstop';
                        jQuery.event.handle.apply(_self, _args);

                    }, special.scrollstop.latency);

                };

            jQuery(this).bind('scroll', handler).data(uid2, handler);

        },
        teardown: function() {
            jQuery(this).unbind( 'scroll', jQuery(this).data(uid2) );
        }
    };

})();

Demo

取自http://james.padolsey.com/javascript/special-scroll-events-for-jquery/

要实现这一点,您必须区分鼠标滚动事件和触摸板事件,使用 JavaScript(目前)还不可能做到这一点。已经有问题 .

Pointer Events are currently in state of Editor's Draft and not yet supported by any browser. See also touch events docs on MDN.