[removed] Capture mouse wheel event and do not scroll the page?

前端 未结 5 645
逝去的感伤
逝去的感伤 2020-11-28 13:29

I\'m trying to prevent a mousewheel event captured by an element of the page to cause scrolling.

I expected false as last parameter to have the expected

相关标签:
5条回答
  • 2020-11-28 13:55

    Finally, after trying everything else, this worked:

       canvas.addEventListener('wheel', (event) => {
        
              // event.preventDefault(); Not Working
              // event.stopPropagation(); Not Working
              event.stopImmediatePropagation(); // WORKED!!
        
             console.log('Was default prevented? : ',event.defaultPrevented); // Says true 
        }, false)
    
    0 讨论(0)
  • 2020-11-28 13:58

    Have you tried event.preventDefault() to prevent the event's default behaviour?

    this.canvas.addEventListener('mousewheel',function(event){
        mouseController.wheel(event);
        event.preventDefault();
    }, false);
    

    Keep in mind that nowadays mouswheel is deprecated in favor of wheel, so you should use

    this.canvas.addEventListener('wheel',function(event){
        mouseController.wheel(event);
        event.preventDefault();
    }, false);
    
    0 讨论(0)
  • 2020-11-28 14:01

    This kind of cancellation seems to be ignored in newer Chrome >18 Browsers (and perhaps other WebKit based Browsers). To exclusively capture the event you must directly change the onmousewheel method of the element.

    this.canvas.onmousewheel = function(ev){
        //perform your own Event dispatching here
        return false;
    };
    
    0 讨论(0)
  • 2020-11-28 14:06

    You can do so by returning false at the end of your handler (OG).

    this.canvas.addEventListener('wheel',function(event){
        mouseController.wheel(event);
        return false; 
    }, false);
    

    Or using event.preventDefault()

    this.canvas.addEventListener('wheel',function(event){
        mouseController.wheel(event);
        event.preventDefault();
    }, false);
    

    Updated to use the wheel event as mousewheel deprecated for modern browser as pointed out in comments.

    The question was about preventing scrolling not providing the right event so please check your browser support requirements to select the right event for your needs.

    Updated a second time with a more modern approach option.

    0 讨论(0)
  • 2020-11-28 14:08

    Just adding, I know that canvas is only HTML5 so this is not needed, but just in case someone wants crossbrowser/oldbrowser compatibility, use this:

    /* To attach the event: */
    addEvent(el, ev, func) {
        if (el.addEventListener) {
            el.addEventListener(ev, func, false);
        } else if (el.attachEvent) {
            el.attachEvent("on" + ev, func);
        } else {
            el["on"+ev] = func; // Note that this line does not stack events. You must write you own stacker if you don't want overwrite the last event added of the same type. Btw, if you are going to have only one function for each event this is perfectly fine.
        }
    }
    
    /* To prevent the event: */
    addEvent(this.canvas, "mousewheel", function(event) {
        if (!event) event = window.event;
        event.returnValue = false;
        if (event.preventDefault)event.preventDefault();
        return false;
    });
    
    0 讨论(0)
提交回复
热议问题