SCSS如何将字符串转换为数字值
我想使用以下功能使用字体大小的信息执行数学操作。我使用SASS Build-In String函数删除rem
$ font-size
的单元信息,但是我无法执行数学操作,因为值类型是字符串。
有没有办法将字符串值类型转换为SASS中的数字值类型?
@use "../abstracts/variables" as *;
@use "sass:math";
@function em($value, $font-size) {
$length: str-length(#{$font-size}); //sample-think about it font-size 1.125rem
$trim: ($length - 3);
$data: str-slice(#{$font-size}, 1, $trim);
$result: math.div($value, $data);
@return $result + "em";
}
I want to perform mathematical operations using the font-size information with the function below. I remove the rem
unit information of $font-size
using the sass build-in string functions, but then I cannot perform mathematical operations because the value type is string.
Is there a way to convert a string value type to numeric value type in sass?
@use "../abstracts/variables" as *;
@use "sass:math";
@function em($value, $font-size) {
$length: str-length(#{$font-size}); //sample-think about it font-size 1.125rem
$trim: ($length - 3);
$data: str-slice(#{$font-size}, 1, $trim);
$result: math.div($value, $data);
@return $result + "em";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好使用助手函数删除
$ font-size
的单位,例如此。It would probably be better to use a helper function to remove the unit of
$font-size
such as this one.但是,如果您尝试使用 stripunit 功能时会遇到问题,则在这样
But if you will have an issue when you are trying to use stripUnit function, like this
In a code like:
Just add a nuber unit 0 or 1 to your
rem
,em
,px
, and etc..., like this:+ 0rem
,* 1rem
.Now the build is fine!