Added non-passive event listener to a scroll-blocking 'touchstart' event

前端 未结 4 1700
旧巷少年郎
旧巷少年郎 2021-01-31 16:32

Suddenly today out of nowhere I started getting this one on every page on our website

Added non-passive event listener to a scroll-blocking \'touchstart\' event.         


        
相关标签:
4条回答
  • 2021-01-31 16:59

    Ok digging this up a little more, this is not a new behavior, it has been reported a while ago and jQuery still hasn't fixed it.

    The problem lies in the fact that for an handler to be passive it has to be certain of never calling preventDefault() but jQuery doesn't know in advance...

    The only tip I could give you is change your console logging level and remove "Verbose". Follow up on this issue for ideas on solving this.

    0 讨论(0)
  • 2021-01-31 17:01

    This solve the problem to me:

    jQuery.event.special.touchstart = {
      setup: function( _, ns, handle ){
        if ( ns.includes("noPreventDefault") ) {
          this.addEventListener("touchstart", handle, { passive: false });
        } else {
          this.addEventListener("touchstart", handle, { passive: true });
        }
      }
    };
    
    0 讨论(0)
  • 2021-01-31 17:14

    The answer from Sergio is correct, add it at the bottom jquery script. If there is issue touchstart and touchmove, just add same code and replace touchstart with touchmove, like this:

    jQuery.event.special.touchstart = {
      setup: function( _, ns, handle ){
        if ( ns.includes("noPreventDefault") ) {
          this.addEventListener("touchstart", handle, { passive: false });
        } else {
          this.addEventListener("touchstart", handle, { passive: true });
        }
      }
    };
    jQuery.event.special.touchmove = {
      setup: function( _, ns, handle ){
        if ( ns.includes("noPreventDefault") ) {
          this.addEventListener("touchmove", handle, { passive: false });
        } else {
          this.addEventListener("touchmove", handle, { passive: true });
        }
      }
    };
    
    0 讨论(0)
  • 2021-01-31 17:24

    I am using various events and this seems to solve my use case

    (function () {
        if (typeof EventTarget !== "undefined") {
            let func = EventTarget.prototype.addEventListener;
            EventTarget.prototype.addEventListener = function (type, fn, capture) {
                this.func = func;
                if(typeof capture !== "boolean"){
                    capture = capture || {};
                    capture.passive = false;
                }
                this.func(type, fn, capture);
            };
        };
    }());
    
    0 讨论(0)
提交回复
热议问题