简体   繁体   English

如何在 JS 中延迟按钮操作直到转换结束

[英]How To Delay Button Action Until Transition ends in JS

I have a button With a Hover Animation.我有一个带有 Hover Animation 的按钮。 This button redirects to another page.此按钮重定向到另一个页面。 Whenever I click on the Button.每当我点击按钮。 The redirect action Happens immediately Before the transition Even Starts.重定向动作发生在过渡甚至开始之前。 How can I delay the action until the transition ends?如何延迟操作直到过渡结束? Iam Using我在用

window.location.href = ""

To do The Redirect Action做重定向动作

probably the simplest would be to just delay the redirection in time, and adjust delay time to how long your animation takes:可能最简单的方法是及时延迟重定向,并将延迟时间调整为 animation 需要多长时间:

setTimeout(function(){ window.location.href = ""; }, 3000);

I am assuming that you are using CSS transition and you can wait for the animateend callback documented here .我假设您正在使用 CSS 转换,您可以等待此处记录animateend回调。

Perhaps there is an easy way to do this, calculate/figure out how much time is taking to complete the transition and wait that time with a timeout .也许有一种简单的方法可以做到这一点,计算/计算完成转换需要多少时间,并在timeout中等待该时间。

You may try something line this:你可以试试这个:

function btnClicked() {
    setTimeout(
        function() {
            window.location.href = "";
        }, 
        500  // suppose, your transition take 500ms to complete
    );
}

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

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