vue uniapp 动态class

复制代码
 1     // 绑定单个内联样式
 2     <view :style="{'display':config.isHaveSearch ? 'block':'none'}" ></view>
 3     <view :style="{color:item.age<18?'#ccc':''}"></view>
 4     
 5     <view :style="{ color: activeColor, fontSize: fontSize + 'px' }"></view>
 6     <view :style="[{ color: activeColor, fontSize: fontSize + 'px' }]"></view>
 7 
 8 
 9 
10     // 绑定单个class属性
11     <view :class="{ 'active': isActive }" ></view>
12     <view :class="{'active':isActive==index}"></view>
13     
14     <view :class="{ active: isActive }"></view>
15     <view :class="{ active: isActive, 'text-danger': hasError }"></view>
16     <view :class="[activeClass, errorClass]"></view>
17     <view :class="[isActive ? activeClass : '', errorClass]"></view>
18     <view :class="[{ active: isActive }, errorClass]"></view>
19 
20 
21     <view :class="cosi==index ? 'select':'unselect'"></view>    
22 
23 
24 
25     // 数组的方式,直接绑定多个 class 属性
26     <view :class="['green', 'font-big','font-bold' ]" ></view>
27 
28 
29     // 多条件绑定样式
30    <view class='step' :class="[item.state=='工作' ? 'online' : '',item.state=='空闲' ? 'idle' : '',item.state=='离线' ? 'offline' : '']">
31 
32 
33 
34     // 绑定多个class属性
35     
36     <view :class="{ 'active': isActive, 'sort': isSort }"></view>
37     // data 中声明
38     :class="classObject"
39     data() {
40       return {
41         classObject:{ active: true, sort:false }
42       }
43     }
44     
45     // 第三种(使用computed属性)
46     :class="classObject"
47     data() {
48       return {
49         isActive: true,
50         isSort: false
51       }
52     },
53     computed: {
54       classObject: function () {
55         return {
56              active: this.isActive,
57            sort:this.isSort
58         }
59     }
60  
61 
62 
63     :class="[isActive,isSort]"
64     data() {
65        return{
66           isActive:'active',
67           isSort:'sort'
68        }
69     }
70     // 数组与三元运算符结合判断选择需要的class
71    <view :class="[item.name? 'lg':'sm']"></view>
72    <view :class="[item.age<18? 'gray':'']"></view>
73    // 数组对象结合
74    :class="[{ active: isActive }, 'sort']"
75    
76 
77 
78 
79 // 复杂情况
80 <text class="title" :style="'filter:grayscale('+(funding>'0'?'100%':'0')+')'" style="color:red;">{{title}}</text>
复制代码

 

 

来源:  https://blog.csdn.net/xkqbaiyuan/article/details/103613853

posted @   伊人兮明眸秋水  阅读(2281)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示