[英]Animating a div back and forth
I'm not an expert dev. 我不是专家。 I was looking for a little help.
我正在寻找一点帮助。
I was trying to make a div move back and forth using toggleClass; 我试图使用toggleClass来回移动div;
$('div').on('click', function() {
$(this).toggleClass('active');
});
and keyframes; 和关键帧;
.active {
animation-name: moveme;
animation-duration: 1s;
animation-timing-function: linear;
animation-fill-mode: forwards;
}
@keyframes moveme {
0% {transform: translate(0,0)}
50% {transform: translate(-200px, 0px)}
100% {transform: translate(-200px,-50px);}
}
Now, when the div goes back to its previous position, it doesn't animate. 现在,当div返回其先前的位置时,它不会生成动画。 I want the div to animate with the same animation but backwards.
我希望div使用相同的动画制作动画,但是向后。 How can I do that?
我怎样才能做到这一点?
Here you can see what I'm trying to accomplish jsfiddle.net/jmrMW/43/ I just made a simple animation for the sake of the example, but it's going to be more complex in the future. 在这里你可以看到我正在努力实现的目标jsfiddle.net/jmrMW/43/我为了这个例子而做了一个简单的动画,但是将来它会变得更复杂。
Thank you in advance ^^ 提前谢谢^^
Here's a fork of your JSFiddle. 这是你的JSFiddle的一个分支。 Pretty much just duplicated what you already had for keyframes but applied it in reverse :)
几乎只是重复你已经拥有的关键帧,但反过来应用它:)
$('div').on('click', function() { if ( $(this).hasClass("go-up") ) { $(this).removeClass("go-up").addClass("go-down"); } else { $(this).addClass("go-up").removeClass("go-down"); } });
div{ background:#aaa; width: 200px; margin: 20vh auto; height: 200px; } .go-up { animation-name: moveup; animation-duration: 1s; animation-timing-function: linear; animation-delay: 0s; animation-fill-mode: forwards; } .go-down { animation-name: movedown; animation-duration: 1s; animation-timing-function: linear; animation-delay: 0s; animation-fill-mode: forwards; } @keyframes moveup { 0% {transform: translate(0,0)} 50% {transform: translate(-100px, 0px)} 100% {transform: translate(-100px,-50px);} } @keyframes movedown { 0% {transform: translate(-100px,-50px);} 50% {transform: translate(-100px, 0px)} 100% {transform: translate(0,0)} }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div></div>
Javascript adds and removes classes instantly. Javascript立即添加和删除类。 And your CSS binds the animation only to
.active
class, so when you click second time - you loose your class - you loose the slight animation. 而你的CSS只将动画绑定到
.active
类,所以当你第二次点击时 - 你松开了你的课程 - 你就会失去轻微的动画。
Now, you can duplicate everything as people mentioned before me, but come on! 现在,您可以复制人们在我之前提到的所有内容,但请继续! Programmers never duplicate, they utilize.
程序员从不复制,他们使用。 Isn't it a good idea to discover all the power of CSS3 animations?
发现CSS3动画的所有功能不是一个好主意吗?
So, I'd like to suggest another approach: to set animation-direction: alternate
and animation-play-state: paused
. 所以,我想建议另一种方法:设置
animation-direction: alternate
和animation-play-state: paused
。 Then define CSS class for moving, either way: 然后定义用于移动的CSS类,无论哪种方式:
.moving {
animation-play-state: running;
}
And then, apply it to the given element for only 1 sec after it has been clicked: 然后,在单击后将其应用于给定元素仅1秒:
var $moveable = $( '.moveable' ).eq( 0 );
$moveable.click(function() {
$moveable.addClass( 'moving' );
setTimeout(function() {
$moveable.removeClass( 'moving' );
}, 1000);
});
Update 更新
This solution has a major downside - using timers in both JS and CSS which obviously won't match. 这个解决方案有一个主要的缺点 - 在JS和CSS中使用计时器显然是不匹配的。 Animation is now infinite, so each time it goes little bit further than should.
动画现在是无限的,所以每次动画都比应该的更进一步。 But we can increment
animation-iteration-count
to disable animation proceeding to the next cycle. 但我们可以增加
animation-iteration-count
来禁用动画进入下一个循环。 Hope that's still fun! 希望这仍然很有趣!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.