Native JS bound pulley rolling events compatible with common browsers _javascript tips

Source: Internet
Author: User

The events of the Pulley scrolling page are encountered in the web effects, but they are implemented differently in different browsers. The following methods I implemented are compatible with common browsers.

function GetData (event) { 
var e = Event | | window.event; 
Gets the scrolling distance (FF each scrolling data is 3 or-3, others are 120 or -120) 
var data = E.detail | | e.wheeldelta; 
alert (data); 
} 

Binding event methods other than IE 
if (document.addeventlistener &&!document.attachevent) 
{ 
Document.addeventlistener (' MouseWheel ', getData); 
FF bound scrolling Event 
document.addeventlistener (' Dommousescroll ', getData); 
} 
IE 
Else if (document.attachevent &&!document.addeventlistener) { 
document.attachevent (' OnMouseWheel ', getData); 
} else{ 
window.onmousewheel = getData; 
}

Notable in your code:

1 Why use Document.addeventlistener &&!document.attachevent to differentiate IE?

Attachevent and DetachEvent are the Internet-specific binding event and bind event methods that exist only in IE. However, there is a more general AddEventListener method to bind events in ie9+ browsers. There is a Document.addeventlistener method in the browser that excludes the IE8 and its version, but includes the Ie9+ browser, so &&!document.attachevent is used later to exclude the Ie9+ browser.

2 It is noteworthy that there is no MouseWheel event in the FF browser, and the time to trigger scrolling is dommousescroll.

3 It is also noteworthy that when you use the AddEventListener binding event, the event name is not preceded on, and you need to add on when using the Attachevent binding event in IE.

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.