gpt4 book ai didi

javascript - sass 函数不起作用

转载 作者:行者123 更新时间:2023-11-28 18:01:13 24 4
gpt4 key购买 nike

任何与代码相关的建议都将受到赞赏:)

函数str-remove-alphabet():-该函数采用一个字符串作为参数。并删除该字符串中的所有字母。并返回一个 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.
}
}
}

函数font-size():-这个函数有两个参数。1.字体大小和2.单位。并转换字体大小(例如 15px)并使用 str-remove-alphabet 函数将其转换为(例如 15)。成功返回后,它开始计算给定单位的字体大小(例如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() 。 enter image description here

最佳答案

我不确定您的 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%
}

应该可以用!

关于javascript - sass 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43516917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com