我正在尝试使用 CSS 翻译执行以下操作:
单击按钮时,气球会出现并飞走。当它们完成飞行时,它们会消失并返回到起始位置(以便下次单击按钮时会发生相同的行为)。
到目前为止我得到了following (气球只是向上飞,不会出现/消失)。
HTML
<div>
<img class="object" src="http://pngimg.com/upload/balloon_PNG580.png" />
</div>
<br><br><button id="button">Fly!!!</button>
CSS
div{
height: 300px;
position: relative;
}
.object {
width: 60px;
position: absolute;
bottom: 0;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
}
.move-up{
transform: translate(0,-150px);
-webkit-transform: translate(0,-150px);
-o-transform: translate(0,-150px);
-moz-transform: translate(0,-150px);
}
JS
$('#button').click(function(){
$('.object').addClass('move-up');
});
问题是,当我尝试将起始 display : none
添加到对象并单击添加 $('.object').show()
时,它会出现我的动画根本没有开始。如何让我的气球飞起来?
最佳答案
您可以使用 CSS3 动画来实现此效果。请参阅内嵌评论以了解每个元素的更多详细信息。
CSS:
.object {
width: 60px;
position: absolute;
bottom: 0;
opacity: 0; /* make the balloon invisible on load and once animation is complete */
}
.move-up{
-webkit-animation: balloon 1s linear; /* the animation to move up */
-moz-animation: balloon 1s linear;
animation: balloon 1s linear;
}
@keyframes balloon { /* included only unprefixed version to keep answer short, demo has the prefixed versions also */
5% {
transform: translate(0, 0); /* keep at original position */
opacity: 1; /* make opacity as 1 after a short time for balloon to appear */
}
50% {
transform: translate(0, -150px); /* move up */
opacity: 1; /* retain the opacity */
}
55%, 100% {
transform: translate(0, -150px); /* retain translated position till end */
opacity: 0; /* hide the ballons and make it hidden for the rest of the duration */
}
}
jQuery:
$('#button').click(function(){
$('.object').addClass('move-up'); // adding the move up animation class to make it move
$('.object.move-up').on('animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd',function(){
$('.object').removeClass('move-up'); // removing the animation class on animation end to make it come back to original state
});
});
关于css - 为什么我的气球不能正常飞行? CSS动画翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25074789/