Simple Example of js mouse pulley rolling event binding
This article mainly introduces a simple example of js mouse pulley rolling event binding (compatible with mainstream browsers ). For more information, see.
The Code is as follows:
/** Event handler for mouse wheel event.
* Mouse scroll event
*/
Var wheel = function (event ){
Var delta = 0;
If (! Event)/* For IE .*/
Event = window. event;
If (event. wheelDelta) {/* IE/Opera .*/
Delta = event. wheelDelta/120;
} Else if (event. detail ){
/** Mozilla case .*/
/** In Mozilla, sign of delta is different than in IE.
* Also, delta is multiple of 3.
*/
Delta =-event. detail/3;
}
/** If delta is nonzero, handle it.
* Basically, delta is now positive if wheel was scrolled up,
* And negative, if wheel was scrolled down.
*/
If (delta)
Handle (delta );
/** Prevent default actions caused by mouse wheel.
* That might be uugly, but we handle scrolls somehow
* Anyway, so don't bother here ..
*/
If (event. preventDefault)
Event. preventDefault ();
Event. returnValue = false;
}
/** Initialization code.
* If you use your own event management code, change it as required.
*/
If (window. addEventListener ){
/** DOMMouseScroll is for mozilla .*/
Window. addEventListener ('domainscroll', wheel, false );
}
/** IE/Opera .*/
Window. onmousewheel = document. onmousewheel = wheel;
/** This is high-level function.
* It must react to delta being more/less than zero.
*/
Var handle = function (delta ){
Var random_num = Math. floor (Math. random () * 100) + 50 );
If (delta <0 ){
// Alert ("scroll down the mouse pulley:" + delta + "times! "); // 1
$ ("Btn_next_pic"). onclick (random_num );
Return;
} Else {
// Alert ("scroll up the mouse wheel:" + delta + "times! "); //-1
$ ("Btn_last_pic"). onclick (random_num );
Return;
}
}