学习Vue的style样式及class绑定(点击变色)

来源:csdn博客 分类: 文章浏览史 发布时间:2020-08-16 19:41:30 最后更新:2020-08-16 浏览:713
转载声明:
本文为摘录自“csdn博客”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-08-16 19:41:30

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Vue的样式绑定-demo(点击字体变颜色)</title>
    <style>
        /* class绑定用到了(包括对象绑定和属性绑定) */
        .actived{
            color:red;
        }
    </style>
</head>
<body>
    <script src="https://vuejs.org/js/vue.js"></script>
    <!-- 一    class绑定:对象绑定 -->
    <div id="app">
        <div :class="{actived:isActived}"  @click="changeColor">
            点击我变色
        </div>
    </div>
    <script>
        var vm=new Vue({
            el:'#app',
            data:{
                isActived:false
            },
            methods:{
                changeColor(){
                    this.isActived=!this.isActived
                }
            }
        })
    </script>
    <!--二    class绑定:绑定数组 -->
    <div id="app">
        <div :class="[actived]" @click="changeColor">
            点我变色
        </div>
    </div>
    <script>
        var vm=new Vue({
            el:'#app',
            data:{
                actived:"",
            },
            methods: {
                changeColor(){
                    this.actived=this.actived==="actived"?"":"actived"
                }
            }
        })
    </script>
    <!--三   style绑定:对象绑定 -->
    <div id="app">
        <div :style="actived" @click="changeColor">点击变色哦</div>
    </div>
    <script>
        var vm=new Vue({
            el:'#app',
            data:{
                actived:{
                    color:'',
                }
            },
            methods:{
                changeColor(){
                    this.actived.color=this.actived.color==='red'?'':'red'
                }
            }
        }) 
    </script>
    <!--四    style绑定:数组绑定 -->
    <div id="app">
        <div :style="[actived]" @click="changColor">点击我变色哦</div>
    </div>
    <script>
        var vm=new Vue({
            el:'#app',
            data:{
                actived:{
                    color:""
                }
            },
            methods:{
                changColor(){
                    this.actived.color=this.actived.color==="red"?'':"red"
                }
            }
        })
    </script>
</body>
</html>

class绑定为两种:对象绑定与数组绑定,

style样式绑定也为两种,对象绑定与属性绑定,

以上是四个方法实现点击变色。要分开试验。

记录自己又学习了Vue的知识点

php技术微信