现在需要一个和底部tabbar的东西一模一样的,要求就是点击某个,它的图片和背景色改为选中的样式,其它的便会默认样式。
现在我已经做了出来,可以实现这样的功能,但是实现的过程实在是不堪入目。下面上代码
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">营养</p>
</div>
<div class="optioncard optioncardlist">
<img :src="[is2?g:h]" alt="" srcset="" :class="[is2?d:c]" @click="changetype(2)">
<p class="optioncardtip">运动</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">内分泌</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)
}
},
这个是部分代码,其中有些无关紧要。现在的基本思路就是加载2套不同样式和src,然后每次点击之后判定进行切换,选中的进行加载,其它的返回默认值。但是这样的代码实在繁琐,感觉程序员不应该写出这样的代码,说起来真挺丢人的,谁有很好的思路可以提供一下。非常感谢。
代码大概是这样的
一点心得:
1,资源类的东内容最好放在网页模板里面而不是放在代码中,这样看起来比较方便;
2,重复性的东西一定要放在循环里面;
2.5,能完全一致就不要特殊化(都用一样的标签,而不是p和img混用);
3,如果没有外部的资源预加载(主要是图片),尽量用css控制显示隐藏而不用v-if进行渲染控制;