Vue.js基礎(chǔ)學(xué)習(xí)之class與樣式綁定
更新時(shí)間:2017年03月20日 14:52:20 作者:笑嘆詞窮
這篇文章主要為大家介紹了Vue.js的Class與樣式綁定,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
打著鞏固 css 知識(shí)的旗號(hào)開始了對(duì) vue 樣式綁定的研究,相比前一篇的 demo,本次內(nèi)容多了各種樣式在里面,變得稍微花哨了些,話不多說,直接上代碼吧:
<html>
<head>
<meta charset="utf-8">
<title>Vue test</title>
<style type="text/css">
body {font-family: Verdana;}
p { font-family: Times, "Times New Roman", serif;}
.static.active {color: green; font-size: 35px;}
div.text-danger {color: red;font-size: 25px;}
div.active {color: blue;font-family: Verdana;}
</style>
<script src="./vue.min.js"></script>
</head>
<body>
<div id="app">
<!-- Create an instance of the todo-item component -->
<todo-item></todo-item>
</div>
<div class="static"
v-bind:class="{ active: isActive, 'text-danger': hasError }">
<p>class property set.</p>
</div>
<div id="app3"
v-bind:class="[activeClass,errorClass]">
<p>group class property set.</p>
</div>
<div id="app4" v-bind:class="[isActive ? 'active' : 'text-danger']">
<p>三元表達(dá)式加樣式</p>
</div>
<div id="app5">
<my-component v-bind:class="{ active: isActive }"></my-component>
</div>
<div id="app6">
<p v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">綁定內(nèi)聯(lián)樣式</p>
<p v-bind:style="styleObject">對(duì)象樣式綁定</p>
</div>
<script>
Vue.component('todo-item', {
template: '<p>todo test.</p>'
})
// 一定要實(shí)例化才能用
var app = new Vue({
el: '#app'
})
// 用類選擇器構(gòu)造一個(gè)Vue對(duì)象并綁定額外的class屬性
var app2 = new Vue({
el: '.static',
data: {
isActive: false,
hasError: true
}
})
// 數(shù)組語法加 class (因?yàn)槭菑南峦夏脴邮?,所以text-danger的color樣式被覆蓋)
var app3 = new Vue({
el: '#app3',
data: {
activeClass: 'active',
errorClass: 'text-danger'
}
})
var app4 = new Vue({
el: '#app4',
data: {
isActive: true
}
})
Vue.component('my-component',{
template: '<p class="static">在已經(jīng)定義好樣式的自定義組件上加樣式</p>'
})
var app5 = new Vue({
el: '#app5',
data: {
isActive: true
}
})
// 綁定內(nèi)聯(lián)樣式
var app6 = new Vue({
el: '#app6',
data: {
activeColor: '#FF00FF',
fontSize: 30,
styleObject: {
color: '#585858',
fontSize: '25px'
}
}
})
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中實(shí)現(xiàn)可編輯table及其中加入下拉選項(xiàng)
這篇文章主要介紹了vue中實(shí)現(xiàn)可編輯table及其中加入下拉選項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue項(xiàng)目安裝less和less-loader的詳細(xì)步驟
這篇文章主要介紹了Vue項(xiàng)目安裝less和less-loader的詳細(xì)步驟,本文分步驟結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
解決vue做詳情頁跳轉(zhuǎn)的時(shí)候使用created方法 數(shù)據(jù)不會(huì)更新問題
這篇文章主要介紹了解決vue做詳情頁跳轉(zhuǎn)的時(shí)候使用created方法 數(shù)據(jù)不會(huì)更新問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Vue CLI3創(chuàng)建項(xiàng)目部署到Tomcat 使用ngrok映射到外網(wǎng)
這篇文章主要介紹了Vue CLI3創(chuàng)建項(xiàng)目部署到Tomcat 使用ngrok映射到外網(wǎng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05

