微信小程序开发实现的选项卡(窗口顶部/底部TabBar)页面切换功能图文详解


Posted in Javascript onMay 14, 2019

本文实例讲述了微信小程序开发实现的选项卡(窗口顶部/底部TabBar)页面切换功能。分享给大家供大家参考,具体如下:

微信小程序开发中选项卡.在android中选项卡一般用fragment,到了小程序这里瞬间懵逼了.

总算做出来了.分享出来看看.

先看效果:

微信小程序开发实现的选项卡(窗口顶部/底部TabBar)页面切换功能图文详解

再上代码:

1.index.wxml

<!--index.wxml-->
<view class="swiper-tab">
  <view class="swiper-tab-list {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swichNav">哈哈</view>
  <view class="swiper-tab-list {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swichNav">呵呵</view>
  <view class="swiper-tab-list {{currentTab==2 ? 'on' : ''}}" data-current="2" bindtap="swichNav">嘿嘿</view>
</view>
<swiper current="{{currentTab}}" class="swiper-box" duration="300" style="height:{{winHeight - 31}}px" bindchange="bindChange">
  <!-- 我是哈哈 -->
  <swiper-item>
   <view>我是哈哈</view>
  </swiper-item>
  <!-- 我是呵呵 -->
  <swiper-item>
   <view>我是呵呵</view>
  </swiper-item>
  <!-- 我是嘿嘿 -->
  <swiper-item>
   <view>我是嘿嘿</view>
  </swiper-item>
</swiper>

2.index.wxss

/**index.wxss**/
.swiper-tab{
  width: 100%;
  border-bottom: 2rpx solid #777777;
  text-align: center;
  line-height: 80rpx;}
.swiper-tab-list{ font-size: 30rpx;
  display: inline-block;
  width: 33.33%;
  color: #777777;
}
.on{ color: #da7c0c;
  border-bottom: 5rpx solid #da7c0c;}
.swiper-box{ display: block; height: 100%; width: 100%; overflow: hidden; }
.swiper-box view{
  text-align: center;
}

3.index.js

//index.js
//获取应用实例
var app = getApp()
Page( {
 data: {
  /**
    * 页面配置
    */
  winWidth: 0,
  winHeight: 0,
  // tab切换
  currentTab: 0,
 },
 onLoad: function() {
  var that = this;
  /**
   * 获取系统信息
   */
  wx.getSystemInfo( {
   success: function( res ) {
    that.setData( {
     winWidth: res.windowWidth,
     winHeight: res.windowHeight
    });
   }
  });
 },
 /**
   * 滑动切换tab
   */
 bindChange: function( e ) {
  var that = this;
  that.setData( { currentTab: e.detail.current });
 },
 /**
  * 点击tab切换
  */
 swichNav: function( e ) {
  var that = this;
  if( this.data.currentTab === e.target.dataset.current ) {
   return false;
  } else {
   that.setData( {
    currentTab: e.target.dataset.current
   })
  }
 }
})

之前没有上传代码.这是下图的代码

demo源码点击此处本站下载

微信小程序开发实现的选项卡(窗口顶部/底部TabBar)页面切换功能图文详解

这样一个类似viewpage的顶部选项卡就出来了.

微信小程序开发中窗口底部tab栏切换页面很简单很方便.

微信小程序开发实现的选项卡(窗口顶部/底部TabBar)页面切换功能图文详解

代码:

1.app.json

//app.json
{
 "pages":[
  "pages/index/index",
  "pages/logs/logs"
 ],
 "window":{
  "backgroundTextStyle":"light",
  "navigationBarBackgroundColor": "#999999",
  "navigationBarTitleText": "tab",
  "navigationBarTextStyle":"white"
 },
  "tabBar": {
  "color": "#ccc",
  "selectedColor": "#35495e",
  "borderStyle": "white",
  "backgroundColor": "#f9f9f9",
  "list": [
   {
    "text": "首页",
    "pagePath": "pages/index/index",
    "iconPath": "images/home.png",
    "selectedIconPath": "images/home-actived.png"
   },
   {
    "text": "目录",
    "pagePath": "pages/catalogue/catalogue",
    "iconPath": "images/note.png",
    "selectedIconPath": "images/note-actived.png"
   },
   {
    "text": "我的",
    "pagePath": "pages/mine/mine",
    "iconPath": "images/profile.png",
    "selectedIconPath": "images/profile-actived.png"
   }
  ]
 }
}

pagePath是页面路径.iconPath是图片路径,icon 大小限制为40kb.
selectedIconPath:选中时的图片路径,icon 大小限制为40kb

tab Bar的最多5个,最少2个.

在pages目录下写好页面即可切换.

希望本文所述对大家微信小程序开发有所帮助。

Javascript 相关文章推荐
JS中简单的实现像C#中using功能(有源码下载)
Jan 09 Javascript
js 实现图片预加载(js操作 Image对象属性complete ,事件onload 异步加载图片)
Mar 25 Javascript
Jquery实现视频播放页面的关灯开灯效果
May 27 Javascript
js判断iframe内的网页是否滚动到底部触发事件
Mar 18 Javascript
利用jQuery实现WordPress中@的ID悬浮显示评论内容
Dec 11 Javascript
详解闭包解决jQuery中AJAX的外部变量问题
Feb 22 Javascript
详解Vue结合后台的列表增删改案例
Aug 21 Javascript
vue  directive定义全局和局部指令及指令简写
Nov 20 Javascript
浅谈发布订阅模式与观察者模式
Apr 09 Javascript
vue搜索和vue模糊搜索代码实例
May 07 Javascript
JavaScript页面倒计时功能完整示例
May 15 Javascript
JS document内容及样式操作完整示例
Jan 14 Javascript
详解jenkins自动化部署vue
May 14 #Javascript
基于JS实现前端压缩上传图片的实例代码
May 14 #Javascript
JavaScript如何实现元素全排列实例代码
May 14 #Javascript
微信小程序实现多选框全选与取消全选功能示例
May 14 #Javascript
js中arguments对象的深入理解
May 14 #Javascript
ios中视频的最后一桢问题解决
May 14 #Javascript
详解可以用在VS Code中的正则表达式小技巧
May 14 #Javascript
You might like
用PHP为SHOPEX增加日志功能代码
2010/07/02 PHP
Laravel 5.4.36中session没有保存成功问题的解决
2018/02/19 PHP
jQuery中fadeIn、fadeOut、fadeTo的使用方法(图片显示与隐藏)
2013/05/08 Javascript
对js关键字命名的疑问介绍
2014/04/25 Javascript
javascript里使用php代码实例
2014/12/13 Javascript
JSON格式的键盘编码对照表
2015/01/29 Javascript
javascript实现类似java中getClass()得到对象类名的方法
2015/07/27 Javascript
js时钟翻牌效果实现代码分享
2020/07/31 Javascript
基于JS判断iframe是否加载成功的方法(多种浏览器)
2016/05/13 Javascript
JS简单实现点击跳转登陆邮箱功能的方法
2017/10/31 Javascript
vue 组件中slot插口的具体用法
2018/04/03 Javascript
node跨域转发 express+http-proxy-middleware的使用
2018/05/31 Javascript
node.js调用C++函数的方法示例
2018/09/21 Javascript
微信小程序使用map组件实现解析经纬度功能示例
2019/01/22 Javascript
详解Vue调用手机相机和相册以及上传
2019/05/05 Javascript
基于ssm框架实现layui分页效果
2019/07/27 Javascript
Vue.js组件通信之自定义事件详解
2019/10/19 Javascript
JavaScript中跨域问题的深入理解
2021/03/04 Javascript
[41:17]完美世界DOTA2联赛PWL S3 access vs CPG 第二场 12.13
2020/12/17 DOTA
简单介绍Python中的round()方法
2015/05/15 Python
Android基于TCP和URL协议的网络编程示例【附demo源码下载】
2018/01/23 Python
Python import与from import使用及区别介绍
2018/09/06 Python
Pycharm以root权限运行脚本的方法
2019/01/19 Python
解决Pycharm 导入其他文件夹源码的2种方法
2020/02/12 Python
Python的in,is和id函数代码实例
2020/04/18 Python
filter使用python3代码进行迭代元素的实例详解
2020/12/03 Python
HTML5 Canvas的常用线条属性值总结
2016/03/17 HTML / CSS
canvas绘制树形结构可视图形的实现
2020/04/03 HTML / CSS
世界上最悠久的自行车制造商:Ribble Cycles
2017/03/18 全球购物
屈臣氏越南官网:Watsons越南
2021/01/14 全球购物
群胜软件Java笔试题
2012/09/29 面试题
送给程序员的20个Java集合面试问题
2014/08/06 面试题
团日活动总结书
2014/05/08 职场文书
党员教师四风问题对照检查材料
2014/09/26 职场文书
护士2014年终工作总结
2014/11/11 职场文书
一篇文章学会Vue中间件管道
2021/06/20 Vue.js