简体   繁体   English

更好的方法来延迟javascript中的函数?

[英]better way to delay a function in javascript?

I am trying to find out a better way to delay myFunction.As you see i animated this image to fade in and when i click on it i trigger a new animation such as slideleft and at the end of this animaton i call myFunctionContinued delayed about 5 sec until my animaton is completed and link is opened.I dont think is a clever way to do that and i would like your suggestions! 我试图找到一个更好的方法来延迟myFunction。当你看到我动画这个图像淡入,当我点击它我触发一个新的动画,如slideleft和在这个动画的结尾我称为myFunctionContinued延迟约5秒直到我的动画完成并且链接打开。我不认为这是一个聪明的方法,我想你的建议!

All code is here jsfidle 所有代码都在这里jsfidle

HTML just a div with id and class fadein HTML只是一个带有id和class fadein的div

<div id="object4" class="fadeIn">
 <a id="myLink" href="#" onclick="myFunction();return false;"><img class="img4" src="http://imageshack.com/a/img89/1871/deqz.png"></a></div>

CSS and keyframes fadein and slideleft CSS和关键帧fadein和slideleft

<style> 

body{

background-color:black;

width:800px;
}

img.img4{
width: 300px; height: 200px;
transform:translate(550px,150px);

}

/*
==============================================
fadeIn
==============================================
*/

.fadeIn{
    animation-name: fadeIn;
    -webkit-animation-name: fadeIn; 

    animation-duration: 1.5s;   
    -webkit-animation-duration: 1.5s;

    animation-timing-function: ease-in-out; 
    -webkit-animation-timing-function: ease-in-out;     

    visibility: visible !important; 
}

@keyframes fadeIn {
    0% {
        transform: scale(0);
        opacity: 0.0;       
    }
    60% {
        transform: scale(1.1);  
    }
    80% {
        transform: scale(0.9);
        opacity: 1; 
    }   
    100% {
        transform: scale(1);
        opacity: 1; 
    }       
}



/*
==============================================
slideLeft
==============================================
*/


.slideLeft{
    animation-name: slideLeft;
    -webkit-animation-name: slideLeft;  

    animation-fill-mode: forwards;
    animation-duration: 4s; 
    -webkit-animation-duration: 4s;

    animation-timing-function: ease-in-out; 
    -webkit-animation-timing-function: ease-in-out;     

    visibility: visible !important; 
}

@keyframes slideLeft {
    0% {
        transform: translateX(40%);
    }
    90% {
        transform: translateX(-3%);
    }

    100% {
        transform: rotate(-130deg) translate(-430px,60px);
    }
    /*from {transform: translateX(40%);}
    to {transform: translateX(-3%);}*/
}

</style>

Function click on object4 trigger animation slideleft wait 5 sec call continuedfunction and open the link in the same window 功能点击object4触发动画slideleft等待5秒,调用continuefunction并在同一窗口中打开链接

<script>

function myFunction()
{
$('#object4').click(function() {
        $(this).addClass("slideLeft");


    });

setTimeout(myFunctionContinued, 5000);

}

function myFunctionContinued(){

window.open("http://www.w3schools.com",'_self',false);}

</script>

Keyframes style photo isnt the final result of my animation that i want to focus is the way as i said to delay. 关键帧样式照片不是我想要关注的动画的最终结果是我说延迟的方式。

Thanks for your time! 谢谢你的时间!

There's an event which fires when a CSS3 animation completes (from: http://blog.teamtreehouse.com/using-jquery-to-detect-when-css3-animations-and-transitions-end 当CSS3动画完成时会触发一个事件(来自: http//blog.teamtreehouse.com/using-jquery-to-detect-when-css3-animations-and-transitions-end

object4.one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend',   
function(e) {

// code to execute after transition ends

}); });

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

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