Skip to content

Instantly share code, notes, and snippets.

@pererinha
Created June 30, 2014 12:58
Show Gist options
  • Select an option

  • Save pererinha/aaef044b021bbf7372e5 to your computer and use it in GitHub Desktop.

Select an option

Save pererinha/aaef044b021bbf7372e5 to your computer and use it in GitHub Desktop.
Angular JS Directive - to disable mouse wheel on input type number
directive( 'ignoreMouseWheel', function( $rootScope ) {
return {
restrict: 'A',
link: function( scope, element, attrs ){
element.bind('mousewheel', function ( event ) {
element.blur();
} );
}
}
} );
// <input type="number" ignore-mouse-wheel ... >
@drew-y

drew-y commented Sep 30, 2014

Copy link
Copy Markdown

Thank You!

@stianlp

stianlp commented Feb 13, 2015

Copy link
Copy Markdown

Thank you! Had to use element[0].blur();

ghost commented Aug 23, 2015

Copy link
Copy Markdown

@stianlp: same here

@gylu

gylu commented Sep 26, 2015

Copy link
Copy Markdown

thank you!!!!!

@joshuapowell

Copy link
Copy Markdown

Great little directive; I also had to implement the replacement by @stianlp of element.blur() with element[0].blur(); My final directive look like this:

(function() {

  'use strict';

  /**
   * @ngdoc directive
   * @name
   * @description
   */
  angular.module('managerApp')
    .directive('ignoreMouseWheel', function () {
      return {
        restrict: 'A',
        link: function (scope, element) {
          element.bind('mousewheel', function() {
            element[0].blur();
          });
        }
      };
    });

}());

@gbreen12

Copy link
Copy Markdown

I added a bit to not lose focus on the input:

directive('ignoreMouseWheel', function ($rootScope, $timeout) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            element.bind('mousewheel', function (event) {
                element.blur();
                $timeout(function () {
                    element.focus();
                }, 10);
            });
        }
    }
});

Basically following a comment on this SO comment: http://stackoverflow.com/questions/9712295/disable-scrolling-on-input-type-number

@carmelc

carmelc commented Jan 13, 2016

Copy link
Copy Markdown

If you want to maintain normal scroll behavior and prevent focus-blur odd blink effects, you can use:

directive('ignoreMouseWheel', function ($document) {
    return {
      restrict: 'A',
      link: function (scope, element) {
        element.bind('mousewheel', function (event) {
          var scrollAmount = event.originalEvent.wheelDelta * -1 + $document.scrollTop();
          event.preventDefault();
          $document.scrollTop(scrollAmount);
        });
      }
    }
  });

It is an angular implementation of http://codepen.io/pixelthing/pen/LIqsg

@silvbaumann

Copy link
Copy Markdown

thx!

@bazizten

Copy link
Copy Markdown

Big thanks @carmelc....:)

@MusicMonkey5555

Copy link
Copy Markdown

@carmelc Thanks for that update, but I was wondering if you or anyone else were able to tweak it so it doesn't skip up the page when scrolling over a field. Trying to figure out if the blink or the jump is worse.
Great resources all together though. Really sad there isn't something built into the html for this since it can get really annoying.

@MusicMonkey5555

Copy link
Copy Markdown

I was going to say I opted for the blink, but that also has the side affect of changing what field is focused.

ghost commented Jan 23, 2018

Copy link
Copy Markdown

For me it was element.bind('wheel', ... and element[0]

@henck

henck commented Dec 3, 2018

Copy link
Copy Markdown

The wheel event is compatible with both Chrome and Firefox. In Firefox, mousewheel does not work. See also here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment