热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用Sass将循环内部的选择器分组-GroupingSelectorsinsideofaloopusingSass

TheIssueImcurrentlyinapickle.IneedtogroupselectorsinsideofaSassloop.Ivetriedma

The Issue

I'm currently in a pickle. I need to group selectors inside of a Sass loop. I've tried many different ways to go about doing this such as:

我现在正在泡菜。我需要在Sass循环中对选择器进行分组。我已尝试过许多不同的方法来做到这一点,例如:

body {
    $store: null;
    @for $i from 1 through 10 {
        $store: append($store, ".offset-by-#{$i}", comma);
    }
    // produces content: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;
    @each $j in $store {
        $store: unquote($j);
    }
    // produces .offset-by-10
}

What I'm trying to accomplish using pure Sass (no Ruby) is the following:

我想用纯Sass(没有Ruby)来完成的是以下内容:

.offset-by-1,
.offset-by-2,
.offset-by-3,
...
.offset-by-10 { foo: bar; }

If you are a Sass god then please give me an idea of what to do here. If this is an inherent limitation of the meta-language then let me know about that too!

如果你是萨斯神,那么请告诉我这里要做什么。如果这是元语言的固有限制,那么请告诉我这一点!

Considerations

I can't use anything other than a mixin to accomplish this because functions are expected to be used on a property value. Mixins, on the other hand allow the production of entire blocks of code.

我不能使用mixin之外的任何东西来实现这一点,因为函数应该用在属性值上。另一方面,Mixins允许生成整个代码块。

2 个解决方案

#1


7  

Have you tried something like this:

你尝试过这样的事情:

@mixin createNumbered($num, $className){
    @for $i from 1 through $num {
        .#{$className}-#{$i} {
            @content;
        }
    }
}

@include createNumbered(10, 'foo-bar'){
    color: white;
}

Updated:

更新:

@mixin createNumbered($num, $className){
    $foo : '';
    @for $i from 1 through $num {
        $foo : $foo + '.' + $className + '-' + $i + ', ';
    }
    #{$foo} {
        @content;
    }
}

@include createNumbered(10, 'foo-bar'){
    color: white;
}

#2


11  

Keep it simple, soldier!

保持简单,士兵!

%foo {
  foo: bar; }

@for $i from 1 through 10 {
  .offset-by-#{$i} {
    @extend %foo; }}

UPD You can also have individual styles with this approach:

UPD你也可以用这种方法获得个人风格:

%foo {
  foo: bar; }

@for $i from 1 through 10 {
  .offset-by-#{$i} {
    @extend %foo;
    margin-left: 50px * $i; }}

Which results in the following CSS:

这导致以下CSS:

.offset-by-1, .offset-by-2, .offset-by-3, .offset-by-4, .offset-by-5, .offset-by-6, .offset-by-7, .offset-by-8, .offset-by-9, .offset-by-10 {
  foo: bar; }

.offset-by-1 {
  margin-left: 50px; }

.offset-by-2 {
  margin-left: 100px; }

.offset-by-3 {
  margin-left: 150px; }

.offset-by-4 {
  margin-left: 200px; }

.offset-by-5 {
  margin-left: 250px; }

.offset-by-6 {
  margin-left: 300px; }

.offset-by-7 {
  margin-left: 350px; }

.offset-by-8 {
  margin-left: 400px; }

.offset-by-9 {
  margin-left: 450px; }

.offset-by-10 {
  margin-left: 500px; }

推荐阅读
author-avatar
air12345
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有