Home > database >  disable mouse wheel click
disable mouse wheel click

Time:12-04

how can I disable mouse a wheel click only in a certain div? I want to use the scroll function normally, but I don't want to switch the cursor to that "quick scroll" when someone clicks the mouse wheel.

I tried this, but it has changed nothing

$(document).on('click', function(e){
    if(e.which == 2){
        e.preventDefault(); //stop default click action from mousewheel
    }
});

CodePudding user response:

To disable the mouse wheel click on a certain div in JavaScript, you can add an event listener for the mousedown event to the div element. In the event listener function, you can prevent the default behavior of the mouse wheel click using the preventDefault() method. Here is an example:

const div = document.querySelector('#my-div');
div.addEventListener('mousedown', function(event) {
  if (event.which === 2) {  // Check if the middle mouse button was clicked
    event.preventDefault();  // Prevent the default behavior
  }
});

Keep in mind that this will only prevent the default behavior of the mouse wheel click, but the scroll event will still be triggered. This means that the div will still be able to scroll if the user uses the mouse wheel or if they use the scroll bar. If you want to completely disable the scroll function, you can set the overflow property of the div to "hidden" or "auto" to prevent the div from scrolling.

const div = document.querySelector('#my-div');
div.style.overflow = 'hidden';  // Set the overflow property to prevent scrolling
  • Related