css - 如何为 mixin 列表多次生成一个类?

标签 css sass compass-sass

我是这个元素的开发人员之一,我们的 SASS 是由设计师交付的。我只了解 SASS 的基础知识,所以我不确定这是否可行。

我有一个类,我想生成多次,类名应该根据混入而改变。

这些是混入:

@mixin small-tablet
{
    @media only screen and (max-width:767px)
    {
        @content;
    }
}

@mixin mobile
{
    @media only screen and (max-width:480px)
    {
        @content;
    }
}

这是我要添加的部分(伪代码):

@include small-tablet, mobile
{
    table.responsive-@mixin-name
    {
        display: block;
    }
}

输出应该是这样的:

@media only screen and (max-width:767px)
{
    table.responsive-small-tablet
    {
        display: block;
    }
}

@media only screen and (max-width:480px)
{
    table.responsive-mobile
    {
        display: block;
    }
}

我怎样才能得到这个结果?

最佳答案

你可以这样试试

$mobile: 480;
$smalltablet: 767;


@mixin mq($width) {
    @media only screen and (max-width:#{$width}px) {
        @content;
    }
}

@mixin generateMediaqueries {
    @include mq($mobile) {
      &-mobile {
        @content; 
      }
    }
    @include mq($smalltablet) {
      &-small-tablet {
        @content;
      }
    }
}

table.responsive {
  @include generateMediaqueries {
     display: block;
  }
}

结果输出:

@media only screen and (max-width: 480px) {
  table.responsive-mobile {
    display: block;
  }
}
@media only screen and (max-width: 767px) {
  table.responsive-small-tablet {
    display: block;
  }
}

关于css - 如何为 mixin 列表多次生成一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23060689/

相关文章:

css - 创建响应式加载器内容

iphone - 如何在 WebKit 中使用 CSS 实现摆动动画?

css - 为什么是:after pseudo-element rendering inside child element instead of the selected element?

html - CSS 背景颜色 : none;

ruby - 安装可移植 ruby​​ 和可移植 xampp 以便在我的 Windows 机器 : Is that possible? 中使用 compass 和 sass

ruby-on-rails - Rails 3.1 和样式表框架

jquery - 透明前景

html - 使用 CSS 对 Angular 裁剪图像并添加边框

sass - Grunt 项目推荐的目录结构是什么

javascript - 如何在 Angular 2 应用程序中正确连接 ng2-datepicker 组件