小程序全屏选项卡

小程序经常会出现选项卡和全屏选项卡,这里写一下我的布局,以便日后使用。

1.创建选项卡组件

我们先创建一个components文件夹和navlist的组件


QQ截图20190718114942.png
navlist的html:
<view class='nav-box'>
   <view bindtap='clicnav' data-index="{{index}}" wx:for="{{navdata}}" wx:key="{{index}}" class='nav-list {{navindex === index?"clicknav":""}}'>
       {{item.text}}
   </view>
 </view>
navlist的js:
// components/navbar/index.js
const App = getApp();

Component({
 /**
  * 组件的属性列表
  */
 properties: {
//接收页面传过来的选项卡标签内容
   navdata: {
     type: Array,
     value: []
   },
//介绍swiper当前页面数值用于滑动swiper时改动选项卡的显示
   current: {
     type: Number,
//当父页面current值改动时触发num方法
     observer: 'num',
   }
 },

 /**
  * 组件的初始数据
  */
 data: {
//默认选择第一个
     navindex:0,
 },
 attached(){
   console.log(this.data.navdata);
 },
 /**
  * 组件的方法列表
  */
 methods: {
//选项卡的点击事件
   clicnav(e){
//index 为选项卡的data-index参数
     let index = e.currentTarget.dataset.index;
//赋值给navindex以改变点击表签的颜色
       this.setData({
         navindex: index
       });
 
     var that = this;
 //将值传给父页面,让父页面改变swiper的当前轮播
     var myEventDetail = { index: index } // detail对象,提供给事件监听函数
     this.triggerEvent('myevent', myEventDetail) //myevent自定义名称事件,父组件中使用
   },

   num(num) {
   //当父页面current值改动时修改navindex的值
     this.setData({
       navindex: num
     });
   }
 }
})
navlist的json:
{
  "component": true,
  "usingComponents": {}
}
navlist的css:
/* components/navlist/navlist.wxss */
.nav-box{
  width: 100%;
  height: 100rpx;
  display: flex;
}
.nav-list{
  flex: 1;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 32rpx;
  color: #000000;

  box-sizing: border-box;
  border-bottom: 2rpx solid #c4c4c4;
}
.clicknav{
  border-bottom: 2rpx solid #e74a45;
  color: #e74a45;
}

2.index页面调用组件

index页面调用选项卡组件并配合swiper滑动

index的html:
<view id="navheight">
   <navlist bind:myevent="toggleToast" navdata="{{navdata}}" current="{{current}}"></navlist>
 </view>
   <swiper style='height:{{swiperHeight}}' current="{{navindex}}" bindchange="navchange">
     <swiper-item>
       <scroll-view style='width:100%;height:100%;background-color:green;' scroll-y>
         我是第一页
       </scroll-view>
     </swiper-item>
     <swiper-item>
       <scroll-view style='width:100%;height:100%;background-color:yellow;' scroll-y>
         我是第二页
       </scroll-view>
     </swiper-item>
     <swiper-item>
       <scroll-view style='width:100%;height:100%;background-color:blue;' scroll-y>
         我是第三页
       </scroll-view>
     </swiper-item>
   </swiper>
index的js:
//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
//传给选项卡的标签内容
    navdata: [{
      text: "个人信息"
    }, {
      text: "个人评价"
    }, {
      text: "项目历史"
    }],
    navindex: 0,
    current: 0,
    swiperHeight: 0,
  },
  
  onLoad: function () {
    let _this = this;
    // 计算siiper到手机底部的高度赋值给swiper的height
    wx.createSelectorQuery().select('#navheight').boundingClientRect(function (rect) {
      wx.getSystemInfo({
        success(res) {
          _this.setData({
            swiperHeight: (res.windowHeight - rect.bottom) + "px"
          });
        }
      })
    }).exec()
  },
  // 获取子组件信息
  toggleToast(e) {
    let index = e.detail.index;
    this.setData({
      navindex: index
    });
  },

  // 滑动轮播事件
  navchange(e) {
    this.setData({
      current: e.detail.current
    });
  }
})
index的json:
{
  "usingComponents": {
    "navlist": "./components/navlist/navlist"
  }
}
index的css:无

至此我们的全屏选项卡就做好了

选项ka.gif
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.小程序起步 (1)点击https://mp.weixin.qq.com/wxopen/waregister?a...
    GXW_Lyon阅读 8,791评论 0 0
  • 前端开发面试题 面试题目: 根据你的等级和职位的变化,入门级到专家级,广度和深度都会有所增加。 题目类型: 理论知...
    怡宝丶阅读 7,408评论 0 7
  • 因新工作主要负责微信小程序这一块,最近的重心就移到这一块,该博客是对微信小程序整体的整理归纳以及标明一些细节点,初...
    majun00阅读 12,168评论 0 9
  • 目录:碎钻【一】碎钻【二】碎钻【三】碎钻【四】碎钻【五】碎钻【六】碎钻【七】碎钻【八】碎钻【八】碎钻【九】碎钻【十...
    兔兔啊兔兔吖阅读 1,495评论 0 0
  • 常言说:生活中不如意有八九,如何保持好的心态呢?我的观点是这么几点: 糊涂一点。经常去装傻,回答多...
    快乐大拙阅读 1,594评论 1 2

友情链接更多精彩内容