当前位置:网站首页>如何用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>
欲了解更多详情,请参见:
蓝牙接口介绍:
边栏推荐
- JSP webshell免杀——JSP的基础
- nodejs+express+mysql简单博客搭建
- The URL in the RTSP setup header of the axis device cannot take a parameter
- Flutter环境配置保姆级教程,让doctor一绿到底
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
- UVM - configuration mechanism
- [TS] 1368 seconds understand typescript generic tool types!
- JS settimeout() and interview questions
- 【AppLinking实战案例】通过AppLinking分享应用内图片
- Analysis of hot spots in AI technology industry
猜你喜欢
Analysis of hot spots in AI technology industry
Shapiro Wilk normal analysis by SPSS
SUS系统可用性量表
Redis set password
13. Semaphore critical zone protection
【AGC】构建服务3-认证服务示例
JSP webshell免殺——JSP的基礎
Easyexcel, a concise, fast and memory saving excel processing tool
使用Windbg静态分析dump文件(实战经验总结)
Ks009 implement pet management system based on SSH
随机推荐
2. Hacking lab script off [detailed writeup]
MySQL数据库远程访问权限设置
12. Process synchronization and semaphore
What is the significance of the college entrance examination
PCL 投影点云
Win11 arm系统配置.net core环境变量
UVM——Callback
Considerations for Apache deploying static web page projects
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
Flutter——Canvas自定义曲线图
nodejs+express+mysql简单博客搭建
简洁、快速、节约内存的Excel处理工具EasyExcel
[SUCTF2018]followme
对话吴纲:我为什么笃信“大国品牌”的崛起?
Windows环境MySQL8忘记密码文件解决方案
Sus system availability scale
Ks009 implement pet management system based on SSH
What are the popular frameworks for swoole in 2022?
[TS] 1368 seconds understand typescript generic tool types!
《MySQL 8 DBA基础教程》简介