【问题标题】:Why are not my balloons flying properly? Css animation translate为什么我的气球不能正常飞行? Css动画翻译
【发布时间】:2014-08-01 07:11:56
【问题描述】:

我正在尝试使用 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() 时,我的动画根本没有开始。如何让我的气球飞起来?

【问题讨论】:

  • 您在寻找类似于this 的内容吗?请注意,此示例使用动画。
  • @Harry 是的,唯一的问题就是下次不能点击了。
  • 这可以通过删除动画结束时的.move-up 来修复,例如here。如果您对这种方法感到满意,我会将其添加为答案并进行更多说明。
  • @Harry 谢谢。您能否将其发布为答案,以便我承认您的帮助?我也忘了告诉我,在我开始的问题中,我说气球不应该在开始时可见,并且只有在点击发生时才开始可见。

标签: css css-animations translate3d


【解决方案1】:

您可以使用 CSS3 动画来实现此效果。有关每个项目的更多详细信息,请参阅 inline cmets。

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
    });
});

Demo

【讨论】:

    【解决方案2】:

    我在动画中使用 translate 属性制作了一个 codepen。 (我的容器高度为 700px)。

    @keyframes moveballoon { 
      0% {  transform: translate(0px, 700px);}
      100% { transform: translate(0px, -1200px);} 
    }
    
    .animationballoon { 
      animation: moveballoon 2s infinite linear forwards;
    }
    

    使用 Jquery 你可以有类似的东西:

    $('#button').click(function(){
    $('.object').addClass('animationballoon');
    });
    

    希望对您有所帮助!

    DEMO HERE

    【讨论】:

      猜你喜欢
      • 2021-06-11
      • 1970-01-01
      • 2015-03-11
      • 2019-12-02
      • 2021-01-24
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多