用 LESS 减少精灵 CSS?
产生 CSS 精灵效果所需的代码重复似乎是使用 LESS 的完美案例。
但除了标准化基本、悬停和活动增量(如下)之外,我看不到任何进一步的方法来减少所需的代码行数。
任何人都可以建议进一步的改进:
@sprite-base:0px;
@sprite-hover:20px;
@sprite-active:40px;
.zone-user .region-user-second ul.text-size li.one a {
background: url("../img/sprite-accessibility.gif") no-repeat scroll 0px @sprite-base transparent;
}
.zone-user .region-user-second ul.text-size li.two a {
background: url("../img/sprite-accessibility.gif") no-repeat scroll -25px @sprite-base transparent;
}
etc ...
.zone-user .region-user-second ul.text-size li.one a:hover {
background: url("../img/sprite-accessibility.gif") no-repeat scroll 0px @sprite-base - @sprite-hover transparent;
}
.zone-user .region-user-second ul.text-size li.two a:hover {
background: url("../img/sprite-accessibility.gif") no-repeat scroll -25px @sprite-base - @sprite-hover transparent;
}
.zone-user .region-user-second ul.text-size li.one a.active {
background: url("../img/sprite-accessibility.gif") no-repeat scroll 0px @sprite-base - @sprite-active transparent;
}
.zone-user .region-user-second ul.text-size li.two a.active {
background: url("../img/sprite-accessibility.gif") no-repeat scroll -25px @sprite-base - @sprite-active transparent;
}
etc ...
The repition of code needed to produce CSS sprite effects seems to be a perfect case for the use of LESS.
But other than standardising the base ,hover and active increments (below) I can't see any further way to reduce the lines of code needed.
Can anyone suggest futher improvements:
@sprite-base:0px;
@sprite-hover:20px;
@sprite-active:40px;
.zone-user .region-user-second ul.text-size li.one a {
background: url("../img/sprite-accessibility.gif") no-repeat scroll 0px @sprite-base transparent;
}
.zone-user .region-user-second ul.text-size li.two a {
background: url("../img/sprite-accessibility.gif") no-repeat scroll -25px @sprite-base transparent;
}
etc ...
.zone-user .region-user-second ul.text-size li.one a:hover {
background: url("../img/sprite-accessibility.gif") no-repeat scroll 0px @sprite-base - @sprite-hover transparent;
}
.zone-user .region-user-second ul.text-size li.two a:hover {
background: url("../img/sprite-accessibility.gif") no-repeat scroll -25px @sprite-base - @sprite-hover transparent;
}
.zone-user .region-user-second ul.text-size li.one a.active {
background: url("../img/sprite-accessibility.gif") no-repeat scroll 0px @sprite-base - @sprite-active transparent;
}
.zone-user .region-user-second ul.text-size li.two a.active {
background: url("../img/sprite-accessibility.gif") no-repeat scroll -25px @sprite-base - @sprite-active transparent;
}
etc ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您未能利用 LESS 的最大功能之一,即嵌套规则。它可以更容易地识别正在发生的事情,并且在我看来它更容易阅读。而且您编写的代码也确实少得多。以下是我如何嵌套您在问题中显示的规则:
You are failing to take advantage of one of the greatest features of LESS which is nesting your rules. It makes it easier to identify what is going on and in my opinion it is easier to read. Also you do write considerably less code. Here is how I would nest the rules you have shown in your question:
嗯,这就是我要做的。 (没有 LESS ..or More.. )
工作示例: http://jsfiddle.net/lollero/gE7df/< /a>
CSS:
HTML:
编辑:我想我脑子有屁什么的..我修复了我的 css 示例中的一个巨大错误。
Well this is what i would do. ( without LESS ..or More.. )
Working example: http://jsfiddle.net/lollero/gE7df/
CSS:
HTML:
Edit: I think I had a brain fart or something.. I fixed a huge error in my css example.