首頁  >  文章  >  web前端  >  vue中class和style設定的相關方法

vue中class和style設定的相關方法

高洛峰
高洛峰原創
2017-02-18 14:50:061463瀏覽

class&style樣式設定

class

html程式碼

<p id="box">
    <strong>阿斯顿发</strong>
</p>

css程式碼

.red {
    color: red;
}
.gray {
    background-color: gray;
}

js程式碼

window.onload = function() {
    new Vue({
        el: '#box',
        data: {
            red: 'red',
            gray: 'gray'
        }
    });
}

樣式生效的寫法

  1. 屬性

    <strong :class="[red, gray]"></strong>
  2. :class="{red: true, gray: true}"

    <strong :class="{red: true, gray: true}"></strong>
    第二種方法也是可以呼叫vue參數了的data屬性的,記住,Vue裡面一切皆是資料

    new Vue({
        el: '#box',
        data: {
           red: 'red',
           gray: 'gray',
           a: true,
           b: false
     }   
    });
    <strong :class="{red: a, gray: b}"></strong>
  3. :class="json",第二種方法的精簡版,官方版本

    new Vue({
        el: '#box',
        data: {
            red: 'red',
            gray: 'gray',
            json: {
                a: true,
                b: false
            }
        }
    });
  4. <strong :class="json"></strong>
style

和class基本上相同

:style="a"
    <strong :style="{color: &#39;red&#39;, backgroundColor: &#39;gray&#39;}"></strong>
    new Vue({
        el: '#box',
        data: {
            red: 'red',
            gray: 'gray',
            a: {
                color: 'red',
                backgroundColor: 'gray' //注意复合样式的书写规范
            }
        }
    });
  1. :style="[a, b]", a, b對應的是data裡的兩個json資料
  1. 更多vue中class和style設定的相關方法 相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:CSS3 文字換行下一篇:CSS3 文字換行