首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从sass函数调用sass mixins

从sass函数调用sass mixins
EN

Stack Overflow用户
提问于 2014-08-30 02:54:51
回答 3查看 1.1K关注 0票数 1

我有一个生成选择器名称的sass混入:

代码语言:javascript
复制
@mixin rocks($name){
  #{$name}-rocks {
    @content;
  }
}

通过调用mixin调用:

代码语言:javascript
复制
@include rocks(dave){
  color: red;
}

我想创建一个自定义函数来为我调用这个混入,以将语法简化为:

代码语言:javascript
复制
rocks(dave){
  color: red;
}

是否可以在选择器之外调用sass @函数?2.从其中调用混入?大致是这样的:

代码语言:javascript
复制
@function rocks($name){
  @include @rocks($name)
}

如果可能的话,我更喜欢自定义的sass函数而不是自定义的ruby函数。谢谢!

EN

回答 3

Stack Overflow用户

发布于 2014-08-30 05:06:00

不,也不是。Sass中的函数只能返回值,不能用于创建CSS。这就是混入的用途:创建混入来调用其他混入。

票数 2
EN

Stack Overflow用户

发布于 2017-09-07 19:35:57

代码语言:javascript
复制
@function class($name) {
  @return $name;
}

@mixin className($classname) {
  .#{class($classname)} {
    @content;
  }
}

@mixin concatinate($classname2) {
  &.#{class($classname2)} {
    @content;
  }
}

@mixin parent($classname1) {
  .#{class($classname1)} & {
    @content;
  }
}

@mixin withparent($classname1) {
  @at-root .#{class($classname1)}#{&} {
    @content;
  }
}

@include className(red) {
  color: red;

  @include className(check) {
    color: green;

    @include concatinate(bcd) {
      color: green2;

      ul {
        li {
          color: red;

          & {
            color: blue;
          }

          @include withparent(colorwe) {
            display: none;
          }
        }
      }

      @include parent(efg) {
        color: green1;
      }
    }
  }


  @include className(blue) {
    color: blue;
  }
}
票数 1
EN

Stack Overflow用户

发布于 2017-09-08 14:26:23

@DonaldDuck感谢您的反馈。就我个人而言,我认为没有必要通过混入调用选择器,如果有人尝试这样做,它将通过使用这些混入来解决。

例如:

代码语言:javascript
复制
//call a class
@include className(one) { 
  properties..
}//o/p    .one { properties..}
//add another class with existing one
@include className(one) {
  @include concatinate(two) {
    @include className(three) {
      properties...
    }
  }
}//o/p   .one.two .three { properties...}
//add parent to hierarchy
@include className(one) {
  @include parent(two) {
    properties..
  }
}//o/p .two .one { properties..}
//add class with top of the hierarchy
@include className(one) {
  @include className(two) {
    @include withparent(three) {
      properties...
    }
  }
} o/p  .one.three .two { properties..}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25574544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档