通过条件判断使用不同的css样式

1.目的

通过动态参数的值来决定使用什么样式,即通过JavaScript中的判断来决定css中使用哪种样式。代码:vue2

2.简单举例

实现前
在这里插入图片描述
实现后
在这里插入图片描述

3.实现

created() {
        console.log(this.disabled)
        if(this.disabled){
            console.log(11111)
            var root = document.documentElement;
            root.style.setProperty('--disabled-color', '#999');
            root.style.setProperty('--disabled-opacity', '0.6');
            root.style.setProperty('--disabled-background-color', '#f5f5f5');
        }else {
            console.log(222222)
        }
    },
在created函数中通过判断传入的disabled的值来判断使用哪种样式,因为created钩子函数在页面渲染前执行,所以在此处实现
这里只做简单演示,如果为true,修改为置灰样式

css代码如下

:root {
    --disabled-color: #db1414;
    --disabled-opacity: 0.6;
    --disabled-background-color: #ece2e2;
}

.el-collapse-item__arrow.el-icon-arrow-right:before{
    background-color: var(--disabled-background-color);
    color: var(--disabled-color);
    opacity: var(--disabled-opacity);
}

将.el-collapse-item__arrow.el-icon-arrow-right:before{换成自己的class名称即可,其他代码不变

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

scdn_wyy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值