微信小程序示例:顶部导航栏

目标:
用户点击导航标签,显示不同的页面。
步骤:

自定义页面(布局)、js控制页面显示。

效果图:


代码:

wxml

<!--pages/swiperTab/swiperTab.wxml-->
<view>  
  <!--顶部导航栏-->  
  <view class="swiper-tab">  
    <view class="tab-item {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swichNav">Tab1</view>  
    <view class="tab-item {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swichNav">Tab2</view>  
    <view class="tab-item {{currentTab==2 ? 'on' : ''}}" data-current="2" bindtap="swichNav">Tab3</view>  
  </view>  
  <!--内容主体-->  
  <swiper class="swiper" current="{{currentTab}}" duration="200" bindchange="swiperChange">  
    <swiper-item>  
      <view>我是tab1</view>  
    </swiper-item>  
    <swiper-item>  
      <view>我是tab2</view>  
    </swiper-item>  
    <swiper-item>  
      <view>我是tab3</view>  
    </swiper-item>  
  </swiper>  
</view>  
wxss
/* pages/swiperTab/swiperTab.wxss */
.page {  
  margin-left: 10rpx;  
  margin-right: 10rpx;  
}  
.swiper-tab {  
  display: flex;  
  flex-direction: row;  
  line-height: 80rpx;  
  border-bottom: 2rpx solid #777;  
}  
.tab-item {  
  width: 33.3%;  
  text-align: center;  
  font-size: 15px;  
  color: #777;  
}  
.swiper {  
  height: 1100px;  
  background: #dfdfdf;  
}  
.on {  
  color: blue;  
  border-bottom: 5rpx solid blue;  
}  
js
// pages/swiperTab/swiperTab.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 导航标签选择1)
   */
  swichNav: function (e) {
    console.log(e);
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
    }
  },
  /**
   * 导航页面显示2)
   */
  swiperChange: function (e) {
    console.log(e);
    this.setData({
      currentTab: e.detail.current,
    })
  }
})
tip:

1、参考文档:https://developers.weixin.qq.com/miniprogram/dev/component/swiper.html

2、小程序的导航栏也可以在app.json配置

     参考文档:https://developers.weixin.qq.com/miniprogram/dev/framework/config.html


猜你喜欢

转载自blog.csdn.net/qq_16769857/article/details/80015962
今日推荐