Vue實(shí)現(xiàn)動(dòng)態(tài)樣式的多種方法匯總
<text :style='{color:state?’#ff9933’:’#ff0000’}'>hello world </text><script>export default {data() {return {state: true}}}</script>2. 動(dòng)態(tài)設(shè)置class
2.1 主要運(yùn)用于:實(shí)現(xiàn)循環(huán)列表中點(diǎn)擊時(shí),相應(yīng)的元素高亮;(默認(rèn)首個(gè)元素高亮)
<template><div v-for='(item,index) in houseList' :key='index' @click='rightTap(index)'><div :class='{’active’ : index === rightIndex}'>{{item.name}}</div></div></template><script>export default {data() {return {rightIndex:0,houseList:[]};},methods:{rightTap(index){ this.rightIndex = index}}}</script><style lang='scss' scoped>.wrapper{width: 118px;height: 60px;margin: 6px auto 0 auto;.houseTitle{font-size: 22px;text-align: center;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}.active{color:#2a7ffa; background-color: pink;}}</style>
2.2 主要運(yùn)用于:為特定數(shù)值設(shè)置相應(yīng)樣式;
<div : v-for='(item,index) in List' :key='index' @click='clickEvent'> <p>{{item.name}}</p> </div>3. 方法判斷
3.1 主要運(yùn)用于:為不同的數(shù)據(jù)值設(shè)置相應(yīng)的樣式;
<template> <div v-for='(item,index) in houseList' :key='index'> <div :style='getStyle(item.status)'>{{item.name}}</div> </div> </template><script>export default { data(){ return{ houseList:[{ id:1, name:1, status:’a’},{ id:2, name:2, status:’b’},{ id:3, name:3, status:’c’} ] } }, methods:{ getStyle(e){ console.log(’值’,e) if(e === ’a’){ return { width:’60px’, height:’60px’, background:’yellow’, margin: ’10px auto’ } }else if(e === ’b’){ return { width:’60px’, height:’60px’, background:’red’, margin: ’10px auto’ } }else if(e === ’c’){ return { width:’60px’, height:’60px’, background:’pink’, margin: ’10px auto’ } } } }}</script>
3.2 主要運(yùn)用于:在元素多從事件下,顯示相應(yīng)的樣式;
<template> <div :@click='handleClick(1)' @mousedown='menuOnSelect(1)'> 主頁(yè) </div> </template><script>export default { return { selected: 0, clicked: 0 } methods:{ menuOnSelect(value){ this.selected = value; }, handleClick(value){ this.selected = 0 this.clicked = value } } }</script><style lang='stylus' scoped> .homeWrap.select background red .homeWrap.click background yellow</style>4. 數(shù)組綁定
<div :class='[isActive,isSort]'></div>// 數(shù)組與三元運(yùn)算符結(jié)合判斷選擇需要的class<div :class='[item.name? ’lg’:’sm’]'></div><div :class='[item.age<18? ’gray’:’’]'></div>// 數(shù)組對(duì)象結(jié)合<div :class='[{ active: isActive }, ’sort’]'></div>data() { return{ isActive:’active’, isSort:’sort’ }}// css.active{ /*這里寫需要設(shè)置的第一種樣式*/ } .sort{ /*這里寫需要設(shè)置的第二種樣式*/ }5. computed結(jié)合es6對(duì)象的計(jì)算屬性名方法
<div :class='classObject'></div> export default { data(){ return{isActive:true } }, computed:{ classObject() {return{ class_a:this.isActive, class_b:!this.isActive// 這里要結(jié)合自身項(xiàng)目情況修改填寫} } } } // css.class_a{ /*這里寫需要設(shè)置的第一種樣式*/} .class_b{ /*這里寫需要設(shè)置的第二種樣式*/ }
以上就是Vue實(shí)現(xiàn)動(dòng)態(tài)樣式的多種方法匯總的詳細(xì)內(nèi)容,更多關(guān)于Vue實(shí)現(xiàn)動(dòng)態(tài)樣式的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 輕松學(xué)習(xí)XML教程2. xpath簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理3. phpstudy apache開啟ssi使用詳解4. jsp實(shí)現(xiàn)登錄驗(yàn)證的過濾器5. jsp cookie+session實(shí)現(xiàn)簡(jiǎn)易自動(dòng)登錄6. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法7. css代碼優(yōu)化的12個(gè)技巧8. jsp EL表達(dá)式詳解9. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))10. 解析原生JS getComputedStyle
