Add var into css class

允我心安 提交于 2021-01-29 08:34:24

问题


I'm currently writing a component that is making an animation in a loop, the animation time is a props passed to the component.

My problem is the following: The animation is made in CSS:

animate:{
   -webkit-transition: 10.0s !important;
   -moz-transition: 10.0s !important;
   -o-transition: 10.0s !important;
   transition: 10.0s !important;
}

I would want to pass the duration inside this class declaration but it seems impossible and I'm using these CSS tricks to restart the animation: https://css-tricks.com/restart-css-animation/ that is including the usage of a class.

Is there any way to init a class with vars in Vue.js or to make a CSS animation in a loop with duration as a parameter ?


回答1:


css isn't static if you use variables could you do

:root{
   --primary-animation:10s;
}

animate:{
   -webkit-transition: var(--primary-animation) !important;
   -moz-transition: var(--primary-animation) !important;
   -o-transition: var(--primary-animation) !important;
   transition: var(--primary-animation) !important;
}

and on component

created(){
  document.documentElement.style.setProperty('--primary-animation', this.myCustomProperty+"s")

}


来源:https://stackoverflow.com/questions/62910230/add-var-into-css-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!