Tips for parsing the scroll wheel Event _ javascript in javascript

Source: Internet
Author: User
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 ".

document.body.onmousewheel = function(event){  event = event || window.event;  console.dir(event);}

Firefox does not support mousewheel

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, each time I scroll downevent.wheelDeltaAll values-120.

For the FireFox browser (also available in operabrowser), the attribute of judging the mouse scroll direction isevent.detail, Scroll down3.

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.

Var isFirefox = (navigator. userAgent. indexOf ("Firefox ")! =-1); if (isFirefox) {element. addEventListener ("DOMMouseScroll", wheelHandler, false);} element. onmousewheel = wheelHandler; // element. onwheel = wheelHandler; // DOM3-level wheel events, which are not supported after IE9 tests, but are supported by both Google and Firefox. in Google, there is a wheelDelta with the detailfunction wheelHandler (event) in Firefox) {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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.