Vue實(shí)現(xiàn)動(dòng)態(tài)樣式的多種方法匯總
1. 三元運(yùn)算符判斷
<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 class="wrapper" v-for="(item,index) in houseList" :key="index" @click="rightTap(index)"> <div class="houseTitle" :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 :class="activeId === item.id?'activeStyle':'machineBar'" 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 class="homeWrap" :class="{'select': selected === 1,'click': clicked === 1}" @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" :class="[item.name? 'lg':'sm']"></div> <div class="item" :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)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue同個(gè)按鈕控制展開(kāi)和折疊同個(gè)事件操作
這篇文章主要介紹了vue同個(gè)按鈕控制展開(kāi)和折疊同個(gè)事件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07vue中的<template>標(biāo)簽與react中的<></>標(biāo)簽區(qū)別詳解
這篇文章主要為大家介紹了vue中的<template>標(biāo)簽與react中的<></>標(biāo)簽區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08vue項(xiàng)目netWork地址無(wú)法訪問(wèn)的問(wèn)題及解決
這篇文章主要介紹了vue項(xiàng)目netWork地址無(wú)法訪問(wèn)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Vue3快速實(shí)現(xiàn)文件上傳OSS的方法詳解
這篇文章給大家介紹了Vue3快速實(shí)現(xiàn)文件上傳OSS的方法,上傳文件可以說(shuō)是經(jīng)典的需求了,在后臺(tái)管理項(xiàng)目中隨處可見(jiàn),一般是由前端進(jìn)行文件上傳,然后再由后端去處理,本文旨在實(shí)現(xiàn)上傳功能,不考慮額外的功能(如文件尺寸限制),感興趣的朋友可以參考下2024-01-01Vue項(xiàng)目npm操作npm run serve或npm run dev報(bào)錯(cuò)及二者
這篇文章主要介紹了Vue項(xiàng)目npm操作npm run serve或npm run dev報(bào)錯(cuò)及二者的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10