简体   繁体   English

jQuery无法正确识别CSS动画结束事件触发的时间

[英]jQuery not correctly recognizing when CSS animation end event triggers

What I have been attempting to implement is a function that will be called to trigger an error animation using the .one(); 我一直试图实现的功能是使用.one();来触发错误动画的函数.one(); function in jQuery. jQuery中的功能。 When I validate a form and it fails, I call the triggerError function in my code, sending it the element that needs to have the animation applied to it. 当我验证表单并且表单失败时,我在代码中调用triggerError函数,向其发送需要对其应用动画的元素。 The .error animation class is correctly applied, but the .one(); .error动画类已正确应用,但.one(); function does not recognize that the animation has completed in order to remove the .error class. 函数无法识别动画已完成以删除.error类。

Here is the Javascript that listens for the click and attempts to apply and remove the animation class: 以下是侦听点击并尝试应用和删除动画类的Javascript:

$('#search').on('click', function(e) {
    if ($(this).val() === "") {
        triggerError($(this).parent());
        return;
    }
});

function triggerError(input) {
    input.addClass('error');

    input.one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function() {
        input.removeClass('error');
    });
}

I've also created a Codepen to illustrate this error on an input. 我还创建了一个Codepen来说明输入中的此错误。

Thanks in advance for any assistance! 在此先感谢您的协助!

You're using animation , not transition - so you should listen to animationend event: 您正在使用animation ,而不是transition因此您应该听animationend事件:

 $('#search').on('click', function(e) { if ($(this).val() === "") { triggerError($(this).parent()); return; } }); function triggerError(input) { console.log('start'); input.addClass('error'); input.one('webkitAnimationEnd animationend', function() { input.removeClass('error'); console.log('stop'); }); } 
 .inline-form { display: inline-flex; margin-bottom: 1rem; border: 1px solid transparent; width: 100%; } .inline-form input { flex-grow: 100; margin: 0; } .inline-form .input-button { height: 2.3125rem; padding: 0 10px; margin: 0; } @keyframes error { from { border: 1px solid red; } to { border: 1px solid transparent; } } .error { animation-name: error; animation-duration: 3s; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="inline-form"> <label for="search-input" class="hidden"></label><input id="search-input" type="text" placeholder="Search"> <button id="search" class="input-button"><i class="fa fa-search" aria-hidden="true"></i></button> </div> 

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

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