编程式方法的好处
1.全局控制,避免样式散乱
2.代码简洁,开发快速 函数式编程大量使用函数,减少了代码的重复,因此程序比较短,开发速度较快
3.接近 自然语言 ,易于理解 函数式编程的自由度很高,可以写出很接近自然语言的代码
4.更方便的代码管理
5.书写样式成为一门艺 术
Less
Bad
.card -t IT le {
font: "Ping fan g -s C-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; @fontF ami ly) {
font-f am ily: @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
plu gin Options: {
'style-resources-loader': {
PR eProcessor: 'less',
patterns: [
resolve('./src/less/theme.less')
]
}
},
// 在任何组件中 或者 less文件中使用
<style lang="less" sco PE d>
. br eadTop {
h ei ght: 60px;
dis play : flex;
align -i tems: 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-l arg e($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样式的示例代码 所遇到的问题。
如果觉得 网站内容还不错, 推荐好友。
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did201536