SCSS mixin 中 if/else 条件的语法 [英] Syntax for if/else condition in SCSS mixin
问题描述
我正在尝试学习 SASS/SCSS,并且正在尝试重构我自己的 mixin 以进行 clearfix
Hi I'm trying to learn SASS/SCSS and am trying to refactor my own mixin for clearfix
我想要的是 mixin 是基于我是否给 mixin 传递了一个宽度.
what I'd like is for the mixin to be based on whether I pass the mixin a width.
到目前为止的想法(只有伪代码,因为我将包括其他混合)
thoughts so far (pseudo code only as I will be including other mixins)
@mixin clearfix($width) {
@if !$width {
// if width is not passed, or empty do this
} @else {
display: inline-block;
width: $width;
}
}
这就是我认为我可以调用它的方式,但它不起作用.
here's how I thought I might call it, but it's not working.
@include clearfix();
或
@include clearfix(100%)
或
@include clearfix(960px)
如果能以最佳或正确的方式执行此操作,我将不胜感激!
I'd appreciate any help on the best or right way to do this!
推荐答案
你可以试试这个:
$width:auto;
@mixin clearfix($width) {
@if $width == 'auto' {
// if width is not passed, or empty do this
} @else {
display: inline-block;
width: $width;
}
}
我不确定您的预期结果,但设置默认值应该返回 false.
I'm not sure of your intended result, but setting a default value should return false.
这篇关于SCSS mixin 中 if/else 条件的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!