你如何动态调用mixin? 用例可能是生成样式指南: // The mixin which should be called.typography-xs(){ font-family: Arial; font-size: 16px; line-height: 22px;}// The mixin which tries to call typography-xs.typography-demo(@
用例可能是生成样式指南:
// The mixin which should be called .typography-xs(){ font-family: Arial; font-size: 16px; line-height: 22px; } // The mixin which tries to call typography-xs .typography-demo(@typographyName, @mixinName) { @{typographyName} { &:before{content: '@{typographyName}';} // Calling the mixin dynamically @mixinName(); } } // Example call of .typograhpy-demo .typography-demo(xs, typography-xs);
这样一个动态调用是否可以用更少的CSS?
你现在不能动态地按你的意愿打电话.但是,您可以使用 pattern matching稍微改变它,如下所示:// The mixin which should be called .typography(xs){ font-family: Arial; font-size: 16px; line-height: 22px; } // The mixin which tries to call typography-xs .typography-demo(@typographyName) { @{typographyName} { &:before{content: '@{typographyName}';} // Calling the mixin dynamically .typography(@typographyName); } } // Example call of .typograhpy-demo .typography-demo(xs);
使用模式匹配,您可以创建其他模式,例如:
.typography(xl){ font-family: Arial; font-size: 32px; line-height: 44px; } .typography(times){ font-family: 'Times New Roman'; font-size: 16px; line-height: 22px; }
现在你可以调用模式xl或次数并让它生成代码.基本上,在连字符之后使用你想要使用的任何东西(比如你的-xs)来区分你的各种排版组,并删除连字符并使用该名称作为模式来匹配mixins.
另外,我会添加一种在@ {typographyName}之前放置选择器的方法,如下所示:
.typography-demo(@typographyName, @selector: ~".") { @{selector}@{typographyName} { &:before{content: '@{typographyName}';} // Calling the mixin dynamically .typography(@typographyName); } }
这样它默认生成一个类,但可以创建一个id或任何选择器字符串,直到@ {typographyName}.