javascript - jQuery 动画在一段时间后中断

标签 javascript jquery html css

当我留在我的 div 中时,如果我出去并再次进入,动画将不会再次运行......所以就像当我退出并再次返回时,当我留在现场而动画正在播放时,它不会重新开始动画..

HTML:

<div class="ani-bounce-slow" style="height: 200px; width: 50%; background-color: #3c3; ">
    <p> This is a bouncing Text!</p>
</div>

CSS:

.bounce-slow {
animation: bounce 3s;
}

@keyframes bounce {
    0%,
    20%,
    50%,
    80%,
    100% {
        transform: translateY(0);
    }
    40% {
        transform: translateY(-30px);
    }
    60% {
        transform: translateY(-15px);
    }
}

jQuery:

$(document).ready(function () {
$('.ani-bounce-slow').mouseenter(function () {
    $(this).addClass('bounce-slow');
});
$('.ani-bounce-slow').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
    function (e) {
        $(this).removeClass('bounce-slow');
    }
);
});

Fiddle here

最佳答案

我认为你的问题是你正在使用 one()函数而不是 on() :

改变:

$('.ani-bounce-slow').one('webkitAnimationEnd...

收件人:

$('.ani-bounce-slow').on('webkitAnimationEnd...

.one( events [, data ], handler )
Attach a handler to an event for the elements. The handler is executed at most once per element per event type.

.on( events [, selector ] [, data ], handler )
Attach an event handler function for one or more events to the selected elements.

关于javascript - jQuery 动画在一段时间后中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33399087/

相关文章:

html - 简单形式 : disable form without adding disabled: true or readonly: true to every input

html - 我应该让网页响应到哪个设备宽度大小?

javascript - 如何在纯 JavaScript 中以最快的方式检查字符串的最后一个字符是否是数字/数字?

javascript - 异步加载 json 作为对象属性,然后在事件中访问它

jquery - HTML 文本区域值正在重置

javascript - 如何在 ASP.NET 中动态地将数据附加到 JQuery 控制变量?

html - Bootstrap,如何使 Logo 适应导航栏大小?

javascript - 如何在正则表达式中使用前瞻来匹配无字符?

javascript - 主干集合长度为0

javascript - 使用 Ebay API GetSellerList 抓取大图像