我该如何使用Susy在不同页面上生成不同的布局?

3

我正在使用Susy 2.1.3作为网格系统。我有一个包含元素,不同的模板具有不同的沟槽。我已经声明了两个不同的布局,并认为我正在正确地调用它们。然而,最后定义的布局适用于所有地方。在下面的代码中,即使在.home页面上也应用了$onepx-gutters布局。

我的SCSS代码非常像:

$small-gutters : (
    columns: 12,
    gutters: 0.137254902,
    output: float,
    gutter-position: split,
    global-box-sizing: border-box,
);

$onepx-gutters : (
    columns: 12,
    gutters: 1px/80px,
    output: float,
    gutter-position: before,
    global-box-sizing: border-box,
);

.home .item-container {
    @include layout($small-gutters);
    @include container();
}

.products .item-container {
    @include layout($onepx-gutters);
    @include container();
}


.item-container .item-width-2 {
    @include span(first 8 of 12);
    &:nth-child(2n+3) {
      clear: both;
    }
}

.item-container .item-width-1 {
    @include span(4 of 12);
}

生成的CSS代码类似于:
.item-width-2 {
    width: 65.66092%;
    float: left;
    margin-left: 0.50287%;
    margin-right: 0.50287%; }
.item-width-2:nth-child(2n+3) {
    clear: both;
}

.item-width-1 {
    width: 32.32759%;
    float: left;
    margin-left: 0.50287%;
    margin-right: 0.50287%;
}

正如您所看到的,它不会为每个实例生成单独的 .home .item-width-2.products .item-width-2,并在每个实例上使用不同的外边距。


您的配套HTML将非常有帮助。 - jonsuh
@jonsuh 我已经添加了生成的CSS代码。我认为你会同意CSS的特异性是问题所在。 - Jodi Warren
如果一个页面有 <div class="home"><div class="item-container">,另一个页面有 <div class="products"><div class="item-container">,那么特定性在这种情况下不应该是一个问题。这就是为什么我对生成的 HTML 感到好奇,特别是设置 homeproducts 类的区域。 - jonsuh
也许我应该更详细地解释一下问题:生成的CSS没有以.home或.products为前缀。理想情况下,我们应该有.home .item-width-2和.products .item-width-2,每个都有不同的边距。我避免试图引导问题,但也许这不是最有效的提问方式。 - Jodi Warren
2个回答

7

你的解决方案之所以有效是因为你改变了代码顺序,而不是因为命名空间。无论在哪里使用@include layout,都会更改所有后续代码的全局变量(直到再次更改它)。还有其他几个选项:

// with-layout 
// - only change the settings for nested code

@include with-layout($onepx-gutters) {
  @include span(3); // this will use $onepx-gutters
}

@include span(3); // this will not


// local context
// - change your settings for any one mixin!

@include span(3 $onepx-gutters); // this will use $onepx-gutters
@include span(3 $small-gutters); // this will use $small-gutters

0

如果我按顺序声明布局,就可以正常工作,如下所示:

@mixin item-width-2() {
    @include span(first 8 of 12);
    &:nth-child(2n+3) {
        clear: both;
    }
}

@mixin item-width-1() {
    @include span(4 of 12);
}

.products {
    .item--holder {
        @include layout($onepx-gutters);
        @include container();
    }
    .item {
        &.width-2 {
            @include item-width-2();
        }
        &.width-1 {
            @include item-width-1();
        }
    }
}
.home {
    .item-holder {
        @include layout($small-gutters);
        @include container();
    }
    .item {
        &.width-2 {
            @include item-width-2();
        }
        &.width-1 {
            @include item-width-1();
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接