找回密码
 立即注册
查看: 782|回复: 0

微信小程序实现页面内导航栏功能

[复制链接]

37

主题

9

回帖

46

积分

管理员

积分
46

最佳新人活跃会员热心会员推广达人宣传达人灌水之王突出贡献优秀版主荣誉管理论坛元老

发表于 2024-12-27 09:56:19 | 显示全部楼层 |阅读模式


WXML代码:
  1. <view class="appear-container">
  2.   <!-- 搜索框 -->
  3.   <view class="search">
  4.     <view class="search-box">
  5.       <image class="search-icon" src="./img/search.png" mode="" />
  6.       <input class="search-text" type="text" placeholder="请输入诉求名称" />
  7.     </view>
  8.   </view>
  9.   <!-- /搜索框 -->
  10.   <!-- 导航栏 -->
  11.   <view class="nav">
  12.     <view wx:for="{{navList}}" class="nav-item {{item.index==chosed?'nav-active':''}}" data-index="{{item.index}}" bindtap="navChange">
  13.       {{item.title}}
  14.     </view>
  15.   </view>
  16.   <!-- /导航栏 -->
  17.   <!-- 内容主体 -->
  18.   <view class="content">
  19.     <!-- 遍历数据,同时根据所选导航栏item展示数据源 -->
  20.     <view wx:for="{{chosed==0?contentList:(chosed==1?contentList1:contentList2)}}" class="content-box">
  21.       <view class="box-title">
  22.         {{item.title}}
  23.       </view>
  24.       <view class="box-date">
  25.         {{item.time}}
  26.       </view>
  27.       <view class="content-status {{item.status==0?'content-status-blue':'content-status-green'}} ">
  28.         {{item.status==0?'处理中':'已完成'}}
  29.       </view>
  30.     </view>
  31.   </view>
  32.   <!-- /内容主体 -->
  33. </view>
复制代码
WXSS代码:
  1. .appear-container {
  2.   display: flex;
  3.   flex-direction: column;
  4.   justify-items: flex-start;
  5.   height: 100vh;
  6. }
  7. /* 搜索框 */
  8. .search {
  9.   padding: 5px 20px;
  10. }

  11. .search-box {
  12.   display: flex;
  13.   justify-content: flex-start;
  14.   align-items: center;
  15.   height: 32px;
  16.   padding: 0 13px;
  17.   border-radius: 16px;
  18.   background-color: #F5F6F7;
  19. }

  20. .search-icon {
  21.   height: 16px;
  22.   width: 16px;
  23.   margin-right: 10px;
  24. }
  25. /* 导航栏 */
  26. .nav {
  27.   display: flex;
  28.   justify-content: space-evenly;
  29.   height: 44px;
  30.   line-height: 44px;
  31. }

  32. .nav-item {
  33.   height: 98%;
  34.   font-weight: 400;
  35.   font-size: 16px;
  36.   color: #8A8F99;
  37. }

  38. .nav-active {
  39.   color: #000000;
  40.   font-weight: 600;
  41.   border-bottom: 2px solid #549BF0;
  42. }
  43. /* 内容主体 */
  44. .content {
  45.   flex: 1;
  46.   padding: 0 20px;
  47.   background-color: #E9EBEF;
  48. }

  49. .content-box {
  50.   position: relative;
  51.   margin-top: 20px;
  52.   padding: 0 16px;
  53.   overflow: hidden;
  54.   background-color: #fff;
  55. }

  56. .box-title {
  57.   padding: 12px 0;
  58.   color: #2E3033;
  59.   font-weight: 520;
  60.   font-size: 16px;
  61.   word-break: break-all;
  62.   border-bottom: 1px solid #DDE0E4;
  63. }

  64. .box-date {
  65.   padding: 12px 0;
  66.   color: #6A707B;
  67.   font-weight: 400;
  68.   font-size: 14px;
  69.   word-break: break-all;
  70. }

  71. .content-status {
  72.   position: absolute;
  73.   top: 7px;
  74.   right: -20px;
  75.   height: 20px;
  76.   width: 72px;
  77.   font-weight: 400;
  78.   font-size: 12px;
  79.   line-height: 20px;
  80.   text-align: center;
  81.   transform: rotate(45deg);
  82. }
  83. /* 处理中 */
  84. .content-status-blue{
  85.   color: #549BF0;
  86.   background-color: #EEF6FF;
  87. }
  88. /* 已完成 */
  89. .content-status-green{
  90.   color: #1BBD8C;
  91.   background-color:#E9FCF6
  92. }
复制代码
WXJS代码:
  1. Page({
  2.   /**
  3.    * 页面的初始数据
  4.    */
  5.   data: {
  6.     //标签列表
  7.     navList: [
  8.       { title: '全部', index: 0 },
  9.       { title: '处理中', index: 1 },
  10.       { title: '已完成', index: 2 },
  11.     ],
  12.     // 被选中的下标
  13.     chosed: 0,
  14.     // 内容主体数据
  15.     contentList: [
  16.       {
  17.         title: '穿越到古代',
  18.         time: '2022-08-28 15:37',
  19.         status: 1
  20.       },
  21.       {
  22.         title: '当伪装太监',
  23.         time: '2022-07-18 15:37',
  24.         status: 1
  25.       },
  26.       {
  27.         title: '攻略皇帝的嫔妃',
  28.         time: '2022-09-38 15:37',
  29.         status: 0
  30.       }
  31.     ],
  32.     contentList1: [{}],
  33.     contentList2: [{}],
  34.   },

  35.   //监听事件
  36.   //导航栏切换事件
  37.   navChange(e: any) {
  38.     //todo:切换样式  更改index,以便切换主体内容
  39.     //通过更给chosed来更改样式和数据源
  40.     let index = e.currentTarget.dataset.index;
  41.     this.setData({
  42.       chosed: index,
  43.     })
  44.   },

  45.   onLoad() {
  46.     //将拿到的数据进行处理,分类
  47.     var contentList01 = this.data.contentList.filter(currentValue => { return currentValue.status == 0 })
  48.     var contentList02 = this.data.contentList.filter(currentValue => { return currentValue.status == 1 })
  49.     this.setData({
  50.       contentList1: contentList01,
  51.       contentList2: contentList02,
  52.     })
  53.   }

  54. })
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

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

本版积分规则

Archiver|手机版|小黑屋|Gitbbs

GMT+8, 2025-4-14 23:05 , Processed in 0.193630 second(s), 24 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表