-1

I tried this, it didn't work

$('body').bind("mousewheel", function() {
    return false;
});

then I tried this

$('body').hover(function (){
    $('body').css('overflow','hidden');
}, 
function (){
    $('body').css('overflow','auto');
})

It sometimes works, but sometimes didn't work.

Is there any idea to disable the mouse wheel?

Intervalia
  • 8,536
  • 1
  • 21
  • 47
Elvinci Chen
  • 101
  • 1
  • 6
  • Only mousewheel scrolling? or all scrolling – Kevin B Apr 08 '19 at 19:37
  • 1
    Possible duplicate of [How to disable scrolling temporarily?](https://stackoverflow.com/questions/4770025/how-to-disable-scrolling-temporarily) – chennighan Apr 08 '19 at 19:39
  • 1
    You're assuming a lot with your code. F.e. scrolling is possible by means of arrow keys as well, touch gestures etc. Why would you like to disable scrolling? – Andy Apr 08 '19 at 19:39
  • So basically you want to lock the document body for scrolling when the body get hovered? – Rafv Apr 08 '19 at 19:40
  • 1
    `body { display: none; }` – Kevin B Apr 08 '19 at 19:41
  • I would look at what it is that's making you want to disable the mouse wheel. If it's scrolling there is css for handling the scroll bar. If it's magnification of images or similar, they can be handled as well without trying to disable the wheel altogether. – nocturns2 Apr 08 '19 at 20:26

1 Answers1

0

Note that in current versions of Chrome, scroll and wheel handlers are treated as passive by default and so can't be cancelled. See feature info.

I'm not sure if there's a way to specify an active handler using jQuery's on method, but you can do this fairly easy with the built-in addEventListener. Also, in this case return false won't work, you'll need to call preventDefault().

document.body.addEventListener('wheel', function(e){ e.preventDefault(); }, { passive: false });
p { width: 320px; }
<p>Lorem ipsum dolor sit amet, audire facilis no pri, ea eam duis laboramus, at pro atqui tollit meliore. Vis ut malorum vocibus percipit, nisl atqui ea sea. Illud errem te vix, ex facer omittam his. Ne sit consul suscipit. Eu usu percipit efficiendi dissentiet.</p>
<p>Te ius dictas oporteat facilisi. Dicta diceret debitis et vim. Paulo insolens comprehensam est cu, dolor assueverit eu mei. Pro an facilisis rationibus. Summo molestie mei cu. His nobis erroribus in, iuvaret repudiare delicatissimi mea ut, nec at elit nostro.</p>
<p>Ad augue invenire efficiendi qui, facilisi mediocritatem ei duo. Ceteros mnesarchum et has, at ius choro dolor verear. Purto sententiae interesset et vim, quis nisl facer vix ei. Est agam ocurreret assentior id, et partem fabellas vim.</p>

I've tested solution in Chrome 73 and Firefox 66. To make sure this works in all browsers, be sure to read MDN and quirksmode.

p.s.w.g
  • 136,020
  • 27
  • 262
  • 299