简体   繁体   English

CSS中过渡属性的动态值

[英]Dynamic value for a transitioned property in CSS

I searched for a way to manage the transition CSS programmatically at runtime. 我寻找了一种在运行时以编程方式管理transition CSS的方法。 Therefore I need to change the top value in the transition relatively at to the behavior of others components. 因此,我需要相对于其他组件的行为来更改transitiontop值。 Is it possible or I am dreaming? 有可能还是我在做梦?

Yes, the transition setup (that is specification of the properties whose change of value should be transitioned) can be done in CSS and we can still dynamically modify the value for those properties. 是的,可以在CSS中完成transition设置(即应更改其值的更改的属性的规范),我们仍然可以动态修改这些属性的值。

All that is needed is a bit of JavaScript to assign the modified value to the element through inline style attributes. 所需要的只是一点点JavaScript,以通过内联样式属性将修改后的值分配给元素。 The transition which is set on the element will apply irrespective of whether the value is changed through a CSS selector or through inline styles. 无论是通过CSS选择器还是通过内联样式更改值,都将应用在元素上设置的transition

 /* transition after some time so that the effect is visible */ window.setTimeout(function() { document.getElementsByTagName('div')[0].style.top = '100px'; /* set the top value dynamically */ }, 500); 
 div { position: absolute; top: 50px; transition: all 1s; } 
 <div>Something</div> 

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

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