首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sass函数不起作用

sass函数不起作用
EN

Stack Overflow用户
提问于 2017-04-20 10:27:21
回答 2查看 4.1K关注 0票数 0

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

str-remove-alphabet():-函数函数该函数接受一个字符串作为argument.and删除任何字母表,在string.and中返回一个int值。

代码语言:javascript
复制
// 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.                    
  }                                                                 
 }                                                                   
}                                                                     

font-size():-函数函数这个函数有两个参数。1.字体大小和2.单位。并转换字体大小(如15 eg),并转换为(例如15)使用str-删除-字母表功能。在成功返回后,它开始计算给定单元的字体大小(如1.223342rem)。

代码语言:javascript
复制
// 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                           
 }                                                                            

}                                                                              

problem :-我无法在字体大小()内调用str-remove字母表()。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-20 11:03:22

我不知道str-remove-alphabet($string)函数的目标是什么,但在我看来,这是一个简单的strip-unit

还请确保不将值作为字符串传递,否则,如果要在算术操作中使用值,则必须将其用于取消报价

我用⚠️高亮显示了我更改的所有内容。

代码语言:javascript
复制
/// 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
    }
} 

所以如果你这样运行它:

代码语言:javascript
复制
.test {
  font-size: font-size(12px, "per"); //  80%
}

应该管用的!

票数 2
EN

Stack Overflow用户

发布于 2017-04-20 10:48:37

函数str-remove-alphabet很可能不执行所需的操作,因为例如,当list-alphabet中的第一个字符不匹配时,它返回整个字符串而不检查其余的字符.类似地,当它找到第一个匹配字符时,它会在字符串中的任何地方替换它并返回结果。因此,如果是$list-alphabets: "p" "x";,那么输入17px就会产生17x

我想您的意思是类似的( str-index是不必要的,因为它无论如何都是由str-replace调用的):

代码语言:javascript
复制
@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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43516917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档