【问题标题】:SCSS combine variables or create new one with mixinSCSS 组合变量或使用 mixin 创建新变量
【发布时间】:2018-08-07 19:45:53
【问题描述】:

这些是一些颜色变量

$orangeTp            :#ec6532;
$orangeBt            :#f58795;
$orangeNv            :#ea6740;

这是我的渐变混合

@mixin gradient-bg($color1,$color2){
    background-image: -webkit-gradient(
        linear,
        left top,
        left bottom,
        from($color1),
        to($color2)
    );
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: top center;
}

我像这样使用它们:

.bg-orange {
    @include gradient-bg($orangeTp,$orangeBt); 
}

这是我尝试使用但失败的新 mixin

@mixin themes($themecolor) {
    @include gradient-bg(#{$themecolor}+Tp,#{$themecolor}+Bt);
    a {
        color: #{$themecolor}+Nv;
        &:hover {
            color: #{$themecolor}+Tp;
        }
    }
}

我尝试完成的是通过使用新变量($themecolor)来创建变量($orangeTp$orangeBt),这样我就可以写:

.bg-orange {
    @include themes('$orange');
}

【问题讨论】:

    标签: variables sass


    【解决方案1】:

    您不能使用插值来创建动态变量,但地图可以帮助您解决问题:

    $colors: (
      orangeTp:#ec6532,
      orangeBt:#f58795,
      orangeNv:#ea6740
    );
    
    @mixin gradient-bg($color1,$color2){
        background-image: -webkit-gradient(
            linear,
            left top,
            left bottom,
            from($color1),
            to($color2)
        );
        background-repeat: no-repeat;
        background-attachment: fixed;
        background-position: top center;
    }
    
    @mixin themes($themecolor) {
        @include gradient-bg(map-get($colors, $themecolor#{Nv}), map-get($colors, $themecolor#{Bt}));
        a {
            color: map-get($colors, $themecolor#{Nv});
            &:hover {
                color: map-get($colors, $themecolor#{Tp});
            }
        }
    }
    
    .bg-orange {
        @include themes(orange);
    }
    

    我为你创建了一个 Sassmeister:https://www.sassmeister.com/gist/e9cf096735ec51b2243a634ae19b9946

    【讨论】:

    • 完美!谢谢@ReSedano
    • 不客气。如果此声明解决了您的问题,请接受它,以便您也可以为其他用户签名。谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2017-03-07
    • 2021-10-24
    • 1970-01-01
    • 2018-09-23
    • 2014-08-03
    相关资源
    最近更新 更多