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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| <!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>12.样式绑定</title> <script src="../js/vue.js"></script> <style> .dvbasic { width: 400px; height: 200px; border: 1px solid black; } .dv1 { background-color: cadetblue; } .dv2 { background-color: coral; } .dv3 { background-color: rgb(222, 216, 50); } .ddvv1 { background-color: rgb(30, 33, 32); } .ddvv2 { color: rgb(241, 227, 227); } .ddvv3 { font-size: 30px; } </style> </head> <body>
<div id="root"> <div class="dvbasic" :class="dvClass" @click="dv1Click"> div1 - click :class 绑定class样式 </div> <br /> <div class="dvbasic" :class="dv2Class" @click="dv2Click"> div2 - click :class 绑定class样式 数组形式绑定 </div> <br /> <div class="dvbasic" :class="dv3Class" @click="dv3Click"> div3 - click :class 绑定class样式 对象形式绑定 </div> <br /> <div class="dvbasic" :style="{fontSize: fsize+'px'}"> div4 - :style="{fontSize: fsize+'px'}" 绑定style样式 </div> <br /> <div class="dvbasic" :style="styleObject"> div4 - :style="styleObject" 绑定style样式 </div> <br /> <div class="dvbasic" :style="styleArr"> div5 - :style="styleArr" 绑定style样式 </div> </div> </body> <script> Vue.config.productionTip = false; const vm = new Vue({ el: '#root', data() { return { styleArr: [ { fontSize: '30px' }, { backgroundColor: 'green' } ], styleObject: { fontSize: '20px', backgroundColor: 'red' }, fsize: 40, dvClass: '', dv2Class: [], dv3Class: { ddvv1: false, ddvv2: false, ddvv3: false }, dv1ArrClass: ['dv1', 'dv2', 'dv3'] }; }, methods: { dv1Click() { const index = Math.floor(Math.random() * 3); console.log(index); this.dvClass = this.dv1ArrClass[index]; }, dv2Click() { if (this.dv2Class.length === 0) { this.dv2Class.push('ddvv1'); this.dv2Class.push('ddvv2'); this.dv2Class.push('ddvv3'); } else { this.dv2Class.shift(); this.dv2Class.shift(); this.dv2Class.shift(); } }, dv3Click() { this.dv3Class.ddvv1 = !this.dv3Class.ddvv1; this.dv3Class.ddvv2 = !this.dv3Class.ddvv2; this.dv3Class.ddvv3 = !this.dv3Class.ddvv3; } } }); </script> </html>
|