1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>绑定样式</title> <script src="../resources/js/vue.js"></script> <style> .basic {width: 300px;height: 50px;border: 1px solid black;} .happy {border: 3px solid red;background-color: rgba(255, 255, 0, 0.644); background: linear-gradient(30deg, yellow, pink, orange, yellow);} .sad {border: 4px dashed rgb(2, 197, 2);background-color: skyblue;} .normal {background-color: #bfa;} .atguigu1 {background-color: yellowgreen;} .atguigu2 {font-size: 20px;text-shadow: 2px 2px 10px red;} .atguigu3 {border-radius: 20px;} </style> </head> <body> <div id="root"> <!-- 绑定class样式,字符串写法:样式的类名不确定,需要动态指定 --> <div class="basic" :class="mood" id="demo" @click="changeMood">{{name}}</div><br><br><br> <!-- 绑定class样式,数组写法:要绑定的样式个数不确定,名字也不确定 --> <div class="basic" :class="arr" >{{name}}</div>
<!-- 绑定class样式,数组写法:要绑定的样式个数确定,名字也确定,需要动态决定用不用 --> <div class="basic" :class="classObj" >{{name}}</div> </div> <script type="text/javascript"> Vue.config.productionTip = false; new Vue({ el: '#root', data:{ name:'Linzepore', mood:'normal', arr:['atguigu1','atguigu2','atguigu3'], classObj:{ atguigu1:true, atguigu2:true, atguigu3:true } }, methods:{ changeMood(){ // document.getElementById('demo').className = 'basic happy' // this.mood = 'happy'、 const arr = ['happy', 'sad', 'normal']; const random = Math.floor(Math.random()*3); this.mood = arr[random] } }
}) </script> </body> </html>
|