Disable scrolling on `<input type=number>`

后端 未结 15 1804
小蘑菇
小蘑菇 2020-12-02 07:13

Is it possible to disable the scroll wheel changing the number in an input number field? I\'ve messed with webkit-specific CSS to remove the spinner but I\'d like to get rid

15条回答
  •  日久生厌
    2020-12-02 07:53

    First you must stop the mousewheel event by either:

    1. Disabling it with mousewheel.disableScroll
    2. Intercepting it with e.preventDefault();
    3. By removing focus from the element el.blur();

    The first two approaches both stop the window from scrolling and the last removes focus from the element; both of which are undesirable outcomes.

    One workaround is to use el.blur() and refocus the element after a delay:

    $('input[type=number]').on('mousewheel', function(){
      var el = $(this);
      el.blur();
      setTimeout(function(){
        el.focus();
      }, 10);
    });
    

提交回复
热议问题