Home >Web Front-end >JS Tutorial >Mouse wheel programming_javascript skills

Mouse wheel programming_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:21:381042browse

I didn't notice this scroll wheel before, but I saw an article here about it: http://www.javascriptsearch.com/guides/Advanced/articles/JSMouseScrolling.html

I put it Turned around. If it is used in actual applications, sometimes it is quite useful. The main thing is to get whether the scroll wheel rolls up or down.

Commented code


function handle(delta) {
if (delta < 0)
…;
else
…;
}

/**Event handler
*/
function wheel(event){
var delta = 0;
if (!event) /* For IE. */
event = window.event;
if ( event.wheelDelta) { /* IE or Opera. */
delta = event.wheelDelta/120;
/**In Opera9, event handling is different from IE
*/
if (window.opera)
delta = - delta;
} else if (event.detail) { /**Compatible with Mozilla.*/
/**In Mozilla, sign of delta is different than in IE.
 * Also, delta is multiple of 3.
 */
delta = -event.detail/3;
}
/**If the increment is not equal to 0, it will trigger
* The main function is to test whether the wheel rolls up or down
*/
if (delta)
handle(delta);
}

/**initialization*/
if (window. addEventListener)
/**Mozilla's DOM-based scroll wheel events **/
window.addEventListener('DOMMouseScroll', wheel, false);
/**IE/Opera.*/
window.onmousewheel = document.onmousewheel = wheel;

In the above code, the code in the handler function is what you need to write. It has a parameter -delta. In fact, it is just the code to determine whether the mouse is scrolling up or down at the moment. As shown below,
uploads/200608/02_013450_deltas.gif

[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn