vue 动态样式 ,style,class

4 篇文章 0 订阅

在这里插入图片描述
**class 动态样式,可以跟变量,数组,json
如果跟json后面是“类名”:boolean,如果是真值就添加这个类名,假值就不添加。

class后面跟json**

<style>
        .red{
            background-color: red;
        }
        .blue{
            background-color: blue;
        }
        .purple{
            background-color: purple;
        }
        .yellow{
            background-color: yellow;
        }
    </style>
     <!-- 每四行隔行变色 -->
        <!-- 0 4 8
        1 5 9
        2 6 10
        3 7 11 -->
        <table>
            <tr v-for="(item,index) in 12"  
            **:class="{red:index%4==0,yellow:index%4==1,blue:index%4==2,purple:index%4==3}">**
                <td>{{index}}</td>
            </tr>
        </table>

在这里插入图片描述

style 动态style 后面跟的是json,也可以是变量

<div id="app">
        <h2 :style="red">春江花月夜</h2>    //red是一个json对象,里面写着css的样式值
        <button @click=change>修改样式</button>
    </div>
    <script>
        new Vue({
            el: "#app", 
            data: { 
                red:{
                    background:"red",
                    color:"white",
                }
            },
            methods: { //方法
                change(){     //点击修改json的值就可以修改h2的样式了
                    this.red.background = "black",
                    this.red.color = "red"
                }
            }
        })
    </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值