<!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>Document</title> <script src="js/vue.js"></script> <style> .myopacity{ /* 半透明 */ opacity: 0.5; } </style> </head> <body> <div id="app"> <!-- mouseover这个事件会在鼠标移动到h1标签上的时候触发 --> <!-- mouseover这个时间会在鼠标从h1标签上移走的时候触发--> <!-- <h1 v-bind:class="{myopacity: isActive}" style="cursor: pointer" v-on:mouseover="changeOpacity1()" v-on:mouseout="changeOpacity2()"> 样式绑定的一个实际应用</h1> --> <h1 v-bind:style="{opacity: isActive ? '0.5' : '1' , cursor: 'pointer'} " v-on:mouseover="changeOpacity1()" v-on:mouseout="changeOpacity2()">样式绑定的一个实际应用</h1> </div> <script> let vm = new Vue({ el:'#app', data:{ isActive:true }, methods:{ changeOpacity1(){ // 通过数据isActive来驱动myopacity从class中删除 this.isActive = false }, changeOpacity2(){ // 通过数据isActive来驱动myopacity从class中 this.isActive = true } } }) </script> </body> </html>