Parse the scroll wheel event in javascript

Source: Internet
Author: User

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.

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.