jquery Event Delegation

社会主义新天地 提交于 2019-12-02 08:14:45

The .hover() is not an event it is just a utility method that adds a mouseenter and mouseleave handlers, so when you want to use event delegation you need to register handlers for those 2 events instead of using hover

$(document).on({
    mouseenter: function () {
        current_h = $(this, 'img')[0].height;
        current_w = $(this, 'img')[0].width;
        $(this).stop(true, false).animate({
            width: (current_w * 2.7),
            height: (current_h * 2.7)
        }, 900);
    },
    mouseleave: function () {
        $(this).stop(true, false).animate({
            width: current_w + 'px',
            height: current_h + 'px'
        }, 400);
    }
}, '.piccon');

Or

$('#videoandmapwrap').on({
    mouseenter: function () {
        current_h = $(this, 'img')[0].height;
        current_w = $(this, 'img')[0].width;
        $(this).stop(true, false).animate({
            width: (current_w * 2.7),
            height: (current_h * 2.7)
        }, 900);
    },
    mouseleave: function () {
        $(this).stop(true, false).animate({
            width: current_w + 'px',
            height: current_h + 'px'
        }, 400);
    }
}, 'img');

Another way to write the same is

$('#videoandmapwrap').on('mouseenter', function () {
    current_h = $(this, 'img')[0].height;
    current_w = $(this, 'img')[0].width;
    $(this).stop(true, false).animate({
        width: (current_w * 2.7),
        height: (current_h * 2.7)
    }, 900);
}, 'img').on('mouseleave', function () {
    $(this).stop(true, false).animate({
        width: current_w + 'px',
        height: current_h + 'px'
    }, 400);
}, 'img');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!