We have all seen these effects. Use the mouse wheel to increase or decrease the numbers in a form, or use the mouse wheel to control the left and right, up and down scrolling of a button. These are all implemented through js event monitoring of the mouse wheel. What we introduce here today is some simple js monitoring of mouse wheel events.
Different events in different browsers
First of all, different browsers have different scroll wheel events. There are mainly two types, onmousewheel (not supported by firefox) and DOMMouseScroll (only supported by firefox). I will not go into details about these two events here. Friends who want to know more, please go to: mousewheel (mousewheel) and DOMMouseScroll events.
In addition, event listening needs to be added during the operation. The code is as follows: Compatible with firefox, use addEventListener to monitor
js returns numerical value to judge the up and down of the scroll wheel
When judging whether the scroll wheel is up or down, compatibility must also be considered in the browser. Currently, among the five major browsers (IE, Opera, Safari, Firefox, Chrome), Firefox uses detail, and the other four types use wheelDelta; both of them only use wheelDelta. The values are inconsistent, which means the meaning is consistent. detail and wheelDelta only take two values each, detail only takes ±3, and wheelDelta only takes ±120, where positive numbers represent upwards and negative numbers represent downwards.
The specific code is as follows:
By running the above code we can see: