gpt4 book ai didi

css - 如何用前后 block 干掉sass mixin代码?

转载 作者:太空狗 更新时间:2023-10-29 12:24:52 25 4
gpt4 key购买 nike

我有以下 scss 代码。

  @if $position == bottom {
&:after {
height: $triangle-width;
width: $triangle-width;
content:"";
position:absolute;
margin-top: -$triangle-width/2 -$stroke-width;
}
}

@if $position == top {
&:before {
height: $triangle-width;
width: $triangle-width;
content:"";
position:absolute;
margin-bottom: -$triangle-width/2 -$stroke-width;
}
}

如您所见,有些代码是重复的。我想知道有没有办法把它弄干。我试图将其放入自己的类(class),但不知何故似乎不起作用。有任何想法吗?我可以在 mixin 中创建一个 mixin,但在我看来这似乎开销太大了。你怎么看?

最佳答案

通常,让事情变干的最好方法是将公共(public)部分分解为混入,然后将它们构建成更大的混入。这正是 Compass 和大多数其他框架的做法。查看Compass list mixins例如。

@mixin base-triangle($triangle-width) {
height: $triangle-width;
width: $triangle-width;
content:"";
position:absolute;
}

@mixin triangle($position, $triangle-width: 4, $stroke-width: 4) {
@if $position == bottom {
&:after {
@include base-triangle($triangle-width);
margin-top: -$triangle-width/2 -$stroke-width;
}
}

@if $position == top {
&:before {
@include base-triangle($triangle-width);
margin-bottom: -$triangle-width/2 -$stroke-width;
}
}
}

.foo {
@include triangle("top", 8px, 8px);
}

.bar {
@include triangle("bottom");
}

编译为:

.foo:before {
height: 8px;
width: 8px;
content: "";
position: absolute;
margin-bottom: -12px;
}

.bar:after {
height: 4;
width: 4;
content: "";
position: absolute;
margin-top: -6;
}

关于css - 如何用前后 block 干掉sass mixin代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30498142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com