微信小程序开发实现的选项卡(窗口顶部/底部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 相关文章推荐
解决jquery .ajax 在IE下卡死问题的解决方法
Oct 26 Javascript
jQuery层次选择器选择元素使用介绍
Apr 18 Javascript
javascript在子页面中函数无法调试问题解决方法
Jan 17 Javascript
jQuery实现鼠标经过图片变亮其他变暗效果
May 08 Javascript
AngularJS学习笔记之基本指令(init、repeat)
Jun 16 Javascript
AngularJS入门教程之过滤器详解
Aug 19 Javascript
js实现点击每个li节点,都弹出其文本值及修改
Dec 15 Javascript
JavaScript实现按键精灵的原理分析
Feb 21 Javascript
jquery加载单文件vue组件的方法
Jun 20 jQuery
JavaScript实现数组全排列、去重及求最大值算法示例
Jul 30 Javascript
用原生 JS 实现 innerHTML 功能实例详解
Apr 03 Javascript
实例分析javascript中的异步
Jun 02 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
实例(Smarty+FCKeditor新闻系统)
2007/01/02 PHP
PHP5中新增stdClass 内部保留类
2011/06/13 PHP
WordPress导航菜单的滚动和淡入淡出效果的实现要点
2015/12/14 PHP
基于ThinkPHP5.0实现图片上传插件
2017/09/25 PHP
php实现的三个常用加密解密功能函数示例
2017/11/06 PHP
ThinkPHP整合datatables实现服务端分页的示例代码
2018/02/10 PHP
PHP实现的CURL非阻塞调用类
2018/07/26 PHP
找到了一篇jQuery与Prototype并存的冲突的解决方法
2007/08/29 Javascript
用JavaScript实现动画效果的方法
2013/07/20 Javascript
javascript包装对象实例分析
2015/03/27 Javascript
JS判断页面是否出现滚动条的方法
2015/07/17 Javascript
AngularJS教程之MVC体系结构详解
2016/08/16 Javascript
jQuery学习笔记——jqGrid的使用记录(实现分页、搜索功能)
2016/11/09 Javascript
JavaScript自动点击链接 防止绕过浏览器访问的方法
2017/01/19 Javascript
vue.js中指令Directives详解
2017/03/20 Javascript
vue2中filter()的实现代码
2017/07/09 Javascript
ES6学习教程之模板字符串详解
2017/10/09 Javascript
jQuery实现鼠标响应式透明度渐变动画效果示例
2018/02/13 jQuery
微信小程序实现倒计时调用相机自动拍照功能
2018/06/10 Javascript
JS一次前端面试经历记录
2020/03/19 Javascript
Node Mongoose用法详解【Mongoose使用、Schema、对象、model文档等】
2020/05/13 Javascript
[02:43]2014DOTA2国际邀请赛 官方Alliance战队纪录片
2014/07/14 DOTA
python实现apahce网站日志分析示例
2014/04/02 Python
Python三元运算实现方法
2015/01/12 Python
浅谈Python中的闭包
2015/07/08 Python
python版本的仿windows计划任务工具
2018/04/30 Python
python字符串string的内置方法实例详解
2018/05/14 Python
HTML5重塑Web世界它将如何改变互联网
2012/12/17 HTML / CSS
意大利会呼吸的鞋:Geox健乐士
2017/02/12 全球购物
社区消防工作实施方案
2014/03/21 职场文书
生物科学专业毕业生求职信
2014/06/02 职场文书
论群众路线学习笔记
2014/11/06 职场文书
2014年财政局工作总结
2014/12/09 职场文书
《赵州桥》教学反思
2016/02/17 职场文书
幼儿园2016年圣诞活动总结
2016/03/31 职场文书
div与span之间的区别与使用介绍
2021/12/06 HTML / CSS