【问题标题】:How do I perform an 'if' statement within a nested sass map (SCSS)?如何在嵌套的 sass 映射 (SCSS) 中执行“if”语句?
【发布时间】:2015-05-05 06:37:33
【问题描述】:

由于我是 sass (SCSS) 的新手,我的野心目前似乎超出了我的能力。有人可以帮助我完成以下任务。


背景

我们目前有一个具有两种不同文件格式的图标系统;一个是PNG,另一个是SVG。我们的一些图标有两种文件类型,一些有 PNG 或 SVG。我希望能够单独列出它们。

这是我的 SCSS 变量映射。

$image-path: "http://www.mywebsite.com/assets/img/icons";
$icons-map: (
    tick: (
        filename:       icons_tick,
        has-png:            true,
        has-svg:            false,
    ),          
    secure: (
        filename:       icons_secure,
        has-png:            true,
        has-svg:            true,
    ),
    checkout: (
        filename:       icons_checkout,
        has-png:            false,
        has-svg:            true,
    ),
);

以及我如何尝试将它们列出...

/* Standard (PNG) Icons */
@each $icon-type, $icon-file in $icons-map {
    @if $has-png == "true" { 
        .icon-#{$icon-type}:after {
            background-image: url('#{$image-path}/png/#{$filename}.png');
        }
    }
}

/* Advanced SVG icons (progressive enhancement for newer mobiles, retina-style displays, etc) */
@media only screen and (-webkit-min-device-pixel-ratio: 2),
only screen and (     -o-min-device-pixel-ratio: 2/1),
only screen and (        min-device-pixel-ratio: 2),
only screen and (                min-resolution: 192dpi),
only screen and (                min-resolution: 2dppx) {
    @each $icon-type, $icon-file in $icons-map {
        @if $has-svg == "true" { 
            .icon.#{$icon-type}:after {
                background-image: url('#{$image-path}/svg/#{$filename}.svg');
            }
        }
    }
}

唯一的问题是它似乎没有计划......有什么想法我会出错吗?我认为这是我在嵌套地图上循环的方式,或者是我设置“if”语句的方式。

我会预计输出是这样的;

/* Standard (PNG) Icons */
.icon.tick {
      background-image: url('http://www.mywebsite.com/assets/img/icons/png/icons_tick.png');
    }
    .icon.secure {
      background-image: url('http://www.mywebsite.com/assets/img/icons/png/icons_secure.png');
    }
/* Advanced SVG icons (progressive enhancement for newer mobiles, retina-style displays, etc) */
    @media only screen and (-webkit-min-device-pixel-ratio: 2),
        only screen and (     -o-min-device-pixel-ratio: 2/1),
        only screen and (        min-device-pixel-ratio: 2),
        only screen and (                min-resolution: 192dpi),
        only screen and (                min-resolution: 2dppx) {
    .icon.secure {
      background-image: url('http://www.mywebsite.com/assets/img/icons/svg/icons_secure.svg');
    }
    .icon.checkout {
      background-image: url('http://www.mywebsite.com/assets/img/icons/svg/icons_checkout.svg');
    }

}

不断收到 $has-png$has-svg 是未定义变量的错误。

感谢您的帮助。

【问题讨论】:

    标签: css if-statement sass each


    【解决方案1】:

    您应该首先尝试使用map-get 获取地图中的值,因为返回值(在我下面的解决方案中为$file)是一个列表。这是一个简化的“变量”,在-> 之后意味着它返回该类型,: 之后是您需要在此处输入的类型,向您展示您的变量将代表什么以及您必须如何使用它们:

    /* @each $type->string, $file->map in $icons-map:map */
    @each $type, $file in $icons-map {
        /* if get-value-with-keyname-in-map($file:map, has-png:string)->boolean; is true */
        @if map-get($file, has-png) == true { 
            .icon-#{$type}:after {
                /* print get-value-with-keyname-in-map($file:map, filename:string)->string; */
                background-image: url('#{$image-path}/png/#{map-get($file, filename)}.png');
            }
        } 
    }
    

    请记住,您正在寻找 keys 而不是变量,唯一存在的变量是您定义的变量,这意味着 has-pnghas-svg 不是变量,它们是映射中的键(在这种情况下,映射是$file,在您的映射$icons-map 中键$type 的值)。此外,由于您将它们定义为 booleans,因此您实际上无法使用 == "true" 进行比较,因为这意味着字符串,因此只需检查它们是否为 == true

    我从中得到的输出是:

    .icon-tick:after {
        background-image: url("http://www.mywebsite.com/assets/img/icons/png/icons_tick.png");
    }
    .icon-secure:after {
        background-image: url("http://www.mywebsite.com/assets/img/icons/png/icons_secure.png");
    }
    

    这似乎是你想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      相关资源
      最近更新 更多