简体   繁体   English

CSS动画:使用相对于前一个的新位置移动div

[英]CSS animation : move a div with the new position relative to the previous

for better performance, I want replace: 为了更好的性能,我想要替换:

$('#foo').animate({ left: '+=42px' }, 500);

by a transition (or animation) CSS3. 通过过渡(或动画)CSS3。 But how can we do to implement "+=" on left property in CSS? 但是我们如何才能在CSS中的左侧属性上实现“+ =”?

How move a div with the new left position relative to the previous? 如何使用相对于前一个的新左侧位置移动div?

thx. 谢谢。

In vanilla-js you can't use += , but you can get the old value instead: 在vanilla-js中你不能使用+= ,但你可以得到旧值:

 document.getElementById('foo').onclick = function() { this.style.left = parseFloat(getComputedStyle(this).left) + 42 + 'px'; }; 
 #foo { position: relative; left: 0; transition: 2s left; } 
 <div id="foo">Click me multiple times</div> 

You can use the transition for smooth animation. 您可以使用过渡进行平滑动画。 you just put transition settings in the CSS of an element like this, 你只需将转换设置放在这样的元素的CSS中,

#foo {
    -webkit-transition: all 0.5s ease-out;
    -o-transition: all 0.5s ease-out;
    -moz-transition: all 0.5s ease-out;
    transition: all 0.5s ease-out
}

Then do your incrementation of left with script like this. 然后用这样的脚本来增加左边的增量。

$('#foo').css('left', '+=42px');

You can refer to this page . 你可以参考这个页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM