Почему мои воздушные шары не летают правильно? CSS анимация перевести

Я пытаюсь сделать следующее с помощью CSS translate:

При нажатии кнопки появляются воздушные шары и разлетаются. Когда они закончат полет, они исчезнут и вернутся в исходное положение (чтобы при следующем нажатии кнопки происходило то же самое).

До сих пор я получал следующее (воздушный шар только взлетает и не появляется / не исчезает).

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() моя анимация вообще не запускается Как может мой воздушный шар летать?

2 ответа

Решение

Вы можете достичь этого эффекта с помощью 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
    });
});

демонстрация

Я сделал кодовую ручку, используя свойство translate внутри анимации. (Мой контейнер имеет высоту 700 пикселей) .

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

Надеюсь, это может помочь!

ДЕМО ЗДЕСЬ

Другие вопросы по тегам