Parse the scroll wheel event in javascript
This article mainly introduces the scroll wheel event in javascript in detail, which is illustrated in great detail. If you need it, you can refer to it.
All modern browsers support the scroll wheel and trigger time when you scroll the scroll wheel. Browsers usually use the scroll wheel to scroll or scale the document, but you can disable the mousewheel event to prevent these default operations. Some interoperability issues affect scroll events, but you can still write cross-platform code. All browsers except Firefox support the "mousewheel" event, but Firefox uses "DOMMouseScroll". The draft Level 3 DOM Event specification suggests replacing "mousewheel" with the event name "wheel ".
?
1 2 3 4 |
Document. body. onmousewheel = function (event ){ Event = event | window. event; Console. dir (event ); } |
Firefox does not support mousewheel
?
1 2 3 |
Document. body. addEventListener ("DOMMouseScroll", function (event ){ Console. dir (event ); }) |
The following scroll wheel is output in the console of chrome and IE9.
The following is the console output for scroll down Firefox
From the output above, we can use a non-standard DOMMouseScroll event to replace mousewheel, and replace wheelDetal with the detail attribute of the event object. However, the scaling ratio and positive and negative symbols of the detail property value are different from those of the wheelDetal value. The value of the detail value is multiplied by-40 and the value of the wheelDetal value is equal.
In browsers other than FireFox, the scroll up and down is related to the following wheelDelta.
According to the test, in my win7 system, whether IE7, IE10, Opera12, or safari5.1, the value of event. wheelDelta is-120 each time.
For FireFox (also available in operabrowser), the property of the mouse scroll direction is event. detail, and the scroll value is 3.
It should be noted that the positive and negative values of the FireFox browser are opposite to those of other browsers. The FireFox browser scroll down to a positive value, while other browsers scroll down to a negative value.
?
1 2 3 4 5 6 7 |
Var isFirefox = (navigator. userAgent. indexOf ("Firefox ")! =-1 ); If (isFirefox ){ Element. addEventListener ("DOMMouseScroll", wheelHandler, false ); } Element. onmousewheel = wheelHandler; // Element. onwheel = wheelHandler; // DOM3-level wheel events are not supported after IE9 test, but both Google and Firefox support them. In Google, wheelDelta has detail in Firefox. Function wheelHandler (event) {event = event | window. event; var delta = event. wheelDelta | detail *-30 ;} |
The above is all the content of this article. I hope you will like it.