README.md
61 lines
| 1 | # jQuery Mouse Wheel Plugin |
| 2 | |
| 3 | A [](https://jquery.com/jQuery](https://jquery.com/](https://jquery.com/) plugin that adds cross-browser mouse wheel support with delta normalization. |
| 4 | |
| 5 | In order to use the plugin, simply bind the `mousewheel` event to an element. |
| 6 | |
| 7 | It also provides two deprecated helper methods called `mousewheel` and `unmousewheel` |
| 8 | that act just like other event helper methods in jQuery. |
| 9 | |
| 10 | The event object is updated with the normalized `deltaX` and `deltaY` properties. |
| 11 | In addition, there is a new property on the event object called `deltaFactor`. Multiply |
| 12 | the `deltaFactor` by `deltaX` or `deltaY` to get the scroll distance that the browser |
| 13 | has reported. |
| 14 | |
| 15 | Here is an example of using both the bind and helper method syntax: |
| 16 | |
| 17 | ```js |
| 18 | // using on |
| 19 | $( "#my_elem" ).on( "mousewheel", function( event ) { |
| 20 | console.log( event.deltaX, event.deltaY, event.deltaFactor ); |
| 21 | } ); |
| 22 | |
| 23 | // using the event helper - not recommended! |
| 24 | $( "#my_elem" ).mousewheel( function( event ) { |
| 25 | console.log( event.deltaX, event.deltaY, event.deltaFactor ); |
| 26 | } ); |
| 27 | ``` |
| 28 | |
| 29 | The old behavior of adding three arguments (`delta`, `deltaX`, and `deltaY`) to the |
| 30 | event handler is now deprecated and will be removed in later releases. |
| 31 | |
| 32 | |
| 33 | ## The Deltas... |
| 34 | |
| 35 | The combination of browsers, operating systems, and devices offer a huge range of possible delta values. In fact if the user |
| 36 | uses a trackpad and then a physical mouse wheel the delta values can differ wildly. This plugin normalizes those |
| 37 | values so you get a whole number starting at +-1 and going up in increments of +-1 according to the force or |
| 38 | acceleration that is used. This number has the potential to be in the thousands depending on the device. |
| 39 | |
| 40 | ### Getting the scroll distance |
| 41 | |
| 42 | In some use-cases we prefer to have the normalized delta but in others we want to know how far the browser should |
| 43 | scroll based on the users input. This can be done by multiplying the `deltaFactor` by the `deltaX` or `deltaY` |
| 44 | event property to find the scroll distance the browser reported. |
| 45 | |
| 46 | The `deltaFactor` property was added to the event object in 3.1.5 so that the actual reported delta value can be |
| 47 | extracted. This is a non-standard property. |
| 48 | |
| 49 | ## Building the code in the repo & running tests |
| 50 | |
| 51 | ```sh |
| 52 | git clone git@github.com:jquery/jquery-mousewheel.git |
| 53 | cd jquery-mousewheel/ |
| 54 | npm install |
| 55 | npm test |
| 56 | ``` |
| 57 | |
| 58 | The unit tests are _very_ basic sanity checks; improvements welcome. |
| 59 | To fully test the plugin, load [](test/index.htmltest/index.html](test/index.html](test/index.html) in each supported |
| 60 | browser and follow the instructions at the top of the file after the unit tests finish. |
| 61 |