sass - Scss 函数不返回计算值
问题描述
我正在研究用于断点的 mixin,并且遇到以下问题。当设置了特定状态(最大宽度的模式)时,应通过提取一个 em 值(1px/16(默认字体大小))重新计算断点。
这是我的代码的重要部分(我可能会摆脱这个函数,基本上这可以内联完成):
$mediaBreakpoint: map-get( $breakpoints, $breakpoint );
// if the mode is for max-width then subtract 1px.
@if map-get( $modes, $mode ) == 'max-width' {
$mediaBreakpoint: calculateMaxWidth(#{$mediaBreakpoint})
}
@debug $mediaBreakpoint;
/**
* calculate the max width based on input
*/
@function calculateMaxWidth($breakpoint){
$newBreakpoint: calc( #{$breakpoint} - 0.0625em ); // 1px in em sizing.
@return $newBreakpoint;
}
但无论我尝试什么,@debug 值都显示为:48em-0.0625em // 这是无效的,我需要实际结果(在本例中为 47.9375)。64em // 有效的最小宽度
这是编译好的css:
@media screen and (max-width: calc( 48em - 0.0625 )) {
}
我错过了什么?
解决方案
I found the answer myself after a lot of debugging. At first I misunderstood the interpolation. After reading the docs in depth I noticed that I should have wrapped the whole calculation instead of just the variable because I am working inside the map expression.
Quoted from the official Sass docs:
Interpolation can be used almost anywhere in a Sass stylesheet to embed the result of a SassScript expression into a chunk of CSS.
I changed my function to calculate like this and then the mixin started working. hooray!
$newBreakpoint: #{$breakpoint - 0.0625em};
推荐阅读
- android - 我想将数据从一个屏幕传递到另一个屏幕,并将数据保存到 ListView/ListTiles
- sql - 如何用另一列中的值替换一列中的值
- graphql - 如果参数为空,GraphQL 查询默认返回全部
- python-3.x - 如何在多线程类之外运行 OpenCV VideoCapture 的读取功能?
- python - 一旦 GAME OVER 来了,如何在不关闭程序并在 PYGAME 中再次运行的情况下重玩游戏?
- r - 在 R 中,什么会导致suspended-session-data 文件夹中的“选项”文件变得太大?
- font-awesome - DotnetNuke 2scx 抑制 Font-Awesome 类
- javascript - modal con vueJS 显示,但我无能为力
- vba - VBA 过程在 O365 升级后不再编译
- java - 等待功能在 10 秒后给我一个错误