用 LESS 减少精灵 CSS?

发布于 2024-12-08 14:25:45 字数 1280 浏览 1 评论 0原文

产生 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城凉梦凉人心 2024-12-15 14:25:45

您未能利用 LESS 的最大功能之一,即嵌套规则。它可以更容易地识别正在发生的事情,并且在我看来它更容易阅读。而且您编写的代码也确实少得多。以下是我如何嵌套您在问题中显示的规则:

@sprite-base:0px;
@sprite-hover:20px;
@sprite-active:40px;


.zone-user .region-user-second ul.text-size li a {
    background: url("../img/sprite-accessibility.gif") no-repeat scroll transparent;

    &.one {
        background-position: 0px @sprite-base;

        &:hover {
            background-position:0px (@sprite-base - @sprite-hover);
        }

        &.active {
            background-position:0px (@sprite-base - @sprite-active);
        }
    }
    &.two {
        background-position:-25px @sprite-base;

        &:hover {
            background-position:-25px (@sprite-base - @sprite-hover);
        }

        &.active {
            background-position:-25px (@sprite-base - @sprite-active);
        }
    }

}

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:

@sprite-base:0px;
@sprite-hover:20px;
@sprite-active:40px;


.zone-user .region-user-second ul.text-size li a {
    background: url("../img/sprite-accessibility.gif") no-repeat scroll transparent;

    &.one {
        background-position: 0px @sprite-base;

        &:hover {
            background-position:0px (@sprite-base - @sprite-hover);
        }

        &.active {
            background-position:0px (@sprite-base - @sprite-active);
        }
    }
    &.two {
        background-position:-25px @sprite-base;

        &:hover {
            background-position:-25px (@sprite-base - @sprite-hover);
        }

        &.active {
            background-position:-25px (@sprite-base - @sprite-active);
        }
    }

}

嗯,这就是我要做的。 (没有 LESS ..or More.. )

工作示例: http://jsfiddle.net/lollero/gE7df/< /a>

CSS:

.Element {
    background-image: url('path/to/img.jpg');
    background-repeat: no-repeat;
}

.E-1 { background-position: 0px 0px; }
.E-1:hover { background-position: -20px 0px; }

.E-2 { background-position: 0px -25px; }
.E-2:hover { background-position: -20px -25px; }

.E-3 { background-position: 0px -55px; }
.E-3:hover { background-position: -20px -55px; }

HTML:

<div class="Element E-1"></div>
<div class="Element E-2"></div>
<div class="Element E-3"></div>

编辑:我想我脑子有屁什么的..我修复了我的 css 示例中的一个巨大错误。

Well this is what i would do. ( without LESS ..or More.. )

Working example: http://jsfiddle.net/lollero/gE7df/

CSS:

.Element {
    background-image: url('path/to/img.jpg');
    background-repeat: no-repeat;
}

.E-1 { background-position: 0px 0px; }
.E-1:hover { background-position: -20px 0px; }

.E-2 { background-position: 0px -25px; }
.E-2:hover { background-position: -20px -25px; }

.E-3 { background-position: 0px -55px; }
.E-3:hover { background-position: -20px -55px; }

HTML:

<div class="Element E-1"></div>
<div class="Element E-2"></div>
<div class="Element E-3"></div>

Edit: I think I had a brain fart or something.. I fixed a huge error in my css example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文