This article introduces the example code of adding the mouse intermediate function mousewheel in Jquery. For more information, see
The Code is as follows:
(Function ($ ){
Var types = ['dommousescroll ', 'mousewheel'];
$. Event. special. mousewheel = {
Setup: function (){
If (this. addEventListener ){
For (var I = types. length; I ;){
This. addEventListener (types [-- I], handler, false );
}
} Else {
This. onmousewheel = handler;
}
},
Teardown: function (){
If (this. removeEventListener ){
For (var I = types. length; I ;){
This. removeEventListener (types [-- I], handler, false );
}
} Else {
This. onmousewheel = null;
}
}
};
$. Fn. extend ({
Mousewheel: function (fn ){
Return fn? This. bind ("mousewheel", fn): this. trigger ("mousewheel ");
},
Unmousewheel: function (fn ){
Return this. unbind ("mousewheel", fn );
}
});
Function handler (event ){
Var orgEvent = event | window. event, args = []. slice. call (arguments, 1), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
Event = $. event. fix (orgEvent );
Event. type = "mousewheel ";
// Old school scrollwheel delta
If (event. originalEvent. wheelDelta) {delta = event. originalEvent. wheelDelta/120 ;}
If (event. originalEvent. detail) {delta =-event. originalEvent. detail/3 ;}
// New school multidimen1_scroll (touchpads) deltas
DeltaY = delta;
// Gecko
If (orgEvent. axis! = Undefined & orgEvent. axis === orgEvent. HORIZONTAL_AXIS ){
DeltaY = 0;
DeltaX =-1 * delta;
}
// Webkit
If (orgEvent. wheelDeltaY! = Undefined) {deltaY = orgEvent. wheelDeltaY/120 ;}
If (orgEvent. wheelDeltaX! = Undefined) {deltaX =-1 * orgEvent. wheelDeltaX/120 ;}
// Add event and delta to the front of the arguments
Args. unshift (event, delta, deltaX, deltaY );
Return $. event. handle. apply (this, args );
}
}) (JQuery );
Call:
$ (Document). mousewheel (function (e, detail ){
}