flex.scss 2.83 KB
// 快捷margin和padding

// 方向列表
$directions: top, right, bottom, left;

// 数值范围(从1到100)
@for $i from 0 through 100 {
    @each $direction in $directions {

        // 生成 margin 类
        .margin-#{$direction}-#{$i} {
            margin-#{$direction}: #{$i}px;
        }

        // 生成 padding 类
        .padding-#{$direction}-#{$i} {
            padding-#{$direction}: #{$i}px;
        }
    }

    // 生成所有方向的 margin 类
    .margin-#{$i} {
        margin: #{$i}px;
    }

    // 生成所有方向的 padding 类
    .padding-#{$i} {
        padding: #{$i}px;
    }
}


// 使用Sass的循环生成动态的CSS类
@for $i from 1 through 100 {
    .w#{$i} {
        width: $i * 1%; // 使用 $i 乘以 1% 来设置宽度
    }
}

@for $i from 1 through 100 {
    .h#{$i} {
        height: $i * 1%; // 使用 $i 乘以 1% 来设置高度
    }
}


@for $i from 1 through 100 {
    .fs#{$i} {
        font-size: #{$i}px;
    }
}



// 主轴方向 属性值
$directionList: row, row-reverse, column, column-reverse;
// 主轴对齐方式 属性值
$justifyContentList: flex-start, flex-end, center, space-between, space-around;
// 交叉轴对齐方式 属性值
$alignItemsList: flex-start, flex-end, center, baseline, stretch;

// 三层遍历,组合所有属性值
@each $direction in $directionList {

    // 简化一些属性值
    $dir: $direction;

    @if $direction =='row' {
        $dir: 'x';
    }

    @if $direction =='column' {
        $dir: 'y';
    }

    @each $justifyContent in $justifyContentList {

        // 简化一些属性值
        $JC: $justifyContent;

        @if $justifyContent =='flex-start' {
            $JC: 'start';
        }

        @if $justifyContent =='flex-end' {
            $JC: 'end';
        }

        @if $justifyContent =='space-between' {
            $JC: 'between';
        }

        @if $justifyContent =='space-around' {
            $JC: 'around';
        }

        @each $alignItems in $alignItemsList {

            // 简化一些属性值
            $AI: $alignItems;

            @if $alignItems =='flex-start' {
                $AI: 'start';
            }

            @if $alignItems =='flex-end' {
                $AI: 'end';
            }

            // 根据变量,组合为css代码
            @if $AI =='center' {
                .flex-#{$dir}-#{$JC} {
                    display: flex;
                    flex-direction: $direction;
                    justify-content: $justifyContent;
                    align-items: center;
                }
            }

            @else {
                .flex-#{$dir}-#{$JC}-#{$AI} {
                    display: flex;
                    flex-direction: $direction;
                    justify-content: $justifyContent;
                    align-items: $alignItems;
                }
            }
        }
    }
}