LESS 中停止次数可变的线性梯度? (或萨斯)

发布于 2024-12-11 10:50:32 字数 1265 浏览 0 评论 0原文

这是我想用 Less 抽象的 CSS。在这种情况下,有 4 个站点。但我还有另外一堂课,有 10 站。如何使用可变数量的参数?

我在文档中看到 @arguments ,但正如您所注意到的,语法有所不同:一些规则使用连续的所有参数,其他规则将它们成对分组:color-stop(x% ,#y)

如果您知道 Sass 中的解决方案,建议它,我可以切换到它。

.action {
    background: -moz-linear-gradient(top, #6db3f2 0%, #54a3ee 50%, #3690f0 51%, #1e69de 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6db3f2), color-stop(50%,#54a3ee), color-stop(51%,#3690f0), color-stop(100%,#1e69de)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #6db3f2 0%,#54a3ee 50%,#3690f0 51%,#1e69de 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #6db3f2 0%,#54a3ee 50%,#3690f0 51%,#1e69de 100%); /* Opera11.10+ */
    background: -ms-linear-gradient(top, #6db3f2 0%,#54a3ee 50%,#3690f0 51%,#1e69de 100%); /* IE10+ */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6db3f2', endColorstr='#1e69de',GradientType=0 ); /* IE6-9 */
    background: linear-gradient(top, #6db3f2 0%,#54a3ee 50%,#3690f0 51%,#1e69de 100%); /* W3C */background: linear-gradient(top, #1e5799 0%,#2989d8 14%,#207cca 84%,#7db9e8 100%); /* W3C */
}

Here's a CSS that I want to abstract with Less. In this case, there are 4 stops. But I have another class with 10 stops. How can I use variable number of arguments?

I see @arguments in the docs, but as you can notice, the syntax differs: some rules use all the arguments in a row, others group them in pairs: color-stop(x%, #y).

If you know a solution in Sass, suggest it, I can switch to it.

.action {
    background: -moz-linear-gradient(top, #6db3f2 0%, #54a3ee 50%, #3690f0 51%, #1e69de 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6db3f2), color-stop(50%,#54a3ee), color-stop(51%,#3690f0), color-stop(100%,#1e69de)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #6db3f2 0%,#54a3ee 50%,#3690f0 51%,#1e69de 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #6db3f2 0%,#54a3ee 50%,#3690f0 51%,#1e69de 100%); /* Opera11.10+ */
    background: -ms-linear-gradient(top, #6db3f2 0%,#54a3ee 50%,#3690f0 51%,#1e69de 100%); /* IE10+ */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6db3f2', endColorstr='#1e69de',GradientType=0 ); /* IE6-9 */
    background: linear-gradient(top, #6db3f2 0%,#54a3ee 50%,#3690f0 51%,#1e69de 100%); /* W3C */background: linear-gradient(top, #1e5799 0%,#2989d8 14%,#207cca 84%,#7db9e8 100%); /* W3C */
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

纸伞微斜 2024-12-18 10:50:32

我自己的解决方案,使用修改后的文件 bourbon 项目:

_linear-gradient.scss

@mixin linear-gradient($pos, $G1, $G2: false,
                        $G3: false, $G4: false,
                        $G5: false, $G6: false,
                        $G7: false, $G8: false,
                        $G9: false, $G10: false) {
    // Detect what type of value exists in $pos
    $pos-type: type-of(nth($pos, 1));

    // If $pos is missing from mixin, reassign vars and add default position
    @if ($pos-type == color) or (nth($pos, 1) == "transparent")    {
        $G10: $G9; $G9: $G8; $G8: $G7; $G7: $G6; $G6: $G5;
         $G5: $G4; $G4: $G3; $G3: $G2; $G2: $G1; $G1: $pos;
        $pos: top; // Default position
    }

    $usual:($G1);
    $webkit: color-stop($G1);
    @each $g in $G2, $G3, $G4, $G5, $G6, $G7, $G8, $G9, $G10 {
        @if $g != false {
            $usual: $usual + ',' + $g;
            $webkit: $webkit + ',' + color-stop($g);
        }
    }
    $usual: unquote($usual);
    $webkit: unquote($webkit);

    background-color: nth($G1, 1);
    background: deprecated-webkit-gradient(linear, $usual); // Safari <= 5.0
    background: -webkit-gradient(linear, $pos, $webkit); // Safari 5.1+, Chrome
    background: -webkit-linear-gradient($pos, $usual); // Safari 5.1+, Chrome
    background: -moz-linear-gradient($pos, $usual);
    background: -ms-linear-gradient($pos, $usual);
    background: -o-linear-gradient($pos, $usual);
    background: linear-gradient($pos, $usual);
}

用法(screen.sass):

@import linear-gradient
button.action
    +linear-gradient(top, #6db3f2 0%, #54a3ee 50%, #3690f0 51%, #1e69de 100%)

我如何使用 django-compressor

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#   'django.contrib.staticfiles.finders.DefaultStorageFinder',
    'compressor.finders.CompressorFinder',
)

INSTALLED_APPS = (
    ...
    'compressor',
)

COMPRESS = True
COMPRESS_PRECOMPILERS = (
    ('text/sass', 'sass {infile} {outfile}'),
)
COMPILER_FORMATS = {
    '.sass': {
        'binary_path': 'sass',
        'arguments': '*.sass *.css'},
}

模板:

{% load compress %}
{% compress css %}
<link href="{{ STATIC_URL }}css/screen.sass" rel="stylesheet" type="text/sass" media="screen,projection"/>
{% endcompress %}

My own solution, using a modified file from bourbon project:

_linear-gradient.scss

@mixin linear-gradient($pos, $G1, $G2: false,
                        $G3: false, $G4: false,
                        $G5: false, $G6: false,
                        $G7: false, $G8: false,
                        $G9: false, $G10: false) {
    // Detect what type of value exists in $pos
    $pos-type: type-of(nth($pos, 1));

    // If $pos is missing from mixin, reassign vars and add default position
    @if ($pos-type == color) or (nth($pos, 1) == "transparent")    {
        $G10: $G9; $G9: $G8; $G8: $G7; $G7: $G6; $G6: $G5;
         $G5: $G4; $G4: $G3; $G3: $G2; $G2: $G1; $G1: $pos;
        $pos: top; // Default position
    }

    $usual:($G1);
    $webkit: color-stop($G1);
    @each $g in $G2, $G3, $G4, $G5, $G6, $G7, $G8, $G9, $G10 {
        @if $g != false {
            $usual: $usual + ',' + $g;
            $webkit: $webkit + ',' + color-stop($g);
        }
    }
    $usual: unquote($usual);
    $webkit: unquote($webkit);

    background-color: nth($G1, 1);
    background: deprecated-webkit-gradient(linear, $usual); // Safari <= 5.0
    background: -webkit-gradient(linear, $pos, $webkit); // Safari 5.1+, Chrome
    background: -webkit-linear-gradient($pos, $usual); // Safari 5.1+, Chrome
    background: -moz-linear-gradient($pos, $usual);
    background: -ms-linear-gradient($pos, $usual);
    background: -o-linear-gradient($pos, $usual);
    background: linear-gradient($pos, $usual);
}

usage (screen.sass):

@import linear-gradient
button.action
    +linear-gradient(top, #6db3f2 0%, #54a3ee 50%, #3690f0 51%, #1e69de 100%)

How I configured Sass with django-compressor:

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#   'django.contrib.staticfiles.finders.DefaultStorageFinder',
    'compressor.finders.CompressorFinder',
)

INSTALLED_APPS = (
    ...
    'compressor',
)

COMPRESS = True
COMPRESS_PRECOMPILERS = (
    ('text/sass', 'sass {infile} {outfile}'),
)
COMPILER_FORMATS = {
    '.sass': {
        'binary_path': 'sass',
        'arguments': '*.sass *.css'},
}

a template:

{% load compress %}
{% compress css %}
<link href="{{ STATIC_URL }}css/screen.sass" rel="stylesheet" type="text/sass" media="screen,projection"/>
{% endcompress %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文