当前位置:Gxlcms > JavaScript > 微信小程序开发之实现选项卡(窗口顶部TabBar)页面切换

微信小程序开发之实现选项卡(窗口顶部TabBar)页面切换

时间:2021-07-01 10:21:17 帮助过:81人阅读

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

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

先看效果:

再上代码:

1.index.wxml

  1. <!--index.wxml-->
  2. <view class="swiper-tab">
  3. <view class="swiper-tab-list {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swichNav">哈哈</view>
  4. <view class="swiper-tab-list {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swichNav">呵呵</view>
  5. <view class="swiper-tab-list {{currentTab==2 ? 'on' : ''}}" data-current="2" bindtap="swichNav">嘿嘿</view>
  6. </view>
  7. <swiper current="{{currentTab}}" class="swiper-box" duration="300" style="height:{{winHeight - 31}}px" bindchange="bindChange">
  8. <!-- 我是哈哈 -->
  9. <swiper-item>
  10. <view>我是哈哈</view>
  11. </swiper-item>
  12. <!-- 我是呵呵 -->
  13. <swiper-item>
  14. <view>我是呵呵</view>
  15. </swiper-item>
  16. <!-- 我是嘿嘿 -->
  17. <swiper-item>
  18. <view>我是嘿嘿</view>
  19. </swiper-item>
  20. </swiper>

2.indexwxss

  1. /**indexwxss**/
  2. swiper-tab{
  3. width: 100%;
  4. border-bottom: 2rpx solid #777777;
  5. text-align: center;
  6. line-height: 80rpx;}
  7. swiper-tab-list{ font-size: 30rpx;
  8. display: inline-block;
  9. width: 33%;
  10. color: #777777;
  11. }
  12. on{ color: #da7c0c;
  13. border-bottom: 5rpx solid #da7c0c;}
  14. swiper-box{ display: block; height: 100%; width: 100%; overflow: hidden; }
  15. swiper-box view{
  16. text-align: center;
  17. }

3.index.js

  1. //index.js
  2. //获取应用实例
  3. var app = getApp()
  4. Page( {
  5. data: {
  6. /**
  7. * 页面配置
  8. */
  9. winWidth: 0,
  10. winHeight: 0,
  11. // tab切换
  12. currentTab: 0,
  13. },
  14. onLoad: function() {
  15. var that = this;
  16. /**
  17. * 获取系统信息
  18. */
  19. wxgetSystemInfo( {
  20. success: function( res ) {
  21. thatsetData( {
  22. winWidth: reswindowWidth,
  23. winHeight: reswindowHeight
  24. });
  25. }
  26. });
  27. },
  28. /**
  29. * 滑动切换tab
  30. */
  31. bindChange: function( e ) {
  32. var that = this;
  33. thatsetData( { currentTab: edetailcurrent });
  34. },
  35. /**
  36. * 点击tab切换
  37. */
  38. swichNav: function( e ) {
  39. var that = this;
  40. if( thisdatacurrentTab === etargetdatasetcurrent ) {
  41. return false;
  42. } else {
  43. thatsetData( {
  44. currentTab: etargetdatasetcurrent
  45. })
  46. }
  47. }
  48. })

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

人气教程排行