微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

未加引号的字符串上的 SASS“错误:$color:不是颜色”

如何解决未加引号的字符串上的 SASS“错误:$color:不是颜色”

我使用 gulp-sass-variables 将变量传递给 sass,它总是会注入一个字符串。

取消引用似乎对我的字符串没有任何影响。

$primary: "#ffffff"; // <- Passed in via gulp-sass-variables

$anchor: scale-color(unquote($primary),$lightness: -14%); // Error: $color: #ffffff is not a color.

我是否以错误的方式使用了 unquote

解决方法

您不需要使用取消引号,只需从颜色中删除双引号即可

$primary: #ffffff; 
$anchor: scale-color($primary,$lightness: -14%); 

工作示例:https://www.sassmeister.com/gist/5db400dc2bd9bb62980a8411f158343e

已编辑

你可以试试下面的代码并检查它是否有效:

// Thanks Hugo for convert string to number function
// https://hugogiraudel.com
// Thanks @roshanakjamali

// remove space from string
@function str-replace($string,$search,$replace) {
  $index: str-index($string,$search);

  @if $index {
    @return str-slice($string,1,$index - 1) + $replace +
      str-replace(
        str-slice($string,$index + str-length($search)),$replace
      );
  }

  @return $string;
}

// convert string to list
@function str-split($string,$separator) {
  $split-arr: ();
  $index: str-index($string,$separator);
  @while $index != null {
    $item: str-slice($string,$index - 1);
    $split-arr: append($split-arr,unquote($item));
    $string: str-slice($string,$index + 1);
    $index: str-index($string,$separator);
  }
  $split-arr: append($split-arr,unquote($string));
  @return $split-arr;
}

// convert string to number
@function number($string) {
  // Matrices
  $strings: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9";
  $numbers: 0 1 2 3 4 5 6 7 8 9;

  // Result
  $result: 0;

  // Looping through all characters
  @for $i from 1 through str-length($string) {
    $character: str-slice($string,$i,$i);
    $index: index($strings,$character);

    @if not $index {
      @return false;
    }

    $number: nth($numbers,$index);
    $result: $result * 10 + $number;
  }

  @return $result;
}

@function convertStringToColor($colors) {
  $string: str-replace($colors," ","");
  $list: str-split($string,",");
  $red: nth($list,1);
  $green: nth($list,2);
  $blue: nth($list,3);

  $rgb: rgb(number($red),number($green),number($blue));

  @return $rgb;
}

$primary: "255,255,255" !default; // use rgb color without rgb. rgb(255,255) => 255,255
$anchor: scale-color(convertStringToColor($primary),$lightness: -14%) !default;

工作示例:https://codepen.io/sunilmca09/pen/eYdojPV

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?