sass 功能不工作

sass function not working

任何与代码相关的建议将不胜感激:)

函数str-remove-alphabet():- 此函数将一个字符串作为 argument.and 删除 string.and return 中的任何字母 int 值。

// 3.str-alpha-remove function. removes alphabet from given string.   
@function str-remove-alphabet($string) {                              
// local variables.                                                 
// loop alphabets list to index occurence of alphabets in string    
@each $i in $list-alphabets {                                       
// if occurence found.                                            
  @if(str-index($string, $i)) {                                     
    $string: str-replace($string, $i, '');                          
    @return $string; // than replace that character with null.      
  } @else {                                                         
    @return $string; // otherwise return string.                    
  }                                                                 
 }                                                                   
}                                                                     

函数字体大小():- 这个函数有两个参数。 1.font-大小和 2. 单位。并转换字体大小(例如 15px)并使用 str-remove-alphabet 函数将其转换为(例如 15)。成功后 return 它开始计算给定单位的字体大小(例如 1.223342rem)。

// 1.font-size calculator based on font-size given and unit.                   

//noinspection CssInvalidFunction                                              

@function font-size($size, $unit: 'em') {                                      
$size: str-remove-alphabet('17px');                                          
$base-font: str-remove-alphabet('15px');                                     
 @if($unit == 'em') {                                                         
   $font-size: ($size / $base-font) em;                                       
   @debug $font-size;                                                         
   @return $font-size; // returns font-size in em format                      
 } @else if($unit == 'per') {                                                 
   $font-size: ($size / $base-font) * 100%;                                   
   @debug $font-size;                                                         
   @return $font-size; // returns font-size in percentage format              
 } @else {                                                                    
   $font-size: ($size / $base-font) * 1rem;                                   
   @debug $font-size;                                                         
   @return $font-size; // else return in rem format                           
 }                                                                            

}                                                                              

问题 :- 我无法在 font-size() 中调用 str-remove-alphabet()。

函数 str-remove-alphabet 很可能没有按照预期执行,因为例如当 list-alphabet 中的第一个字符不匹配时 returns 整个字符串而不检查其余字符characters... 同样,当它找到第一个匹配字符时,它会在字符串中的所有位置替换它并 returns 结果。因此,如果 $list-alphabets: "p" "x";,则输入 17px 将产生 17x

我想你的意思是这样的(str-index 不是必需的,因为无论如何它都会被 str-replace 调用):

@function str-remove-alphabet($string) {

 //$list-alphabets: "p" "x";

 // local variables.                                                 
 // loop alphabets list to index occurence of alphabets in string    
 @each $i in $list-alphabets {                                       
    $string: str-replace($string, $i, '');
 }
 @return $string;
}

我不确定你的 str-remove-alphabet($string) 函数的目标是什么,但对我来说它看起来像一个简单的 strip-unit

还要确保您没有将值作为字符串传递,否则如果您想在算术运算中使用它们,则必须 unquote 它们。

我用 ⚠️ 突出显示了我更改的所有内容。

/// Remove the unit of a length
/// @param {Number} $number - Number to remove unit from
/// @return {Number} - Unitless number
@function strip-unit($number) {
  @if type-of($number) == 'number' and not unitless($number) {
    @return $number / ($number * 0 + 1);
  }

  @return $number;
}

@function font-size($size, $unit: "em") {
    $size: strip-unit($size); // ⚠️ using strip-unit
    $base-font: strip-unit(15px); // ⚠️ using strip-unit
    @if($unit == "em") {
        $font-size: ($size / $base-font) * 1em; // ⚠️ * was missing
        @debug $font-size;
        @return $font-size; // returns font-size in em format
    } @else if($unit == "per") {
        $font-size: ($size / $base-font) * 100%;
        @debug $font-size;
        @return $font-size; // returns font-size in percentage format
    } @else {
        $font-size: ($size / $base-font) * 1rem;
        @debug $font-size;
        @return $font-size; // else return in rem format
    }
} 

所以如果你 运行 它是这样的:

.test {
  font-size: font-size(12px, "per"); //  80%
}

应该可以!