javascript - 如何在事件执行完毕后立刻停止事件
ringa_lee
ringa_lee 2017-06-14 10:54:31
[JavaScript讨论组]

我有一个鼠标移入移出显示隐藏的事件,但是鼠标如果多次滑过就会执行多次,如何在鼠标移出事件执行完毕后立刻停止事件?

$(".target").on('mouseenter',function() {
    $(this).children('.p1').show(function(){
        $(this).addClass('animated fadeInLeft');
        $(this).removeClass('animated fadeInLeft');
    })
});
$(".target").on('mouseleave',function() {
    $(this).children('.p1').hide(function(){
        $(this).addClass('animated fadeOutLeft');
        $(this).removeClass('animated fadeOutLeft');
    })
});
ringa_lee
ringa_lee

ringa_lee

全部回复(5)
PHP中文网

题主如果要用只执行一次的方法,用.one()就行,但是一般jQuery的动画特效一定要考虑动画队列的问题,建议在执行动画之前加上.stop()方法来停止“进入动画队列但是未完全执行完”的动画

大家讲道理
$(".target").on('mouseenter',function() {
    $(this).children('.p1').show(function(){
        $(this).addClass('animated fadeInLeft');
        $(this).removeClass('animated fadeInLeft');
    })
});
$(".target").on('mouseleave',function() {
    $(this).children('.p1').hide(function(){
        $(this).addClass('animated fadeOutLeft');
        $(this).removeClass('animated fadeOutLeft');
    })
    $(this).unbind();    //加一句这个取消当前dom的所有绑定事件
});
黄舟

on后边加个e,你用.one()这个API它就是执行一次自动删除了。

参考

漂亮男人

你是想说马上中止动画吧,用 stop()

$(this).children('.p1').stop(true, true)
学习ing

$(".target").off('mouseenter').on('mouseenter',function() {

$(this).children('.p1').show(function(){
    $(this).addClass('animated fadeInLeft');
    $(this).removeClass('animated fadeInLeft');
})

});
$(".target").off('mouseenter').on('mouseleave',function() {

$(this).children('.p1').hide(function(){
    $(this).addClass('animated fadeOutLeft');
    $(this).removeClass('animated fadeOutLeft');
})

});

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号