Javascript-javascript tips for disabling scroll wheel events

Source: Internet
Author: User
This article mainly introduces information about how javascript disables scroll wheel events. If you need it, you can refer to what we are compatible with at ordinary times and always adjust the compatibility of earlier IE versions, but this is not because earlier browsers are not powerful. But because Firefox is too powerful, it is totally different from the feelings of other browsers. In addition to Firefox, all browsers can use the MouseWheel event to handle the response of the mouse wheel. However, Firefox does not support MouseWheel, but uses DOMMouseScroll with no headers. This is incompatible with Firefox and other browsers. That is to say, Firefox can only use DOMMouseScroll to handle mouse scroll events. Firefox can only use MouseWheel. The two events have different implementation principles, and they process different data.

var firefox = navigator.userAgent.indexOf('Firefox') != -1;firefox ? img.addEventListener('DOMMouseScroll', MouseWheel, false) :      (img.onmousewheel = MouseWheel);   function MouseWheel(e) {    e = e || window.event;     if (e.stopPropagation) e.stopPropagation();    else e.cancelBubble = true;         if (e.preventDefault) e.preventDefault();    else e.returnValue = false;   }

Let's take a look at the complete code.

Use the scroll wheel to adjust the value

0

Script // judge the browser var isIE = navigator. userAgent. match (/MSIE (\ d)/I); isIE = isIE? IsIE [1]: undefined; var isFF =/FireFox/I. test (navigator. userAgent); // obtain the element var counter = document. getElementById ("counter"); // if (isIE <9) of the mouse wheel event // counter of the MouseWheel event in a traditional browser. attachEvent ("onmousewheel", function () {// calculates the scroll distance of the mouse wheel. // a grid of 3 rows, each row is 40 pixels. Therefore, divided by 120 var v = event. wheelDelta/120; counter. innerHTML = counter. innerHTML * 1 + v; // the default method of blocking browsers: return false;}); else if (! IsFF) // modern browsers except Firefox also use the MouseWheel event counter. addEventListener ("mousewheel", function (e) {// calculate the distance from the scroll wheel var v = e. wheelDelta/120; counter. innerHTML = counter. innerHTML * 1 + v; // block the default browser Method e. preventDefault () ;}, false); else // use DOMMouseScroll event counter for the wonderful Firefox. addEventListener ("DOMMouseScroll", function (e) {// calculate the scroll distance of the mouse wheel. // a grid contains three rows, the difference here is that it is a negative value var v =-e. detail/3; counter. innerHTML = counter. innerHTML * 1 + v; // block the default browser Method e. preventDefault () ;}, false); script

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.