关于css:编程式处理Css样式

8次阅读

共计 1555 个字符,预计需要花费 4 分钟才能阅读完成。

编程式办法的益处

1. 全局管制,防止款式散乱

2. 代码简洁,开发疾速 函数式编程大量应用函数,缩小了代码的反复,因而程序比拟短,开发速度较快

3. 靠近自然语言,易于了解 函数式编程的自由度很高,能够写出很靠近自然语言的代码

4. 更不便的代码治理

5. 书写款式成为一门艺术

Less

Bad

.card-title {
    font: "PingFang-SC-medium";
    color: #333;
    font-size: 18px;
}

.card-title {
    font: "PingFang-SC-regular";
    font-size: 14px;
    color: #333;
}

Good

// 申明 less 函数
.mixin-font-class(@fontColor: yellow; @fontSize; @fontFamily) {
    font-family: @fontFamily;
    font-size: @fontSize;
    color: @fontColor;
}

利用

h6 {.mixin-font-class(@fontColor:red;@fontSize:12px;@fontFamily:"PingFang-SC-medium");
}
h2{.mixin-font-class(@fontColor:blue;@fontSize:15px;@fontFamily:"PingFang-SC-regular");
}

全局 Less

在 Vue-cli 模式中

// 增加全局 less
pluginOptions: {
        'style-resources-loader': {
            preProcessor: 'less',
            patterns: [resolve('./src/less/theme.less')
            ]
        }
},
// 在任何组件中或者 less 文件中应用
<style lang="less" scoped>
.breadTop {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-right: 15px;
    h6 {.mixin-font-class(@fontColor:red;@fontSize:12px;@fontFamily:"PingFang-SC-medium");
       }
       h2{.mixin-font-class(@fontColor:blue;@fontSize:15px;@fontFamily:"PingFang-SC-regular");
       }
}
</style>

scss

$font-normal-color = #222;
$font-light-color = #333;

@mixin font-class($fontFamily, $fontSize, $fontColor) {
    font-family: $fontFamily;
    font-size: $fontSize;
    color: $fontColor;
}

@mixin font-large($size: 14px, $color: $font-normal-color) {@include font-class($font-family-medium, $size, $color);
}

@mixin font-normal($size: 14px, $color: $font-light-color) {@include font-class($font-family-regular, $size, $color);
}

应用

.form-title {@include font-large(16px, red);
}

.form-text {@include font-large(12px, blue);
}

留神 less 函数的参数应用的 @,scss 应用的 $

其余举荐

三十分钟学会 Less
SASS 用法指南

正文完
 0