css - 如何通过更少修复空格或引号来创建随机关键帧名称

标签 css animation less css-animations

我为我的工作写了一个less包。

像这样的代码:

.goTop(@time:1s,@position:-100%){
	@random_suffix:`Math.round(Math.random() * 1.0e8)`;
	@keyframes @random_suffix{
		from{transform:translateY(0);}
		to{transform:translateY(@position);}
	}
	animation-name:@random_suffix;
	animation-duration:@time;
	animation-timing-function:ease-out;
	animation-fill-mode:forwards;
}

然后我可以在我需要的地方使用它。

像这样:

.testbox{
  .goTop(1s);
 }

为什么要使用随机数?

因为,如果所有的关键帧都是同名的,最后的部分会覆盖旧的部分。(参数不一样)

现在,关键帧名称是一个随机数。

但当关键帧名称为数字时,动画将不会运行。

所以我希望这个名字像goTop_12345678

我尝试联系变量名的字符串。

@temp1:goTop_;
@temp2:`Math.round(Math.random() * 1.0e8)`;
@test:@temp1 + @temp2;
// Error
//{"type":"Syntax","message":"Cannot read property 'numerator' of undefined","filename":"input","index":94,"line":4,"callLine":null,"column":0,"extract":["@temp2:goTop_;","@test:@temp1 + @temp2;",""]}

@temp1:'goTop_';
@temp2:`Math.round(Math.random() * 1.0e8)`;
@test:`@{temp1} + @{temp2}`;
//"goTop_12345678"
//It contains the symbol quote "",and the animation will not run.

@temp1:goTop_;
@temp2:@temp1+`Math.round(Math.random() * 1.0e8)`;
@test:@temp2;
//"goTop_ 12345678"
//It contains the space ,and the animation will not run too.

//For the second and third way
//I have try to use replace way to solve it,but I failed.

所以,...

如何解决这个问题...

非常感谢。

@云头

最佳答案

感谢@seven-phases-max。

var 的正确写法是这样的:

@random: `Math.round(Math.random() * 1.0e8)`;
@name: ~"go-top-@{random}"; // <- here we go

//or

@name:~"go-top-`Math.round(Math.random() * 1.0e8)`";

但是新的问题是随机数不一样...

.test {
  background-color: #f39;
  width: 200px;
  height: 200px;
  margin-top: 400px;
  -webkit-animation-name: goTop_70024767;
          animation-name: goTop_70024767;
  -webkit-animation-duration: 1s;
          animation-duration: 1s;
  -webkit-animation-timing-function: ease-out;
          animation-timing-function: ease-out;
  -webkit-animation-fill-mode: forwards;
          animation-fill-mode: forwards;
}
@-webkit-keyframes goTop_50030730 {
  from {
    -webkit-transform: translateY(0);
            transform: translateY(0);
  }
  to {
    -webkit-transform: translateY(-100%);
            transform: translateY(-100%);
  }
}
@keyframes goTop_50030730 {
  from {
    -webkit-transform: translateY(0);
            transform: translateY(0);
  }
  to {
    -webkit-transform: translateY(-100%);
            transform: translateY(-100%);
  }
}

关于css - 如何通过更少修复空格或引号来创建随机关键帧名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43258855/

相关文章:

javascript - 打印时扩展div的高度

css - 在 Mozilla 中动画前变换

grails lesscss-resources 插件问题

jquery - 如何使用 jQuery 为元素添加动画效果?

java - 使用多线程更新GUI

css - 导入 Yeoman/Angularjs 应用程序

less - 有条件地根据另一个变量设置一个变量的值

css - 设置\放置一个 DIV(CSS 层)以表格单元格作为引用

css - Roboto 压缩字体无法在 mozilla firefox 中正确呈现

css - Stylus - 在循环中创建函数?