如何在 JavaScript 函数中使用 Sass 变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24186431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:19:19  来源:igfitidea点击:

How can I use Sass variables in a JavaScript function?

javascriptjquerycsssass

提问by Bas

I have a variable in Sass which determines how many columnsare supposed to fit in 1 rowfor my grid system.

我在 Sass 中有一个变量,它决定了我的网格系统columns应该有多少适合 1 row

Normally, those are 12 columns.

通常,这些是 12 columns

In JavaScript I've got a function which checks if there are more then 12 columns in a row.

在JavaScript中我有哪些检查是否有更多然后12个函数columnS IN一个row

If so, it hides the whole row.

如果是这样,它将隐藏整行。

This function looks like this:

这个函数看起来像这样:

function checkColumns() {
    var maxColumns = 12;
    $('.row').each(function () {
        var elementsLength = $(this).children('.column').length;
        if (elementsLength > maxColumns) {
            $(this).remove();
        }
    });
}

Is there any way to change the maxColumnsvariable to the same number which is in my sass variables? Besides then changing it manually every time I change the number in my sass file.

有什么方法可以将maxColumns变量更改为与我的 sass 变量中相同的数字?除了每次更改 sass 文件中的数字时手动更改它。

采纳答案by bottens

Just seen this earlier this morning here: http://viget.com/extend/sharing-data-between-sass-and-javascript-with-json

今天早上刚在这里看到这个:http: //viget.com/extend/sharing-data-between-sass-and-javascript-with-json

First you need to include: https://github.com/vigetlabs/sass-json-vars

首先你需要包括:https: //github.com/vigetlabs/sass-json-vars

Install the gem:

安装宝石:

gem install sass-json-vars

Place the variables in a json file

将变量放在 json 文件中

// variables.json
{
    "font-sans": "Helvetica, sans-serif",
    "colors": {
        "red": "#c33"
    }
}

Import the file in Sass to expose variable names:

在 Sass 中导入文件以公开变量名称:

@import "variables.json"

body {
    color: map-get($colors, red);
    font: $font-sans;
}

Require sass-json-vars when compiling

编译时需要 sass-json-vars

sass style.scss -r sass-json-vars