国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Vue實現動態樣式的多種方法匯總

瀏覽:42日期:2022-09-28 17:33:36
目錄1. 三元運算符判斷2. 動態設置class3. 方法判斷4. 數組綁定5. computed結合es6對象的計算屬性名方法1. 三元運算符判斷

<text :style='{color:state?’#ff9933’:’#ff0000’}'>hello world </text><script>export default {data() {return {state: true}}}</script>2. 動態設置class

2.1 主要運用于:實現循環列表中點擊時,相應的元素高亮;(默認首個元素高亮)

<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 主要運用于:為特定數值設置相應樣式;

<div : v-for='(item,index) in List' :key='index' @click='clickEvent'> <p>{{item.name}}</p> </div>3. 方法判斷

3.1 主要運用于:為不同的數據值設置相應的樣式;

<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 主要運用于:在元素多從事件下,顯示相應的樣式;

<template> <div :@click='handleClick(1)' @mousedown='menuOnSelect(1)'> 主頁 </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. 數組綁定

<div :class='[isActive,isSort]'></div>// 數組與三元運算符結合判斷選擇需要的class<div :class='[item.name? ’lg’:’sm’]'></div><div :class='[item.age<18? ’gray’:’’]'></div>// 數組對象結合<div :class='[{ active: isActive }, ’sort’]'></div>data() { return{ isActive:’active’, isSort:’sort’ }}// css.active{ /*這里寫需要設置的第一種樣式*/ } .sort{ /*這里寫需要設置的第二種樣式*/ }5. computed結合es6對象的計算屬性名方法

<div :class='classObject'></div> export default { data(){ return{isActive:true } }, computed:{ classObject() {return{ class_a:this.isActive, class_b:!this.isActive// 這里要結合自身項目情況修改填寫} } } } // css.class_a{ /*這里寫需要設置的第一種樣式*/} .class_b{ /*這里寫需要設置的第二種樣式*/ }

以上就是Vue實現動態樣式的多種方法匯總的詳細內容,更多關于Vue實現動態樣式的資料請關注好吧啦網其它相關文章!

標簽: Vue
相關文章:
主站蜘蛛池模板: 汉沽区| 通化县| 长顺县| 遵化市| 平定县| 临桂县| 高清| 班戈县| 遂昌县| 鄂尔多斯市| 通河县| 泗洪县| 文登市| 孟连| 甘肃省| 中宁县| 伊川县| 瑞丽市| 绥芬河市| 嘉黎县| 喜德县| 托克逊县| 花莲县| 乌恰县| 庄浪县| 汨罗市| 福贡县| 安仁县| 新和县| 凤山市| 伽师县| 临桂县| 阿拉善右旗| 乌拉特前旗| 安国市| 中江县| 巴彦淖尔市| 敖汉旗| 莆田市| 新建县| 南岸区|