编程式处理Css样式的示例代码

发布时间:2022-04-13 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了编程式处理Css样式的示例代码脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

编程式方法的好处

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使用的$

到此这篇关于编程式处理Css样式的文章就介绍到这了,更多相关编程式处理Css样式内容请搜索脚本宝典以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本宝典!

脚本宝典总结

以上是脚本宝典为你收集整理的编程式处理Css样式的示例代码全部内容,希望文章能够帮你解决编程式处理Css样式的示例代码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: