一、前言

小程序开发中,有很多封装好的控件供开发者使用,但是,很常见的tab选项卡居然没有,只能自己搞一个。

实现原理也很简单,无非是用给view(tab)设置一个点击事件bintap,并且给view(tab)一个data-idx索引,根据当前index来改变tab的状态并决定swiper显示那个内容,改变swiper的内容只需要改变swiper的current就好。

二、效果

三、实现

wxml

<view class='ticket-container'>
<scroll-view scroll-y bindscrolltoupper="upper" bindscrolltolower="lower" bindscroll="scroll" scroll-into-view="{{toView}}"
scroll-top="{{scrollTop}}">
<!--Tab布局-->
<view class='title'>
<view class="{{0 == currentIndex ? 'title-sel-selected' : 'title-sel'}}" bindtap='titleClick' data-idx='0'>
<text>未使用</text>
<hr class="line-style"/>
</view> <view class="{{1 == currentIndex ? 'title-sel-selected' : 'title-sel'}}" bindtap='titleClick' data-idx='1'>
<text>已使用</text>
<hr class="line-style"/>
</view>
<view class="{{2 == currentIndex ? 'title-sel-selected' : 'title-sel'}}" bindtap='titleClick' data-idx='2'>
<text>已过期</text>
<hr class="line-style" />
</view>
</view> <!--内容布局-->
<swiper class='swiper' bindchange='pagechange' current='{{currentIndex}}'>
<swiper-item class='swiper'>
<view wx:for="{{firstList}}" wx:for-item="item" class='record-item'>
<view class='name'>
<span>名称:{{item.name}}</span>
<span>面额:{{item.money}}</span>
</view>
</view>
</swiper-item>
<swiper-item class='swiper' class='swiper'>
<view wx:for="{{secondList}}" wx:for-item="item" class='record-item'>
<view class='name'>
<span>名称:{{item.name}}</span>
<span>面额:{{item.money}}</span>
</view>
</view>
</swiper-item>
<swiper-item class='swiper' class='swiper'>
<view wx:for="{{thirdList}}" wx:for-item="item" class='record-item'>
<view class='name'>
<span>名称:{{item.name}}</span>
<span>面额:{{item.money}}</span>
</view>
</view>
</swiper-item>
</swiper>
</scroll-view>
</view>

js文件

// pages/mine/mine.js
Page({ /**
* 页面的初始数据
*/
data: {
currentIndex: 0,
"firstList": [{ name: 'w券1', money: '5.00' }, { name: 'w券2', money: '50.00'}],
"secondList": [{ name: 'y券1', money: '10.00' }, { name: 'y券2', money: '20.00' }],
"thirdList": [{ name: 'g券1', money: '30.00' }, { name: 'g券2', money: '40.00' }],
}, //swiper切换时会调用
pagechange: function (e) {
if ("touch" === e.detail.source) {
let currentPageIndex = this.data.currentIndex
currentPageIndex = (currentPageIndex + 1) % 3
this.setData({
currentIndex: currentPageIndex
})
}
},
//用户点击tab时调用
titleClick: function (e) {
let currentPageIndex =
this.setData({
//拿到当前索引并动态改变
currentIndex: e.currentTarget.dataset.idx
})
} })

wxss文件

.container {
height: 100%;
min-height: 100%;
display: flex;
flex-direction: column;
box-sizing: border-box;
} .title {
width: 100%;
height: 88rpx;
background: white;
display: flex;
align-items: center;
justify-content: space-around;
} .title-sel {
color: #24272c;
font-size: 32rpx;
display: flex;
flex-direction: column;
align-items: center;
} .title-sel .line-style{
background: #fff;
height: 6rpx;
width: 40rpx;
position: relative;
margin-top: 10rpx;
} .title-sel-selected{
color: #006bff;
font-size: 32rpx;
display: flex;
flex-direction: column;
align-items: center;
}
.title-sel-selected .line-style{
background: #006bff;
height: 6rpx;
width: 90rpx;
position: relative;
margin-top: 10rpx;
} .swiper {
width: 90%;
flex: 1;
overflow: scroll;
margin: 0 auto;
} .record-item {
margin-top: 10rpx;
background-color: white;
padding-bottom: 5rpx;
padding-top: 5rpx;
font-size: 30rpx;
} .name{
display: flex;
justify-content: space-between;
border: 1px solid #eee;
padding: 2vh;
border-radius: 10rpx
}

四、参考官方文档

那么当内容改变时,我们也要改变tab选项卡的状态,这时候我们给swiper来一个bindchange,同样是官方文档

