简体   繁体   English

jQuery动画在一段时间后中断

[英]jQuery animations break after some time

When I stay in my div the animation won't work again if I go out and in again... So like when I stay in the field while the animation is playing when I quit and go back in it again, it doesn't start the animation over again.. 当我停留在div中时,如果我又一次又一次地进入动画,动画将不再起作用。因此,就像当我退出动画并再次返回其中时,当我在动画播放过程中待在现场时一样,它不会重新开始动画。

HTML: HTML:

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

CSS: 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: 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 在这里摆弄

I think your problem is that you are using one() function instead of on() : 我认为您的问题是您使用的是one()函数而不是on()

Change: 更改:

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

To: 至:

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

.one( events [, data ], handler ) .one(事件[,数据],处理程序)
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 ) .on(事件[,选择器] [,数据],处理程序)
Attach an event handler function for one or more events to the selected elements. 一个或多个事件的事件处理函数附加到所选元素。

Looks like you just need to rip out the class to stop the animation on leave, and just add it back on enter. 看起来您只需要退出课程以在休假时停止动画,然后在回车时将其重新添加即可。 This will make the animation happen every time. 这将使动画每次都发生。

$('.ani-bounce-slow').mouseenter(function () {
    $(this).addClass('bounce-slow');
});
$('.ani-bounce-slow').mouseleave(function () {
    $(this).removeClass('bounce-slow');
});

If you want the animation to continue on mouse leave, then you need to check for animation completion, or set a timer for how loong the animation should be. 如果希望动画在鼠标离开时继续播放,则需要检查动画是否完成,或者设置一个计时器确定动画的播放时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM