現(xiàn)在需要一個和底部tabbar的東西一模一樣的,要求就是點(diǎn)擊某個,它的圖片和背景色改為選中的樣式,其它的便會默認(rèn)樣式。
現(xiàn)在我已經(jīng)做了出來,可以實(shí)現(xiàn)這樣的功能,但是實(shí)現(xiàn)的過程實(shí)在是不堪入目。下面上代碼
HTML
<div class="optioncardbox">
<div class="optioncard optioncardtext" @click="changetype(0)">
<p :class="[is0?a:b]">ALL</p>
<p class="optioncardtip">全部</p>
</div>
<div class="optioncard optioncardlist">
<img :src="[is1?e:f]" alt="" srcset="" :class="[is1?d:c]" @click="changetype(1)">
<p class="optioncardtip">營養(yǎng)</p>
</div>
<div class="optioncard optioncardlist">
<img :src="[is2?g:h]" alt="" srcset="" :class="[is2?d:c]" @click="changetype(2)">
<p class="optioncardtip">運(yùn)動</p>
</div>
<div class="optioncard optioncardlist">
<img :src="[is3?i:j]" alt="" srcset="" :class="[is3?d:c]" @click="changetype(3)">
<p class="optioncardtip">睡眠</p>
</div>
<div class="optioncard optioncardlist">
<img :src="[is4?k:l]" alt="" srcset="" :class="[is4?d:c]" @click="changetype(4)">
<p class="optioncardtip">情緒</p>
</div>
<div class="optioncard optioncardlist">
<img :src="[is5?m:n]" alt="" srcset="" :class="[is5?d:c]" @click="changetype(5)">
<p class="optioncardtip">內(nèi)分泌</p>
</div>
</div>
JavaScript
export default {
data() {
return {
is0: true,
is1: false,
is2: false,
is3: false,
is4: false,
is5: false,
istip0: true,
istip1: false,
istip2: false,
goodsinfo: [],
targetid: 0,
a: 'optioncardfirst',
b: 'notoptioncardfirst',
c: 'optioncardimg',
d: 'notoptioncardimg',
e: '../../../static/image/icon16-y.png',
f: '../../../static/image/icon16.png',
g: '../../../static/image/icon17-y.png',
h: '../../../static/image/icon17.png',
i: '../../../static/image/icon18-y.png',
j: '../../../static/image/icon18.png',
k: '../../../static/image/icon19-y.png',
l: '../../../static/image/icon19.png',
m: '../../../static/image/icon20-y.png',
n: '../../../static/image/icon20.png',
o: 'islessonlistoptioncard',
p: 'lessonlistoptioncard',
}
},
methods: {
changetype(x) {
if (x == 0) {
this.is0 = true
this.is1 = false
this.is2 = false
this.is3 = false
this.is4 = false
this.is5 = false
this.targetid = 0
this.getLessonlist(0)
this.istip0 = true
this.istip1 = false
this.istip2 = false
} else if (x == 1) {
this.is1 = true
this.is0 = false
this.is2 = false
this.is3 = false
this.is4 = false
this.is5 = false
this.targetid = 16
this.getLessonlist(0)
this.istip0 = true
this.istip1 = false
this.istip2 = false
} else if (x == 2) {
this.istip0 = true
this.istip1 = false
this.istip2 = false
this.is2 = true
this.is0 = false
this.is1 = false
this.is3 = false
this.is4 = false
this.is5 = false
this.targetid = 17
this.getLessonlist(0)
} else if (x == 3) {
this.istip0 = true
this.istip1 = false
this.istip2 = false
this.is3 = true
this.is0 = false
this.is2 = false
this.is1 = false
this.is4 = false
this.is5 = false
this.targetid = 18
this.getLessonlist(0)
} else if (x == 4) {
this.istip0 = true
this.istip1 = false
this.istip2 = false
this.is4 = true
this.is0 = false
this.is2 = false
this.is3 = false
this.is1 = false
this.is5 = false
this.targetid = 19
this.getLessonlist(0)
} else if (x == 5) {
this.istip0 = true
this.istip1 = false
this.istip2 = false
this.is5 = true
this.is0 = false
this.is2 = false
this.is3 = false
this.is4 = false
this.is1 = false
this.targetid = 20
this.getLessonlist(0)
}
},
這個是部分代碼,其中有些無關(guān)緊要?,F(xiàn)在的基本思路就是加載2套不同樣式和src,然后每次點(diǎn)擊之后判定進(jìn)行切換,選中的進(jìn)行加載,其它的返回默認(rèn)值。但是這樣的代碼實(shí)在繁瑣,感覺程序員不應(yīng)該寫出這樣的代碼,說起來真挺丟人的,誰有很好的思路可以提供一下。非常感謝。
https://codepen.io/kingdil/pe...
1.tabbar圖片和文字一一對應(yīng),首先考慮到的肯定是循環(huán)數(shù)據(jù)格式應(yīng)該如下
tabs:[
{
title:'全部',
ico:'http://thyrsi.com/t6/367/1536299620x-1404781180.png',
icon:'http://thyrsi.com/t6/367/1536299648x-1404781180.png',
},
{
title:'營養(yǎng)',
ico:'http://thyrsi.com/t6/367/1536299680x-1404781180.png',
icon:'http://thyrsi.com/t6/367/1536299701x-1404781180.png',
},
{
title:'運(yùn)動',
ico:'http://thyrsi.com/t6/367/1536299735x-1404781180.png',
icon:'http://thyrsi.com/t6/367/1536299715x-1404781180.png',
}
]
2.選中后圖片變化,文字變色,設(shè)選中的索引值為curIdx,當(dāng)選中索引值等于tabbar索引值時便是該tabbar被選中。添加選中后的class,改變選中后的圖標(biāo)
<ul class="tabs">
<li :class="{'cur':curIdx==index}" v-for="(item,index) in tabs" @click="selectTabs(index)">
<div class="tab_ico">
<img :src="curIdx==index?item.icon:item.ico" alt="">
</div>
<div class="tab_title">
<span v-text="item.title"></span>
</div>
</li>
</ul>
3.綁定點(diǎn)擊事件,傳索引值,點(diǎn)擊哪個tabbar的時候curIdx就等于改索引值
selectTabs:function(index){
this.curIdx=index;
},
代碼大概是這樣的
<script src="https://libs.cdnjs.net/vue/2.3.0/vue.min.js"></script>
<script src="https://libs.cdnjs.net/jquery/1.12.4/jquery.min.js"></script>
<style>
.b{background-color:black;}
.a{background-color:red;}
.optioncardbox{width:50px;}
.hide{display:none;}
</style>
<p style="line-height: 18px; font-size: 18px; font-family: times;">
<div id='tbar' class="optioncardbox">
<div v-for='(btn,key) in btns' class="optioncard optioncardtext" @click="changetype(key)">
<p :class="{'hide':!(!btn.src1&&btn.selected)}" class="a">{{btn.text}}</p>
<p :class="{'hide':!(!btn.src1&&!btn.selected)}" class="b">{{btn.text}}</p>
<img :class="{'hide':!(btn.src1&&btn.selected)}" :src="btn.src1" alt="1"/>
<img :class="{'hide':!(btn.src1&&!btn.selected)}" :src="btn.src2" alt="2"/>
<p class="optioncardtip">{{btn.tip}}</p>
</div>
</div>
</p>
<script>
$(function(){
var app=new Vue({
el:'#tbar',
data: {
btns:[
{selected:true,text:'All',tip:'全部'},
{selected:false,src1:'./pic1.png',src2:'./pic2.png',tip:'營養(yǎng)'},
{selected:false,src1:'./pic1.png',src2:'./pic2.png',tip:'運(yùn)動'},
{selected:false,src1:'./pic1.png',src2:'./pic2.png',tip:'睡眠'},
{selected:false,src1:'./pic1.png',src2:'./pic2.png',tip:'情緒'},
{selected:false,src1:'./pic1.png',src2:'./pic2.png',tip:'內(nèi)分泌'}
]
},
methods: {
changetype:function(key) {
this.btns.forEach(function(e){e.selected=false});
this.btns[key].selected=true;
}
}
});
});
</script>
一點(diǎn)心得:
1,資源類的東內(nèi)容最好放在網(wǎng)頁模板里面而不是放在代碼中,這樣看起來比較方便;
2,重復(fù)性的東西一定要放在循環(huán)里面;
2.5,能完全一致就不要特殊化(都用一樣的標(biāo)簽,而不是p和img混用);
3,如果沒有外部的資源預(yù)加載(主要是圖片),盡量用css控制顯示隱藏而不用v-if進(jìn)行渲染控制;
先給你個demo,之后給你說到說到想法。
<div id="app">
<ul>
<li v-for="(item,index) in tabbar.list" :class="{selected: index == tabbar.index}" @click="tabbar.index = index">{{index === tabbar.index?item.img_h:item.img}}</li>
</ul>
</div>
new Vue({
el: '#app',
data: {
tabbar: {
list: [
{name: 'ALL',img: '1.jpg',img_h: '1_h.jpg'},
{name: 'ALL',img: '1.jpg',img_h: '1_h.jpg'},
{name: 'ALL',img: '1.jpg',img_h: '1_h.jpg'},
{name: 'ALL',img: '1.jpg',img_h: '1_h.jpg'}
],
index: 0
}
}
})
你這個看上去是一樣的,為什么不考慮用循環(huán)呢?
既然都用了0123
來表示為什么不用一個統(tǒng)一變量呢?
如果高亮和不高亮的命名有規(guī)律,那么可以判斷選中直接拼接呀? tabbar.index == index ? '_h':''
我實(shí)現(xiàn)的辦法是利用索引。
寫一個記錄索引的字段,點(diǎn)擊的時候改變那個字段的值,與當(dāng)前點(diǎn)擊的索引進(jìn)行匹配,相等則添加class。
大概是下面這樣
<nav class="st-top-category">
<span class="st-item" :class="{cur:curcate === index}" v-for="(cat,index) in categoryList" :key="index" v-on:click="changecate(index,cat.type,cat.value)">{{cat.label}}</span>
</nav>
data () {
return {
curcate: 0,
categoryList: []
}
},
methods:{
changecate(index,type,value){
this.curcate = index;
}
}
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。