找回密码
 立即注册
搜索

如何用list组件实现tabbar标题栏

1
回复
373
查看
[复制链接]

46

主题

47

帖子

465

积分

 楼主| 2021-7-23 12:40:59 显示全部楼层 |阅读模式

需求描述

当我们不想用tabbar组件实现标题栏时,还有什么其他选择么?


回复

使用道具 举报

23

主题

142

帖子

825

积分

2021-7-23 12:47:28 显示全部楼层

需求分析

答案是list组件,通过list组件设置样式flex-direction: row;可以满足横向展示标题的效果,同时,增加点击标题时的样式切换可以满足动态展示的效果。

假设定义一个数组,用来表示标题内容,该数组的元素需要同时包含标题的名字和点击的颜色,当我们点击对应标题时,可以将点击的标题颜色设置为红色,其他未被点击的标题设置为黑色。

效果如下(点击“经济”和“音乐”标题):

image.png


image.png

重点代码如下:

      <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 ($idxcolorevt) {

      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-directionrow;

    height50px;

  }

  .list-item {

    width80px;

    margin-left10px;

  }

  .container {

    flex-directioncolumn;

  }

  .text {

    font-size30px;

  }

  .text_red {

    color#dc143c;

  }

  .text_black {

    colorrgba(0000.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 ($idxcolorevt) {

      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>

 

欲了解更多详情,请参见:

蓝牙接口介绍:

https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-api-bluetooth-0000001074629693


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册