2013-01-23 74 views
0

我正在制作一个动画,使用CSS进行设计,并使用JQUERY完成动作。 这工作正常,现在如果用户再次点击按钮,那么这个动画应该有它以前的位置。要将该动画复制回原来的位置

这里是我的代码 -

<!DOCTYPE html> 
<html> 
<head> 
<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script> 

$(document).ready(function(){ 
$(".t").click(function(){ 
$("p").animate({left:'350px',top:'350px'},1000); 
$("p").animate({left:'600px'},1000); 
$("p").animate({top:'348px',opacity:'0.8'},500); 

});}); 
$(document).ready(function(){ 
$(".t").click(function(){ 

$(".u").animate({right:'350px',top:'260px'},1000); 
$(".u").animate({left:'500px'},500); 
});}); 
$(document).ready(function(){ 
$(".t").click(function(){ 
$("#q").animate({top:'250px',opacity:'0.4'},1000); 
$("#q").animate({left:'500px'},500); 
$("#q").animate({top:'460px'},500); 
$("#q").animate({left:'500'},500); 

}); 
}); 
$(document).ready(function(){ 
$(".t").click(function(){ 
$("#s").animate({top:'350px',left:'400px'},500); 
$("#s").animate({top:'360px'},500); 
}); 

}); 
$(document).ready(function(){ 
$(".t").click(function(){ 
$("#c").animate({top:'360px',left:'500px',opacity:'0.5'},500); 
}); 
}); 


</script> 
</head> 
<body> 


<input type="submit" style="width:80px;font-size:2em;color:white;height:80px;box-shadow:0px -0px 5px skyblue; border-radius:100%;font-weight:bold;font-family:calibri;text-shadow: -1px -1px 0px #111111;background-color:#98bf22;cursor:pointer;" value="Press" class="t"></input> 

<span id="s" style="background:red;width:100px;height:100px;position:absolute;box-shadow:0px 0px 5px 1px white;top:22%;left:20%;border-top-left-radius:100%;border-bottom-left-radius:100%;"></span> 
<p style="background:#777641;border-top-right-radius:100%;border-bottom-right-radius:100%;width:100px;height:100px;position:absolute;top:21.3%;left:60%;box-shadow:0px 0px 5px 2px white;"></p> 
<span class="u" style="background:#2198bf;border-top-right-radius:100%;border-top-left-radius:100%;width:100px;height:100px;position:absolute;top:14%;left:40%;box-shadow:0px 0px 5px 3px white;"></span> 
<span id="q" style="background:blue;width:100px;border-bottom-left-radius:100%;border-bottom-right-radius:100%;height:100px;position:absolute;top:30%;left:40%;box-shadow:0px 0px 5px 2px white;"></span> 
<span id="c" style="background:yellow;width:100px;height:100px;position:absolute;top:22%;left:40%;box-shadow:0px 0px 5px 1px white;"></span> 

</body> 
</html> 

这里是fiddle- http://jsfiddle.net/stackmanoz/vbkBQ/9/

+0

请参阅http://jsfiddle.net/bstakes/hzFTg/#base小提琴。希望它可以帮助 –

+1

@ManojKumar,你将不得不更加具体:你是否希望按钮从一开始就以完全相同的方式重复完全相同的动画?每次按下时,你是否希望它回到动画帧?你想让它倒退吗? – Norguard

+0

@Norguard,我想要动作切换,当动画完成时,你再次点击按钮,你会得到相同的。是的,我希望它重复动画,每次我点击按钮 – Manoj

回答

0

这个脚本做什么你期待

$(document).ready(function(){ 
$(".t").click(function(){ 
    if($("p").css("left") == '60%'){ //check if p exist in the default position 
     $("p").animate({left:'350px',top:'350px'},1000); 
     $("p").animate({left:'600px'},1000); 
     $("p").animate({top:'348px',opacity:'0.8'},500); 
    }else{ //if p is animated and move to new position reset is position 
     alert("here"); 
     $("p").css('top','21.3%'); 
     $("p").css('left','60%'); 
    } 
});}); 

该脚本检查P元素是动画和移动到一个新的位置,如果移动它重置它,否则它动画eleme NT。这个类似的检查也可以为您的小提琴中的其他元素完成。

+0

我不想给其他职位。我想要的是如果动画完成,那么它应该自动移动到第二次点击之前的状态。有什么办法吗? – Manoj

+0

@usman在您的quesion发布的小提琴[链接](http://jsfiddle.net/bstakes/hzFTg/#base)就是这么做的。 – Mehavel

相关问题