当前位置:网站首页>如何用list组件实现tabbar标题栏
如何用list组件实现tabbar标题栏
2022-07-02 07:16:00 【华为开发者论坛】
需求描述
当我们不想用tabbar组件实现标题栏时,还有什么其他选择么?
需求分析
答案是list组件,通过list组件设置样式flex-direction: row;可以满足横向展示标题的效果,同时,增加点击标题时的样式切换可以满足动态展示的效果。
假设定义一个数组,用来表示标题内容,该数组的元素需要同时包含标题的名字和点击的颜色,当我们点击对应标题时,可以将点击的标题颜色设置为红色,其他未被点击的标题设置为黑色。
效果如下(点击“经济”和“音乐”标题):
重点代码如下:
<list class="list" for="{
{listData}}">
<list-item class="list-item" type="item">
<text class="text_{
{$item.color}}" onclick="changeColor($idx,$item.color)">{
{ $item.name }}</text>
</list-item>
</list>
listData: [
{ color: "black", name: "体育" },
{ color: "black", name: "政治" },
{ color: "black", name: "社会" },
{ color: "black", name: "经济" },
{ color: "black", name: "文学" },
{ color: "black", name: "音乐" },
{ color: "black", name: "美术" },
{ color: "black", name: "时事" },
]
changeColor: function ($idx, color, evt) {
if (this.num === $idx) {
this.listData[$idx].color = "red"
}
else {
this.listData[$idx].color = "red"
this.listData[this.num].color = "black"
this.num = $idx
}
console.log("changeColor" + $idx);
}
解决方案
<template>
<!-- Only one root node is allowed in template. -->
<div class="container">
<div>
<list class="list" for="{
{listData}}">
<list-item class="list-item" type="item">
<text class="text_{
{$item.color}}" onclick="changeColor($idx,$item.color)">{
{ $item.name }}</text>
</list-item>
</list>
</div>
</div>
</template>
<style>
.list {
flex-direction: row;
height: 50px;
}
.list-item {
width: 80px;
margin-left: 10px;
}
.container {
flex-direction: column;
}
.text {
font-size: 30px;
}
.text_red {
color: #dc143c;
}
.text_black {
color: rgba(0, 0, 0, 0.54);
}
</style>
<script>
module.exports = {
data: {
num: 0,
color: '',
listData: [
{ color: "black", name: "体育" },
{ color: "black", name: "政治" },
{ color: "black", name: "社会" },
{ color: "black", name: "经济" },
{ color: "black", name: "文学" },
{ color: "black", name: "音乐" },
{ color: "black", name: "美术" },
{ color: "black", name: "时事" },
]
},
onInit() {
},
changeColor: function ($idx, color, evt) {
if (this.num === $idx) {
this.listData[$idx].color = "red"
}
else {
this.listData[$idx].color = "red"
this.listData[this.num].color = "black"
this.num = $idx
}
console.log("changeColor" + $idx);
}
}
</script>
欲了解更多详情,请参见:
蓝牙接口介绍:
边栏推荐
- 2. Hacking lab script off [detailed writeup]
- JSP webshell免殺——JSP的基礎
- 1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
- MySQL keyword
- Flink calculates topn hot list in real time
- UVM - usage of common TLM port
- js promise.all
- MySQL lethal serial question 4 -- are you familiar with MySQL logs?
- 全网显示 IP 归属地,是怎么实现的?
- UVM - configuration mechanism
猜你喜欢
随机推荐
PCL 从一个点云中提取一个子集
Point cloud projection picture
SPSS做Shapiro-Wilk正态分析
全网显示 IP 归属地,是怎么实现的?
从MediaRecord录像中读取H264参数
PCL之K-d树与八叉树
学习open62541 --- [66] UA_String的生成方法
华为联机对战服务玩家掉线重连案例总结
转换YV12到RGB565图像转换,附YUV转RGB测试
从.bag文件中读取并保存.jpg图片和.pcd点云
stm32和電機開發(上比特系統)
618再次霸榜的秘密何在?耐克最新财报给出答案
AppGallery Connect场景化开发实战—图片存储分享
01-spooldir
[TS] 1368 seconds understand typescript generic tool types!
Use WinDbg to statically analyze dump files (summary of practical experience)
PCL 投影点云
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
HDU1228 A + B(map映射)
Importing tables from sqoop