Vue.js中的綁定樣式實(shí)現(xiàn)
綁定class樣式
1、字符串寫法

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue初識(shí)</title>
<script type="text/javascript" src="./js/vue.js"></script>
<style>
.basic{
width: 400px;
height: 100px;
border:1px solid black
}
.happy{
background: pink;
}
.sad{
background: skyblue;
}
.normal{
background: aquamarine;
}
</style>
</head>
<body>
<div id="root">
<!--綁定class樣式--字符串寫法,適用于:樣式類名不確定,需動(dòng)態(tài)指定-->
<div class="basic" :class="mood" @click="changeMood">{{name}}</div>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:"#root",
data:{
name:'好好學(xué)習(xí)',
mood:'normal'
},
methods:{
changeMood(){
const arr = ['happy','sad','normal']
//生成0-2的隨機(jī)數(shù)
this.mood = arr[Math.floor(Math.random()*3)]
}
}
})
</script>
</body>
</html>2、數(shù)組寫法

<!DOCTYPE html>
<html lang="en">
<head>
......
<style>
.basic {
width: 400px;
height: 100px;
border: 1px solid black
}
......
.addOne {
background: orange;
}
.addTwo {
font-size: 40px;
}
.addThree {
border-radius: 5px;
}
</style>
</head>
<body>
<div id="root">
......
<!--綁定class樣式--數(shù)組寫法,適用于:要綁定的樣式個(gè)數(shù)和名字都不確定-->
<div class="basic" :class="classArr">{{name}}</div>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el: "#root",
data: {
name: '好好學(xué)習(xí)',
mood: 'normal',
classArr: ['addOne', 'addTwo', 'addThree']
}
......
})
</script>
</body>
</html>3、對(duì)象寫法
<style>
.basic {
width: 400px;
height: 100px;
border: 1px solid black
}
.addOne {
background: orange;
}
.addTwo {
font-size: 40px;
}
</style>
<!--綁定class樣式-對(duì)象寫法-適用于:綁定樣式個(gè)數(shù)確定,名字也確定,但動(dòng)態(tài)決定用不用-->
<div class="basic" :class="classObj">{{name}}</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:"#root",
data:{
name:"好好學(xué)習(xí)",
classObj:{
addOne:false,
addTwo:true
}
}
})
</script>style 綁定樣式
<!--正常的style寫法-->
<div class="basic" style="font-size: 40px">{{name}}</div>
<!--使用vue展示樣式-->
<div class="basic" :style="{fontSize:fsize+'px'}">{{name}}</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el: "#root",
data: {
name: "好好學(xué)習(xí)",
fsize: 40
}
})
</script>或者使用 style 對(duì)象寫法:
<div class="basic" :style="styleObj">{{name}}</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el: "#root",
data: {
name: "好好學(xué)習(xí)",
styleObj:{
fontSize:"30px",
color:"red",
backgroundColor:"orange"
}
}
})
</script>或者 style 數(shù)組寫法:
<div class="basic" :style="[styleObj,styleObj2]">{{name}}</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el: "#root",
data: {
name: "好好學(xué)習(xí)",
styleObj:{
fontSize:"30px",
color:"red"
},
styleObj2:{
backgroundColor:"orange"
}
}
})
</script>綁定樣式
1、class樣式
- 寫法:
class="xxx"xxx 可以是字符串、對(duì)象、數(shù)組 - 字符串寫法適用于:類名不確定,要?jiǎng)討B(tài)獲取
- 對(duì)象寫法適用于:要綁定多個(gè)樣式,個(gè)數(shù)不確定,名字也不確定
- 數(shù)組寫法適用于:要綁定多個(gè)樣式,個(gè)數(shù)確定,名字也確定,但不確定用不用
2、 style樣式 :style="{fontsize: xxx}"其中xxx是動(dòng)態(tài)值
:style="[a,b]"其中a、b是樣式對(duì)象
scoped
作用:讓樣式在局部生效,防止沖突 寫法::<style scoped> 后期在組件中用
到此這篇關(guān)于Vue.js中的綁定樣式實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue綁定樣式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3實(shí)現(xiàn)簡(jiǎn)易音樂播放器組件
這篇文章主要為大家詳細(xì)介紹了Vue3實(shí)現(xiàn)簡(jiǎn)易音樂播放器組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
解析vue?3.0?使用axios庫發(fā)起?post?get?的配置過程
get post 請(qǐng)求開發(fā)中最普通最常見的請(qǐng)求方式但是在vue中如何實(shí)現(xiàn)呢 這里記錄一下配置過程,對(duì)vue?使用axios發(fā)起?post?get配置過程感興趣的朋友一起看看吧2022-05-05
關(guān)于在vue-cli中使用微信自動(dòng)登錄和分享的實(shí)例
本篇文章主要介紹了關(guān)于在vue-cli中使用微信自動(dòng)登錄和分享的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
vue-resource攔截器設(shè)置頭信息的實(shí)例
下面小編就為大家?guī)硪黄獀ue-resource攔截器設(shè)置頭信息的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
淺談vue中$event理解和框架中在包含默認(rèn)值外傳參
這篇文章主要介紹了淺談vue中$event理解和框架中在包含默認(rèn)值外傳參,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

