jQuery 1.9.1 $.event.handle.apply alternative

2019-02-22 03:58发布

I recently updated one of my project to jQuery 1.9.1, and I can no longer use $.event.handle.apply() method. I've searched and found, that I can place jquery.migrate.js. I just want to confirm if there is any other option? My google-fu is failing here...

--EDIT-- Here is the code (not mine... copied from the plugin) that is causing the issue...

// Event handler function
function mouseWheelHandler(event)
{
    var sentEvent = event || window.event,
        orgEvent = sentEvent.originalEvent || sentEvent,
        args = [].slice.call( arguments, 1 ),
        delta = 0,
        deltaX = 0,
        deltaY = 0;
        event = $.event.fix(orgEvent);
        event.type = "mousewheel";

    // Old school scrollwheel delta
    if ( orgEvent.wheelDelta ) { delta = orgEvent.wheelDelta/120; }
    if ( orgEvent.detail     ) { delta = -orgEvent.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);
}

5条回答
做自己的国王
2楼-- · 2019-02-22 04:16

I found this topic while looking for a solution for Infinite Scroll which does not work with jQuery 1.9.* and causes the same error as from the original post.

JQNinja's comment provided a suitable solution: changing the single instance of $.event.handle.apply to $.event.dispatch.apply in the plugin fixes Infinite Scroll. I am assuming it will also fix the original poster's problem.

I understand that modifying a plugin is not ideal, but since it is no longer maintained at the time of writing, I probably don't have to concern myself with updates -- and let's assume that, should maintenance be resumed, this problem will be fixed as well!

查看更多
孤傲高冷的网名
3楼-- · 2019-02-22 04:18

i Found solution here , i test it and it work fine
Get mouse wheel events in jQuery?

$(document).ready(function(){
    $('#foo').bind('mousewheel', function(e){
        if(e.originalEvent.wheelDelta /120 > 0) {
            $(this).text('scrolling up !');
        }
        else{
           $(this).text('scrolling down !');
        }
    });
});
查看更多
Explosion°爆炸
4楼-- · 2019-02-22 04:19

or you can try:

$.event.dispatch.call( obj, event );

查看更多
虎瘦雄心在
5楼-- · 2019-02-22 04:20

change this line:

return $.event.handle.apply(this, args);

to:

return jQuery.event.handle.apply(this, args);

查看更多
Evening l夕情丶
6楼-- · 2019-02-22 04:21

As you will see here this plugin wasn't longer developped. But it the end of this discussion you have a way to solve some problem :

On line 642 of the plugin js file I have this:

event.type = 'lastclick';
$.event.handle.apply(Me, [event,clicks])

I just changed those two to this:

event.type = 'lastclick';
event.isPropagationStopped = function() { return false };
$.event.handle.apply(Me, [event,clicks])

So, I just reseted the isPropagationStopped and sudently it started working fine!

I hope this will help you.

查看更多
登录 后发表回答