Vue.js中對css的操作(修改)具體方式詳解
使用v-bind:class或者v-bind:style或者直接通過操作dom來對其樣式進(jìn)行更改;
1.v-bind:class || v-bind:style
其中v-bind是指令,: 后面的class 和style是參數(shù),而class之后的指在vue的官方文檔里被稱為'指令預(yù)期值'(這個不必深究,反正個人覺得初學(xué)知道他叫啥名有啥用就好了)同v-bind的大多數(shù)指令(部分特殊指令如V-for除外)一樣,除了可以綁定字符串類型的變量外,還支持一個單一的js表達(dá)式,也就是說v-bind:class的'指令預(yù)期值'除了字符串以外還可以是對象或者數(shù)組(‘v-bind:'中的v-bind可省略)。
1.1:對象語法:
通過對象來綁定v-bind:class=“{'css類名':控制是否顯示(true or false)}”
<template>
<div>
<div class='mycolor' :class="{'colordisplay':display}"><span>1.1我的對象更改&&綁定test</span></div>
</div>
</template>
<script>
export default {
name: 'mytest',
data() {
return {
display: true
}
},
mounted() {},
computed: {},
methods:{}
}
</script>
<style>
.colordisplay {
display: inline;
background: red;
border: 1px solid blue
}
</style>
如果display為true,那么此時該部分的class就是 class="mycolor colordisplay",通過設(shè)置display的值就可以控制colordisplay的顯示
如果要設(shè)置綁定多個class的話就和正常的對象鍵值對一樣中間用逗號隔開就可以了v-bind:class="{'colordisplay':display,'ispay':pay}"
1.2:內(nèi)聯(lián)樣式:
v-bind:style='mycolor'
template
<div :style='mypagestyle'><span>1.2我的樣式內(nèi)聯(lián)更改&&綁定test</span></div>
data
mypagestyle:{color: 'yellow',background:"blue"},
1.3:數(shù)組語法:
也可以通過數(shù)組來綁定v-bind:style='[mycolor1,mycolor2]'
<div :style="[myarr,myarrtest]"><span>1.3我的數(shù)組更改&&綁定test</span></div>
然后設(shè)置返回的數(shù)據(jù)為
myarr:{color: 'white'},
myarrtest:{background:'#000',display:'inline'},
2.計算屬性
也可以通過計算屬性計算(適用于較為復(fù)雜判斷)樣式
<div class='computed' :class='compuretu'>2.計算屬性判斷</div>
將計算屬性的返回值作為類名,通過判斷serd和slid的值來控制樣式的顯示與否
data() {
return {serd:true,slid:true}
},
computed: {
compuretu: function() {
return {compuretu: this.serd && this.slid}
}
}
設(shè)置樣式
.compuretu{color:white;background: red}
3.操作節(jié)點(diǎn)
由于vue本身是虛擬dom如果通過document來進(jìn)行節(jié)點(diǎn)樣式更改的時候有可能會出現(xiàn)'style' is not definde的錯誤,
這個問題的解決方式就必須得對vue 的理解要求更高一層了,它可以通過在vue本身的周期mounted函數(shù)里用ref和$refs 來獲取樣式,來完成對其樣式的更改:示例如下:
<template>
<div>
<div style=“color: green;” ref="abc"><span>我的test</span></div>
</div>
</template>
<script>
export default {
name: 'mytest',
data() {
return {}
},
mounted() {console.log(this.$refs.abc.style.cssText)}
}
<script>
<style>
</style>
說明:
1.ref被用來給元素(子組件)注冊引用信息;
2.vm.$refs 一個對象,持有已注冊過 ref 的所有子組件(或HTML元素);
使用:在 HTML元素 中,添加ref屬性,然后在JS中通過vm.$refs.屬性來獲取
上述會將style的內(nèi)容全部輸出(color: green;)
這樣的話對其進(jìn)行更改就可以對相應(yīng)的屬性直接更改(this.$refs.abc.style.color=red)
<template>
<div>
<div :class='{mycss}'><span>我的單個class屬性的test</span></div>
<div class='mycolor' :class="{'colordisplay':display}"><span>1.1我的對象更改&&綁定test</span></div>
<div :style='mypagestyle'><span>1.3我的樣式內(nèi)聯(lián)更改&&綁定test</span></div>
<div :style="[myarr,myarrtest]"><span>1.3我的數(shù)組更改&&綁定test</span></div>
<div class='computed' :class='compuretu'>2.計算屬性判斷</div>
<div style="color: green;" ref="abc"><span>3.我的dom更改test</span></div>
</div>
</template>
<script>
export default {
name: 'mytest',
data() {
return {
serd:true,
slid:true,
mycss: {
color: ''
},
mypagestyle:{
color: 'yellow',
background:"blue"
},
myarr:{
color: 'white'
},
myarrtest:{
background:'#000',
display:'inline'
},
display: true
}
},
mounted() {
console.log(this.$refs.abc.style.cssText)
this.$refs.abc.style.color = 'red' //修改屬性
this.$refs.abc.style.background = 'black' //新增屬性
this.$refs.abc.style.display = 'inline'
console.log(111)
console.log(this.$refs.abc.style.display)
},
computed: {
compuretu: function() {
return {
compuretu: this.serd && this.slid
}
}
},
methods:{
}
}
</script>
<style>
.mycss {
color: blue
}
.colordisplay {
display: inline;
background: red;
border: 1px solid blue
}
.mycolor {
color: orange
}
.computed {
border: 1px solid yellow
}
.compuretu{
color:white;
background: red;
}
</style>
當(dāng)然最后這種方式對于初入門的朋友來講可能會有點(diǎn)理解不透,所以我更建議大家使用前幾種方式
總結(jié)
以上所述是小編給大家介紹的Vue.js中對css的操作(修改)具體方式詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue關(guān)于this.$refs.tabs.refreshs()刷新組件方式
這篇文章主要介紹了vue關(guān)于this.$refs.tabs.refreshs()刷新組件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Vite創(chuàng)建Vue3項目及Vue3使用jsx詳解
vite是新一代的前端構(gòu)建工具,下面這篇文章主要給大家介紹了關(guān)于Vite創(chuàng)建Vue3項目以及Vue3使用jsx的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Vue.js中實(shí)現(xiàn)密碼修改及頁面跳轉(zhuǎn)和刷新的完整指南
在現(xiàn)代Web應(yīng)用中,用戶賬戶管理是一個核心功能,其中密碼修改是一個常見的需求,本文將詳細(xì)介紹如何在Vue.js應(yīng)用中實(shí)現(xiàn)用戶密碼修改功能,并在成功后跳轉(zhuǎn)到登錄頁面并刷新該頁面,需要的朋友可以參考下2024-12-12
vue請求接口并且攜帶token的實(shí)現(xiàn)
本文主要介紹了vue請求接口并且攜帶token的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
vue的表單數(shù)據(jù)收集案例之基本指令和自定義指令詳解
收集表單數(shù)據(jù)可以使用這個v-model實(shí)現(xiàn)這個數(shù)據(jù)的綁定,但是在有些輸入框中,還需要一些其他的指令搭配這個v-model指令結(jié)合使用,這篇文章主要介紹了vue的表單數(shù)據(jù)收集,基本指令和自定義指令,需要的朋友可以參考下2023-01-01

