假设你仍然有希望的主题是一个样式表(而不是多张作为cimmanon在评论中指出),并假设在你使用LESS 1.3。 2+,那么以下代码通过设置需要主题更改的类的循环来减少重复数量。
请注意,这对Codepen不起作用(它引发了错误uncaught throw #
,可能是因为它们运行的是较早版本的LESS),但您可以通过将代码放入LESS's compiler来正确编译。
LESS(基于关闭您Codepen代码为演示添加的主题)
//////////////////////////////////////////////////////
// CONSTANTS
@lightColour: #fff;
@darkColour: #000;
@lightBg: #fff;
@darkBg: #000;
@numberOfThemes: 3; //controls theme loop
//////////////////////////////////////////////////////
// MIXINS
//Theme Definitions by parametric mixin numbers (1), (2), etc.
.themeDefs(1) {
@lightColour: #f00;
@darkColour: #fff;
@lightBg: #f00;
@darkBg: #fff;
}
.themeDefs(2) {
//inverse of 1
@lightColour: #fff;
@darkColour: #f00;
@lightBg: #fff;
@darkBg: #f00;
}
.themeDefs(3) {
@lightColour: #cfc;
@darkColour: #363;
@lightBg: #cfc;
@darkBg: #363;
}
.curvy {
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}
//////////////////////////////////////////////////////
// GENERAL STYLING
* {padding: 0;margin: 0;}
html {text-align: center;}
h2 {padding: 20px 0;}
.box {
.curvy;
color: @lightColour;
background: @darkBg;
display:inline-block; width:10%; padding:20px 5%; margin:0 1% 20px 1%;
}
//////////////////////////////////////////////////////
// THEME BUILDING
.buildThemes(@index) when (@index < @numberOfThemes + 1) {
[email protected]{index} {
.themeDefs(@index);
color: @lightColour;
background: @darkBg;
.box {
color: @darkColour;
background: @lightBg;
}
}
.buildThemes(@index + 1);
}
//stop loop
.buildThemes(@index) {}
//start theme building loop
.buildThemes(1);
CSS输出(只显示为简洁的环形主题CSS)
.theme-1 {
color: #ff0000;
background: #ffffff;
}
.theme-1 .box {
color: #ffffff;
background: #ff0000;
}
.theme-2 {
color: #ffffff;
background: #ff0000;
}
.theme-2 .box {
color: #ff0000;
background: #ffffff;
}
.theme-3 {
color: #ccffcc;
background: #336633;
}
.theme-3 .box {
color: #336633;
background: #ccffcc;
}
是否有你想让你的“主题”都在同一个CSS文件中吗?看起来只要使用每个文件具有一个主题的备用样式表就会变得非常简单。 – cimmanon 2013-03-07 16:09:04
嗨cimmanon。如果我只是使用CSS,我会沿着这条路线走下去。 我只是认为在声明后可能会有一种简单的方法来改变实际的变量值,这意味着我只需要少量几行就可以说出新的变量颜色在“.theme- 2“,然后一切都会改变颜色与这些新的变量值相关联。尽管感谢您的建议。 – user1814828 2013-03-07 17:44:35
我明白你想要什么,但你还没有回答我的问题。为每个文件创建1个主题将是最简单的主题化方法,并允许用户避免下载他们不需要的样式,因为他们永远不会使用除了他们选择的(或默认)主题之外的其他任何东西。如果每个页面都有自己的主题(即主题不可配置),那么使用单独的样式表并不合理。 – cimmanon 2013-03-07 17:52:16