简体   繁体   English

JavaScript animation 延迟

[英]JavaScript animation delay

I have animation by JavaScript and I want to add some sort of delay after it and before it execute second part of the code.我有 JavaScript 的 animation ,我想在它之后和执行代码的第二部分之前添加某种延迟。 similar to Ajax success / done functionality类似于 Ajax 成功/完成功能

Code代码

$('#startButton').on('click', function() {
    // run animation on click
    document.getElementById("rightHand").animate([
        { transform: 'translateY(80px)' }, 
        { transform: 'translateY(0px)' }
    ], {
        duration: 100
    });
    //run second part
    $('#portfolio').show();
    $('.footer').show();
    var hash = $('#portfolioSection');
    $('html, body').animate({
        scrollTop: $(hash).offset().top
    }, 800);
});

Note Second part of the code waits till animation finish and execute after let say 300ms.注意代码的第二部分等到 animation 完成并在 300 毫秒后执行。 So it gives users time to enjoy the animation instead of immediately jump to next section.因此,它让用户有时间享受 animation,而不是立即跳到下一节。

Any idea?任何想法?

Put your 2nd part into a separate function and then use settimeout (you may adjust the time by yourself)将您的第二部分放入单独的 function 中,然后使用 settimeout (您可以自己调整时间)

$('#startButton').on('click', function() {
    // run animation on click
    document.getElementById("rightHand").animate([
        { transform: 'translateY(80px)' }, 
        { transform: 'translateY(0px)' }
    ], {
        duration: 100
    });

    // say after 3 seconds to trigger secondpart

    setTimeout(function(){ secondpart(); }, 3000);
    
    });


function secondpart()
{   
        //run second part
    $('#portfolio').show();
    $('.footer').show();
    var hash = $('#portfolioSection');
    $('html, body').animate({
        scrollTop: $(hash).offset().top
    }, 800);

    }

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

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