gpt4 book ai didi

css - 参数混合值(不)影响嵌套变量

转载 作者:太空宇宙 更新时间:2023-11-04 04:47:58 25 4
gpt4 key购买 nike

我试图允许列中的元素是行中唯一或第一个元素(为其提供适当的边距)的情况,并添加了 .first、.only 和 .firstOnly 类。有没有办法合并参数混合中提供的值来定义(或更改)变量?

$colCount: 2;
$colSpan: 1;
$gutter: 4%;

$width: $colSpan * ((100% - $gutter) / $colCount);


@mixin width($colCount: 2, $colSpan: 1, $gutter: 4%) {
$width: $colSpan * ((100% - $gutter) / $colCount);
}

@mixin columns($colCount: 2, $colSpan: 1, $gutter: 4%) {
@content;
@include width() {
width: $width;
}


&.first {
@include first();
}

&.only {
@include only();
}

&.firstOnly {
@include firstOnly();
}
}

@mixin first($colCount: 2, $colSpan: 1, $gutter: 4%) {
margin-left: 100% - $width;
}

@mixin only($colCount: 2, $colSpan: 1, $gutter: 4%) {
margin-left: 100% - (2 * $width) - (2 * $gutter);
}

@mixin firstOnly($colCount: 2, $colSpan: 1, $gutter: 4%) {
margin-left: 100% - $width - $gutter;
margin-right: 100% - $width;
}

最佳答案

这里发生了很多不太正确的事情。

// If this is part of a reusable library, use the default flag
// I typically prefer prefixing global variables to avoid collisions with other code
// (eg. $grid-colCount instead of $colCount)
$colCount: 2 !default;
$colSpan: 1 !default;
$gutter: 4% !default;

// Mixin changed to a function, better for its intended purpose
@function width($colCount: $colCount, $colSpan: $colSpan, $gutter: $gutter) {
@return $colSpan * ((100% - $gutter) / $colCount);
}

// The arguments are set to our global variables by default
@mixin columns($colCount: $colCount, $colSpan: $colSpan, $gutter: $gutter) {
@content;
// Something weird was going on here that just plain didn't compile
width: width($colCount, $colSpan, $gutter);

&.first {
// This is where your mixin was getting messed up,
// it wasn't passing the values it received on to the mixins
@include first($colCount, $colSpan, $gutter);
}

&.only {
@include only($colCount, $colSpan, $gutter);
}

&.firstOnly {
@include firstOnly($colCount, $colSpan, $gutter);
}
}

@mixin first($colCount: $colCount, $colSpan: $colSpan, $gutter: $gutter) {
margin-left: 100% - width($colCount, $colSpan, $gutter);
}

@mixin only($colCount: $colCount, $colSpan: $colSpan, $gutter: $gutter) {
margin-left: 100% - (2 * width($colCount, $colSpan, $gutter)) - (2 * $gutter);
}

@mixin firstOnly($colCount: $colCount, $colSpan: $colSpan, $gutter: $gutter) {
margin-left: 100% - width($colCount, $colSpan, $gutter) - $gutter;
margin-right: 100% - width($colCount, $colSpan, $gutter);
}

当我们这样调用 mixin 时:

.test {
@include columns(3, 8, 40%);
}

.test2 {
@include columns;
}

我们得到这个结果:

.test {
width: 160%;
}

.test.first {
margin-left: -60%;
}

.test.only {
margin-left: -300%;
}

.test.firstOnly {
margin-left: -100%;
margin-right: -60%;
}


.test2 {
width: 48%;
}

.test2.first {
margin-left: 52%;
}

.test2.only {
margin-left: -4%;
}

.test2.firstOnly {
margin-left: 48%;
margin-right: 52%;
}

关于css - 参数混合值(不)影响嵌套变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14088445/

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