Jquery增加鼠标中间功能mousewheel的实例代码
代码如下:
(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 multidimensional 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);
调用:
$(document).mousewheel(function (e, detail) {
}
上一篇: 孔雀开屏花样多
下一篇: 男人和女人对抗要讲策略