計算屬性computed案例、動態(tài)綁定css樣式案例課外拓展

一、點擊按鈕添加
body部分:

        <div id="itany">
                <h1>Computed 計算屬性</h1>
                <button v-on:click="a++">Add to A</button>
                 <button v-on:click="b++">Add to B</button>
                <p>A - {{a}}</p>
                <p>B - {}</p>
                <p>Age + A = {{addToA}}</p>
              <p>Age + B = {{addToB}}</p>
     </div>

js部分:

<script src="js/vue.js"></script>
<script>
    new Vue({
        el:"#itany",
        data:{
            a:0,
            b:0,
            age:20
        },
        methods:{
            
        },
        computed:{
            addToA:function(){
                console.log("Add to A");
                return this.a+this.age;
            },
            addToB:function(){
                console.log("Add to B");
                return this.b+this.age;
            }
        }
    })
</script>

效果圖:


computed計算屬性案例.png

二、動態(tài)綁定css樣式

body部分:

<div id="itany">
   <h1>動態(tài) CSS Class</h1>
   <h2>示例1</h2>
   <div v-on:click="changeColor = !changeColor" v-bind:class="{changeColor:changeColor}">
       <span>jack</span>
   </div>

   <h2>示例2</h2>
   <button v-on:click="changeColor = !changeColor">change color</button>
   <button v-on:click="changeLength = !changeLength">change Length</button>
   <div v-bind:class="compClasses">
       <span>rose</span>
   </div>
 </div>

js部分:

<script src="js/vue.js"></script>
<script>
    new Vue({
        el:"#itany",
        data:{
            changeColor:false,
            changeLength:false
        },
        computed:{
            compClasses:function(){
                return{
                    changeColor:this.changeColor,
                    changeLength:this.changeLength
                }
                
            }
        }
    })
</script>

css部分:

<style>
    span{
        background:red;
        display: inline-block;
        padding:10px;
        color: #fff;
        margin:10px 0;
    }
    .changeColor span{
        background:green;
    }
    .changeLength span:after{
        content:"length";
        margin-left:10px;
    }
</style>

效果圖:


動態(tài)綁定css樣式.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容