css - 需要帮助创建 scss 函数以返回字符串

标签 css sass mixins

我需要在很多地方包含模块导入,所以我想在 scss 中创建 mixin 或函数。

on my mixin.scss

 @function import-modules($import,$grid){
      @return $import +' modules/modulename_'+$grid ;
  }

in main.scss where i want to import, i have

import-modules (@import,1024);

但它会抛出错误。如何创建函数以返回导入语句?

我得到的错误:“import-modules”后的无效 CSS:应为“{”,是“('@import',1024);”)

感谢您的回答,我是这样修改和尝试的:

$modulesList: 'modules/header/header_', 
'modules/news/news_', 
'modules/footer/footer_', 
'modules/follow_us/follow_us_', 
'modules/ads/ads_', 
'modules/newsletter/newsletter_', 
'modules/comments/comments_';

@function import-modules($grid){
    @each $module in $modulesList{
                @return $module+$grid;
                }

}
@import url(import-modules(1024));

在 css 中,我看到: @import url("modules/header/header_1024");

最佳答案

Sass 不执行任意字符串。如果要导入文件,则必须使用 @import 指令。

@function import-modules($grid){
    @return 'modules/modulename_#{$grid}';
}

@import url(import-modules(1024));

然而...

Imports may contain #{} interpolation, but only with certain restrictions. It’s not possible to dynamically import a Sass file based on a variable; interpolation is only for CSS imports. As such, it only works with url() imports.

来源:http://sass-lang.com/documentation/file.SASS_REFERENCE.html#import

关于css - 需要帮助创建 scss 函数以返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22795452/

相关文章:

html - 多个背景图像 - 只需要一个来覆盖

html - HTML表格行上不同颜色的双单边边框

javascript - 你能帮我用 Sass 代码解释 `&` 吗?

css - 悬停状态下的选项卡可访问性

css - 基于百分比加深 SCSS Mixin

less - 可选的 LESS mixin 变量可以引用另一个变量吗

jQuery/CSS - 如何设置元素在另一个元素中首次出现的样式?

css - 为什么在 SASS 中 #{$var} 之后没有编译括号?

Javascript mixin 模式设置实例特定的 this 变量

html - 为什么 Chrome 不在列表中显示#?