欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Jquery增加鼠标中间功能mousewheel的实例代码

程序员文章站 2022-10-15 22:33:39
代码如下: (function ($) {     var types = ['dommousescroll', '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) {

}