sass - SCSS "If"条件带逗号。它是如何工作的?

标签 sass

我发现这是在代码中。据我了解 $should-use-modal 最后应该是 ether falsetrue

$should-use-modal: if(global-variable-exists(use-modal), $use-modal, false) !default;

共有 3 个部分:

  1. global-variable-exists(use-modal) - 检查变量 use-modal 是否存在
  2. $use-modal 是代码中其他地方的 falsetrue
  3. 我们只有false

这里的逻辑是什么?如果结果为 truetruefalse 结果会是什么?或其他组合。我不明白

最佳答案

如果满足条件(第一个参数),则使用第二个参数,如果不满足第三个参数。

例如:

if(true, blue, red) // Outputs "blue"
if(false, blue, red) // Outputs "red"

The built-in if() function allows you to branch on a condition and returns only one of two possible outcomes. It can be used in any script context. The if function only evaluates the argument corresponding to the one that it will return -- this allows you to refer to variables that may not be defined or to have calculations that would otherwise cause an error (E.g. divide by zero).

就您而言:

$should-use-modal: if(global-variable-exists(use-modal), $use-modal, false) !default;

@if $should-use-modal {
  .modal {
    color: red;
  }
}

不会有任何 css 输出,因为 $should-use-modal 变量未定义,因此 $should-use-modal 为 false(指定为第三个if 函数参数)。但如果你定义变量:

$use-modal: something;
$should-use-modal: if(global-variable-exists(use-modal), $use-modal, false) !default;

@if $should-use-modal {
  .modal {
    color: red;
  }
}

由于定义了变量,将输出以下css

.modal {
  color: red;
}

希望有帮助。

有关 sass 条件的更多信息 here .

关于sass - SCSS "If"条件带逗号。它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50486541/

相关文章:

ruby-on-rails-3 - SASS 与 Rails 3

html - 如何拥有更小的按钮 css

html - 使用 img 覆盖修改插入符号

css - 如何在 SASS 中创建一个可以接受键并返回所有祖先列表的递归函数?

sass - 在 Sass 中使用 @include 与 @extend?

css - Sass嵌套扩展内部是自己的父级

ruby-on-rails - 跨样式表的 SCSS 常量 (Rails)

css - ie9 中的 fontello 字体不起作用?

html - 带有白色光标的透明输入文本和背景

css - rails 应用程序 : pipeline asset not working as expected