如果您希望能够更改将接收分配的变量名称navSpritePath2x
,那么我认为 LESS 不可能。
但是,如果您只是想将该变量名称抽象为 mixin,那么它是可能的。这样,它可以像这样使用(例如):
较少的
@navSpritePathOne: '/i/data-board/navigation/navigation-sprite1.gif';
@navSpritePathTwo: '/i/data-board/navigation/navigation-sprite2.gif';
.createPath2x(@path){
@navSpritePath2x: `@{path}.replace(/\.[\w\?=]+$/, function(match) { return "@2x" + match; })`;
}
.test {
background-image: url(@navSpritePathOne);
.high-density & {
.createPath2x(@navSpritePathOne);
background-image: url(@navSpritePath2x);
}
}
.test2 {
background-image: url(@navSpritePathTwo);
.high-density & {
.createPath2x(@navSpritePathTwo);
background-image: url(@navSpritePath2x);
}
}
CSS 输出
.test {
background-image: url('/i/data-board/navigation/navigation-sprite1.gif');
}
.high-density .test {
background-image: url("/i/data-board/navigation/navigation-sprite1@2x.gif");
}
.test2 {
background-image: url('/i/data-board/navigation/navigation-sprite2.gif');
}
.high-density .test2 {
background-image: url("/i/data-board/navigation/navigation-sprite2@2x.gif");
}
background-image
当然,如果你总是要用它来设置一组类似的属性(比如,与
少少_
@navSpritePathOne: '/i/data-board/navigation/navigation-sprite1.gif';
@navSpritePathTwo: '/i/data-board/navigation/navigation-sprite2.gif';
.createPath2x(@path){
background-image: url(@path);
.high-density & {
background-image: `@{path}.replace(/\.[\w\?=]+$/, function(match) { return "@2x" + match; })`;
}
}
.test {
.createPath2x(@navSpritePathOne);
}
.test2 {
.createPath2x(@navSpritePathTwo);
}
CSS 输出与上面显示的完全相同,但 LESS 代码较长。