jo_*_*ura 16 css arguments mixins less css-animations
有没有办法将一个mixin或style的声明传递给另一个mixin作为输入参数?
我们来看一个动画关键帧的例子.以下是我们如何在纯CSS中定义关键帧:
@-moz-keyframes some-name
{
from { color: red; }
to { color: blue; }
}
@-webkit-keyframes some-name
{
from { color: red; }
to { color: blue; }
}
@keyframes some-name
{
from { color: red; }
to { color: blue; }
}
Run Code Online (Sandbox Code Playgroud)
想法是使用mixins简化这些声明,所以我们可以有如下内容:
.keyframes(name, from, to)
{
// here we need somehow to reproduce structure
// that we have in an example above
}
// define one animation
.my-from() { color: red; }
.my-to() { color: blue; }
// the following won't work because you cannot pass mixin as a parameter
// in way I have here, so I am looking for a way to solve this problem
.keyframes('some-name', .my-from, .my-to);
// define another animation
.another-from() { font-size: 1em; }
.another-to() { font-size: 2em; }
.keyframes('another-name', .another-from, .another-to);
Run Code Online (Sandbox Code Playgroud)
系统将具有可以动态附加到应用程序以及移除的不同模块.所以,不建议我使用,@import因为事实并非如此.使用有关模块及其自己的LESS样式的信息以及诸如mixins库等的基本LESS依赖性,动态地动态编译输出CSS.
注意:如果你知道一种传递类定义而不是mixin的方法,它对我有用.在上面的例子中,它将.my-from代替.my-from()等等.
Sco*_*ttS 24
我们现在可以通过1.7.0更新以及创建规则集的能力以及在设置中@keyframes使用变量来更直接地做到这一点.
现在我们真的可以通过规则集传递一个参数mixin,或者我们可以自己传递属性stings.所以考虑一下:
少(使用1.7)
.keyframes(@name, @from, @to) {
@frames: {
from { @from(); }
to { @to(); }
};
@pre: -moz-keyframes;
@-moz-keyframes @name
{
@frames();
}
@-webkit-keyframes @name
{
@frames();
}
@keyframes @name
{
@frames();
}
}
.keyframes(testName, {color: red; .myMix(0);}, {color: blue; .myMix(1);});
.myMix(@value) {opacity: @value;}
Run Code Online (Sandbox Code Playgroud)
请注意,我传递了属性设置和mixin调用,我的输出是:
CSS输出
@-moz-keyframes testName {
from {
color: red;
opacity: 0;
}
to {
color: blue;
opacity: 1;
}
}
@-webkit-keyframes testName {
from {
color: red;
opacity: 0;
}
to {
color: blue;
opacity: 1;
}
}
@keyframes testName {
from {
color: red;
opacity: 0;
}
to {
color: blue;
opacity: 1;
}
}
Run Code Online (Sandbox Code Playgroud)
注意规则集如何在括号中传递,{...}然后通过@from()和@to()(看起来很像mixin调用)调用.我正在使用这些传递的规则集来设置另一个规则集,@frames然后调用它来填充关键帧定义.
更一般地说
在这里,我将私有mixin传递给另一个mixin,然后从其他mixin调用它:
减
.someMixin(@class; @expectedMixin) {
.@{class} {
@expectedMixin();
.myPrivateMix(0.6);
test: 1;
}
}
.someMixin(newClass; {.myClass;});
.myClass {
.myPrivateMix(@value) {opacity: @value;}
}
Run Code Online (Sandbox Code Playgroud)
CSS输出
.newClass {
opacity: 0.6;
test: 1;
}
Run Code Online (Sandbox Code Playgroud)
保留下面的遗留信息.
哇,这需要做一些,但我想我有一些你可以使用的东西.但是,它确实需要在模块中对mixin进行一些特殊定义,特别是使用模式匹配.所以...
请注意,在特定的未来mixin中使用的模块mixin是如何使用相同的mixin名称定义的,但具有不同的模式名称.这是实现这项工作的关键.
// define one animation in a module
.from(my-from){ color: red; }
.to(my-to) { color: blue; }
// define one animation in another module
.from(another-from){ font-size: 1em; }
.to(another-to) { font-size: 2em; }
Run Code Online (Sandbox Code Playgroud)
如果您还需要模块中的各个mixin名称,您应该能够这样做:
// define one animation in a module
.my-from(){ color: red; }
.my-to() { color: blue; }
.from(my-from){ .my-from() }
.to(my-to) { .my-to() }
// define one animation in another module
.another-from(){ font-size: 1em; }
.another-to() { font-size: 2em; }
.from(another-from){ .another-from() }
.to(another-to) { .another-to() }
Run Code Online (Sandbox Code Playgroud)
这应该允许一个人调用直接mixin,.my-from()或者在后来的mixins中.from()通过模式匹配访问单个mixin组,使其可以可变地访问.
对于你的@keyframes例子,这是非常困难的.实际上,堆栈溢出答案对于帮助我解决应用问题至关重要,@name因为它遵循@keyframes定义,因此不适用于正常的LESS规则.应用@name看起来讨厌的解决方案,但它的工作原理.它确实有一个非常不幸的必要,即定义一个选择器字符串来播放动画(因为它使用该字符串来帮助构建最后一个}关键帧).这种命名限制只适用于以@类似@keyframes和可能开头的css字符串@media.
此外,因为我们的模块文件中使用了标准的mixin名称,所以我们可以在新的mixin中一致地访问它,同时传递变量以通过模式匹配选择该mixin 的正确变体.所以我们得到:
少于1.3.3或以下
// define mixin in mixin file
.keyframes(@selector, @name, @from, @to) {
@newline: `"\n"`; // Newline
.setVendor(@pre, @post, @vendor) {
(~"@{pre}@@{vendor}keyframes @{name} {@{newline}from") {
.from(@from);
}
to {
.to(@to);
}
.Local(){}
.Local() when (@post=1) {
(~"}@{newline}@{selector}") {
-moz-animation: @name;
-webkit-animation: @name;
-o-animation: @name;
-ms-animation: @name;
animation: @name;
}
}
.Local;
}
.setVendor("" , 0, "-moz-");
.setVendor(~"}@{newline}", 0, "-webkit-");
.setVendor(~"}@{newline}", 0, "-o-");
.setVendor(~"}@{newline}", 0, "-ms-");
.setVendor(~"}@{newline}", 1, "");
}
Run Code Online (Sandbox Code Playgroud)
少1.4.0+
.keyframes(@selector, @name, @from, @to) {
@newline: `"\n"`; // Newline
.setVendor(@pre, @post, @vendor) {
@frames: ~"@{pre}@@{vendor}keyframes @{name} {@{newline}from";
@{frames} {
.from(@from);
}
to {
.to(@to);
}
.Local(){}
.Local() when (@post=1) {
@animationSector: ~"}@{newline}@{selector}";
@{animationSector} {
-moz-animation: @name;
-webkit-animation: @name;
-o-animation: @name;
-ms-animation: @name;
animation: @name;
}
}
.Local;
}
.setVendor("" , 0, "-moz-");
.setVendor(~"}@{newline}", 0, "-webkit-");
.setVendor(~"}@{newline}", 0, "-o-");
.setVendor(~"}@{newline}", 0, "-ms-");
.setVendor(~"}@{newline}", 1, "");
}
Run Code Online (Sandbox Code Playgroud)
您可以给它自己的名字,并且只为模块mixins上的模式匹配传递直线模式(所有都没有点[.]和没有引号),但不要忘记您还需要一个选择器字符串(这是引用)让mixin正常工作:
.keyframes('.changeColor', some-name, my-from, my-to);
.keyframes('.changeFontSize', another-name, another-from, another-to);
Run Code Online (Sandbox Code Playgroud)
@-moz-keyframes some-name {
from {
color: red;
}
to {
color: blue;
}
}
@-webkit-keyframes some-name {
from {
color: red;
}
to {
color: blue;
}
}
@-o-keyframes some-name {
from {
color: red;
}
to {
color: blue;
}
}
@-ms-keyframes some-name {
from {
color: red;
}
to {
color: blue;
}
}
@keyframes some-name {
from {
color: red;
}
to {
color: blue;
}
}
.changeColor {
-moz-animation: some-name;
-webkit-animation: some-name;
-o-animation: some-name;
-ms-animation: some-name;
animation: some-name;
}
@-moz-keyframes another-name {
from {
font-size: 1em;
}
to {
font-size: 2em;
}
}
@-webkit-keyframes another-name {
from {
font-size: 1em;
}
to {
font-size: 2em;
}
}
@-o-keyframes another-name {
from {
font-size: 1em;
}
to {
font-size: 2em;
}
}
@-ms-keyframes another-name {
from {
font-size: 1em;
}
to {
font-size: 2em;
}
}
@keyframes another-name {
from {
font-size: 1em;
}
to {
font-size: 2em;
}
}
.changeFontSize {
-moz-animation: another-name
-webkit-animation: another-name;
-o-animation: another-name;
-ms-animation: another-name;
animation: another-name;
}
Run Code Online (Sandbox Code Playgroud)