【微信小程序】微信小程序-实现tab的更多相关文章

  1. 微信小程序横版日历,tab栏

    代码地址如下:http://www.demodashi.com/demo/14243.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  2. 微信小程序基于swiper组件的tab切换

    代码地址如下:http://www.demodashi.com/demo/14010.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  3. 微信小程序 微信支付

    微信小程序前端自处理: //时间戳 timeStamp() { return parseInt(new Date().getTime() / 1000) + '' }, //随机数 randomStr ...

  4. 微信小程序——智能小秘“遥知之”源码分享(语义理解基于olami)

    微信小程序智能生活小秘书开发详解 >>>>>>>>>>>>>>>>>>>>> ...

  5. 微信公众号支付|微信H5支付|微信扫码支付|小程序支付|APP微信支付解决方案总结

    最近负责的一些项目开发,都用到了微信支付(微信公众号支付.微信H5支付.微信扫码支付.APP微信支付).在开发的过程中,在调试支付的过程中,或多或少都遇到了一些问题,今天总结下,分享,留存. 先说注意 ...

  6. 微信小程序+“芝麻小客服”可设自动关注公众号,助力运营闭环

    微信小程序全面上线已经接近1年的时间,从最初的"用完即走"理念到2017年总计更新开放60余次的功能创新,微信小程序不一定会爆发下一次的红利,但绝对是微信生态中重要的一环. 芝麻小 ...

  7. 微信小程序—微信自动退款

    微信小程序—微信自动退款 一.业务背景 微信自动退款串接基于酷客多小程序商城系统,为方便财务人员进行订单退款而开发,将酷客多小程序系统财务退款流程和微信退款系统打通.实现一个系统管理运营. 二.业务流 ...

  8. 玩玩小程序:使用 WebApi 交互打造原生的微信小程序 - 图灵小书架

    使用 WebApi 交互打造原生的微信小程序 - 图灵小书架 目录 介绍 源码地址 扫一扫体验 代码分析 其它相关信息(互联网搜集) 介绍 定时抓取图灵社区官网的首页.最热.推荐和最新等栏目的相关图书 ...

  9. 微信小程序+微信管理后台+微信用户前台

    代码地址如下:http://www.demodashi.com/demo/15043.html #### 微信小程序+微信管理后台+微信用户前台 #### 产品介绍 基础功能开发:景区微信地图导游.天 ...

  10. 浅析微信支付:微信支付简单介绍(小程序、公众号、App、H5)

    本文是[浅析微信支付]系列文章的第二篇,主要讲解一下普通商户接入的支付方式以及其中的不同之处. 上篇文章讲了本系列的大纲,没有看过的朋友们可以看一下. 浅析微信支付:前篇大纲 微信支付是集成在微信客户 ...

随机推荐

  1. 无辜的RAD(RAD是让你去创造和使用可复用的组件,不是让程序员“变白痴”)good

    无辜的RAD 2005-3-21 说实话,RAD很无辜.从出生的那天其就被骂,天天被指着鼻子说“不就是拖个控件嘛”,就好像当年说学电脑“不就是插个鼠标嘛”.也怪程序员大都天性犯贱,就爱一遍又一便的写基 ...

  2. 关于 win32 下磁盘的遍历方法

    最近要写个在线专杀的东东,虽然是专杀(本来只要清除几个特定的文件和杀几个特定的进程,然后把用户的注册表恢复正常,很多病毒木马最喜欢干的一件事情就是写 映像劫持 然后机器一重启,安全相关的软件全部玩完了 ...

  3. Windows下获取逻辑cpu数量和cpu核数量(用GetLogicalProcessorInformation,从XP3才开始有的API)

    代码可在Windows NT下正常运行 具体API说明请参照如下文档: GetLogicalProcessorInformation 点击打开链接 点击打开链接 点击打开链接 typedef BOOL ...

  4. Google地图下载工具代码

    // // Google Map Tiles Downloader in C# by coolypf // No rights reserved, neither warranty nor guara ...

  5. Java集合框架Collection(1)ArrayList的三种遍历方法

    ArrayList是java最重要的数据结构之一,日常工作中经常用到的就是ArrayList的遍历,经过总结,发现大致有三种,上代码: package com.company; import java ...

  6. 错误处理之try、catch、finally中的return、throw执行顺序。

    今天遇到一个让人无语的代码块 try { bilSheetService.syncUser(bilWebseviceLog, userId, optType); }catch (Exception e ...

  7. 高并发 Nginx+Lua OpenResty系列(3)——模块指令

    Nginx Lua 模块指令 Nginx共11个处理阶段,而相应的处理阶段是可以做插入式处理,即可插拔式架构:另外指令可以在http.server.server if.location.locatio ...

  8. Spring Boot 集成配置 HTTPS

    这是泥瓦匠的第108篇原创 文章工程: * JDK 1.8 * Maven 3.5.2 * Spring Boot 1.5.9.RELEASE ## 一.HTTPS 是什么 问:什么是HTTP? 答: ...

  9. 【dateFormatSymbols】JAVA特殊日期格式转换

    记录:特殊日期格式转换,如将yyyyMMdd转为01MAY2019 public static final String DATE_VIP_FORMAT = "yyyyMMdd"; ...

  10. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...