javascript - 在动画期间拒绝 jQuery 事件

标签 javascript jquery jquery-events

悬停时,我想为一个 div 设置动画。在动画期间,我想多次禁用悬停的可能性以避免动画调用。我用 unbind 删除了 mouseenter 事件。动画完成后,应该再次添加 mouseenter 事件,但我无法完成这项工作。

这是 jQuery:

items.hover(function (e) {
    $(e.currentTarget).unbind('mouseenter');
    if ($(this).hasClass('xy')) {
        $('div.block', this).addClass('xxx').removeClass('zzz').animate({
            top: '0'
        });
    }
}, function (e) {
    if ($(this).hasClass('xy')) {
        $('div.block', this).animate({
            top: topHeightVal
        }, 200, function () {
            $(e.currentTarget).bind('mouseenter');
        }).addClass('zzz').removeClass('xxx');
    }
});

最佳答案

尝试 .stop()

Stop the currently-running animation on the matched elements.

你的代码变成

items.hover(function (e) {
    $(e.currentTarget).unbind('mouseenter');
    if ($(this).hasClass('xy')) {
        $('div.block', this).addClass('xxx').removeClass('zzz').stop(true, true).animate({
            top: '0'
        });
    }
}, function (e) {
    if ($(this).hasClass('xy')) {
        $('div.block', this).stop(true, true).animate({
            top: topHeightVal
        }, 200).addClass('zzz').removeClass('xxx');
    }
});

关于javascript - 在动画期间拒绝 jQuery 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19814266/

相关文章:

javascript - jQuery 在 jQuery ReplaceWith 中进行更改

javascript - jQueryUI 选项卡通过 SlideUp 和 SlideDown 切换

jquery - 在事件菜单链接上添加类

ASP.NET javascript 调试在 2 个项目中不起作用

javascript - 当 jqgrid 编辑使用 web api 获取数据时,无法将数据绑定(bind)到 jqgrid 中的下拉列表

javascript - 在 javascript 数组中维护对 DOM 对象和/或 jQuery 对象的引用是个坏主意吗?

javascript - 动态禁用选择字段条件是如果我们更改另一个选择字段

javascript - 在父 div 调整大小时调整图表大小

JavaScript HTTP GET 请求适用于 Firefox 和 iPad Safari,但不适用于 Mac OS Safari

javascript - Three.js 点击事件后沿路径移动相机