在 scss 中使用 math.div 代替 /

kum*_*004 2 sass

需要使用 math.div 而不是斜杠重写此函数。下面给出的 URL 中提到的详细信息。(由于一些格式问题无法在此处发布代码)

jsfiddle 域 + /26ty5aj7

@return #{($px / $base-font-size) + $metric};

eve*_*way 5

这是具有更新语法的函数。确保将@use导入保留在文件的顶部。

@use "sass:math";

@function px2em($px, $metric: 'em', $base-font-size: 16px) {
  @if unitless($px) {
    @warn "Assuming #{$px} to be in pixels, attempting to convert it into pixels.";
    @return px2em($px * 1px, $metric, $base-font-size);
  } @else if unit($px) == em {
    @return $px;
  }
  $test: #{math.div($px, $base-font-size) + $metric};
  @return $test;
}

// Pixels to rem based on sass-mq
@function px2rem($px) {
  @if unit($px) == rem {
    @return $px;
  }

  @return px2em($px, 'rem');
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果您不这样做或由于某种原因无法使用 `math.div`,有一些技巧可以除以简单的数字,例如 `$x / 2`,只需使用 `$x * 0.5`。对于复杂的情况,您需要调用该函数并且需要进行升级 (6认同)
  • 从 [docs](https://sass-lang.com/documentation/break-changes/slash-div#automatic-migration),还有一个自动迁移工具:`npm install -g sass-migrator` 然后`sass-migrator 划分 **/*.scss` ` (3认同)
  • 使用 math.div 的 sass 兼容性是因为 "sass": "^1.33" (2认同)