微信小程序开发记录(三)顶部导航栏切换实现

微信小程序实现顶部导航切换效果可用swiper来实现,效果如下:

代码如下:

WXML:

<view class="page">


<!--顶部导航栏-->

<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:

.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:

Page({

data: {

// tab切换

currentTab: 0,

},

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,

})

}

},

swiperChange: function (e) {

console.log(e);

this.setData({

currentTab: e.detail.current,

})


},

onLoad: function (options) {

// 生命周期函数--监听页面加载

},

onReady: function () {

// 生命周期函数--监听页面初次渲染完成

},

onShow: function () {

// 生命周期函数--监听页面显示

},

onHide: function () {

// 生命周期函数--监听页面隐藏

},

onUnload: function () {

// 生命周期函数--监听页面卸载

},

onPullDownRefresh: function () {

// 页面相关事件处理函数--监听用户下拉动作

},

onReachBottom: function () {

// 页面上拉触底事件的处理函数

},

onShareAppMessage: function () {

// 用户点击右上角分享

return {

title: 'title', // 分享标题

desc: 'desc', // 分享描述

path: 'path' // 分享路径

}

}

})

 

  • 13
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值