简体   繁体   English

SCSS / SASS条件化变量

[英]SCSS/SASS conditionalize a variable

I am trying to see if there is a way to conditionals a variable in my scss. 我正在尝试查看是否有一种方法可以在我的scss中设置条件变量。 The scenario being for a fallback font loader throwing a class on my body when all fonts are loaded - so I could switch out fonts. 备用字体加载器的场景是在加载所有字体时在我的身上抛出一个类,因此我可以切换字体。

So the idea is I have my _variables.scss file set up, and inside it has all my site fonts looking something like this - 因此,我的想法是设置了_variables.scss文件,并且在其中包含了我所有的网站字体,如下所示:

$primary-font-family: 'Font1', sans-serif;
$primary-font-normal: 400;
$primary-font-bold: 700;

$secondary-font-family: 'Font2', serif;
$secondary-font-normal: normal;
$secondary-font-bold: normal;

This has been working great for me, but I am looking for an easy way to swap them out without added class listeners in the css to every instance where I am consuming these fonts. 这对我来说一直很好,但是我正在寻找一种简便的方法来交换它们,而无需在CSS中将类监听器添加到我使用这些字体的每个实例中。 So I was wondering if there might be a way to conditionalize these variables - 所以我想知道是否可能有一种条件化这些变量的方法-

 // if .fonts-loaded class is on body, use loaded font
 $primary-font-family: 'Font1', sans-serif;
 // else use system default font
 $primary-font-family: 'defaultSystemFont', sans-serif;

Unsure if something like this would be possible, does anyone know of a way to achieve this. 不确定是否会发生这种情况,是否有人知道实现此目标的方法。 Thanks for reading! 谢谢阅读!

Sass would never know if something was loaded on the page like JS would. Sass永远不会知道页面上是否加载了像JS这样的东西。

Once it compiles, Sass becomes static CSS and doesn't necessarily communicate according to changes made on the page (beyond knowing it should style things a certain way according to a pre-defined pseudo-class.) 编译后,Sass变为静态CSS,并且不一定根据页面上的更改进行通信(除了知道它应该根据预定义的伪类以某种方式设置样式)。

Also, the more we keep telling a page to "think more" - the less it benefits in terms of loading time. 同样,我们越多地告诉页面“多思考”-从加载时间上受益越少。

Since CSS is "cascading", you can always set a default font stack such as: 由于CSS是“级联”的,因此您始终可以设置默认字体堆栈,例如:

.stuff {color: #333; font-family: Arial, Helvetica, sans-serif; font-size: 1em;}

...and then, lower in the CSS (possibly within a partial that's imported after the global layout, as part of the theme): ...然后,降低CSS的位置(可能是在全局布局之后导入的部分中,作为主题的一部分):

.stuff {font-family: "SuperSpecialFont";}

It will then load, as it's read, setting the default foundation and then progressively enhancing it. 然后,它将在读取时加载,设置默认基础,然后逐步增强它。 Sass won't ever be aware of a page's loading status, however, it will deliver style based on what's available and when - which allows you to have a fallback until everything it's supposed to present is in place. Sass永远不会知道页面的加载状态,但是,它将根据可用的内容和时间提供样式-这使您可以进行后备,直到应该呈现的所有内容都放好为止。

=== ===

According to your comment, there doesn't seem to be a reason for a conditional in this case. 根据您的评论,在这种情况下似乎没有理由要有条件。

Fundamental CSS would handle it with .thisClass .thatClass or .thisClass > .thatClass - depending on how strict you want to be. 基本的CSS可以使用.thisClass .thatClass.thisClass > .thatClass -取决于您要严格的程度。 So, for example, in Sass: 因此,例如,在Sass中:

.thatClass {
    color: #333;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 1em;
}

.thisClass {
    .thatClass {
        font-family: 'SuperSpecialFont';
    }
}

If it's nested inside of .thisClass , then it will apply those properties. 如果它嵌套在.thisClass ,那么它将应用这些属性。 If not, it will stay as it was originally defined. 如果没有,它将保持原始定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM