Mouse scroll event

Source: Internet
Author: User
This article mainly discusses compatibility issues.

After testing, ie/opera belongs to the same type. You can use attachevent to add scroll wheel events.

/* Ie registration event */If (document. attachevent) {document. attachevent ('onmousewheel ', scrollfunc );}

Firefox uses addeventlistener to add scroll wheel events

/* Firefox registration event */If (document. addeventlistener) {document. addeventlistener ('dommousescroll ', scrollfunc, false );}

Safari and chrome belong to the same type. You can use HTML Dom to add events.

window.onmousewheel=document.onmousewheel=scrollFunc;//IE/Opera/Chrome

Except Firefox, you can use HTML Dom to add events. Therefore, the following method is used to add events:

/* Register the event */If (document. addeventlistener) {document. addeventlistener ('domainscroll', scrollfunc, false);} // w3cwindow. onmousewheel = document. onmousewheel = scrollfunc; // IE/Opera/chrome

Detail and wheeldelta

Compatibility should also be considered when judging the scroll wheel up or down in the browser. In the five browsers (ie, opera, Safari, Firefox, Chrome), Firefox uses detail, and the other four use wheeldelta; the two values are inconsistent, indicating the same meaning. Detail and wheeldelta take only two values, detail take only ± 3, and wheeldelta take only ± 120, where positive numbers represent upward, negative number indicates downward.

Code highlighting produced by actipro codehighlighter (freeware) http://www.CodeHighlighter.com/--> 1 <p> <label for = "wheeldelta"> scroll value: </label> (ie/opera) <input type = "text" id = "wheeldelta"/> </P> <p> <label for = "detail"> scroll value: (Firefox) </label> <input type = "text" id = "detail"/> </P> <SCRIPT type = "text/JavaScript"> var otxt = document. getelementbyid ("TXT");/************************** function: determine the scroll wheel direction * parameter: Event * return: scroll wheel direction 1: Up-1: down ************************/var scrollfunc = function (e) {var direct = 0; E = E | window. event; var T1 = document. getelementbyid ("wheeldelta"); var t2 = document. getelementbyid ("detail"); If (E. wheeldelta) {// IE/Opera/chrome t1.value = E. wheeldelta;} else if (E. detail) {// Firefox t2.value = E. detail;} scrolltext (direct);}/* Register event */If (document. addeventlistener) {document. addeventlistener ('domainscroll', scrollfunc, false);} // w3cwindow. onmousewheel = document. onmousewheel = scrollfunc; // IE/Opera/Chrome/Safari </SCRIPT>

 

Mouse scroll event

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.