微信小程序,滑动选项卡案例

浏览1570

微信小程序,滑动选项卡:


1、设置data-current属性用于:点击当前项时,通过点击事件swichNav中处理e.dataset.current取到点击的目标值。 

2、swiper组件的current组件用于控制当前显示哪一页 

3、swiper组件绑定change事件switchTab,通过e.detail.current拿到当前页


效果图:

GIF.gif

wxml代码:

<view class="tabbox">
<scroll-view scroll-x="true" class="tab-h" scroll-left="{{scrollLeft}}">
  <view class="tab-item {{currentTab==0?'active':''}}" data-current="0" bindtap="swichNav">推荐<view></view></view>
  <view class="tab-item {{currentTab==1?'active':''}}" data-current="1" bindtap="swichNav">电视剧<view></view></view>
  <view class="tab-item {{currentTab==2?'active':''}}" data-current="2" bindtap="swichNav">电影<view></view></view>
  <view class="tab-item {{currentTab==3?'active':''}}" data-current="3" bindtap="swichNav">综艺<view></view></view>
  <view class="tab-item {{currentTab==4?'active':''}}" data-current="4" bindtap="swichNav">动漫<view></view></view>
</scroll-view>
<swiper class="tab-content" current="{{currentTab}}" duration="300" bindchange="switchTab" style="height:{{winHeight}}rpx">
  <swiper-item>
	<scroll-view scroll-y="true" class="scoll-h">
		<view class="item-ans">
		<!-- 首页2 -->
			1
		<!-- 首页2 -->
		</view>
	</scroll-view>
  </swiper-item>
  <swiper-item>
	<scroll-view scroll-y="true" class="scoll-h">
		<view class="item-ans">
		  <!-- 首页2 -->
				2
		  <!-- 首页2 -->
		</view>
	</scroll-view>
  </swiper-item>
  <swiper-item>
	<scroll-view scroll-y="true" class="scoll-h">
		<view class="item-ans">
		  <!-- 首页3 -->
					3
		  <!-- 首页3 -->
		</view>
	</scroll-view>
  </swiper-item>
  <swiper-item>
	<scroll-view scroll-y="true" class="scoll-h">
		<view class="item-ans">
		  <!-- 首页4 -->
			4
		  <!-- 首页4 -->
		</view>
	</scroll-view>
  </swiper-item>
  <swiper-item>
	<scroll-view scroll-y="true" class="scoll-h">
		<view class="item-ans">
		<!-- 首页5 -->
		  5
		<!-- 首页5 -->
		</view>
	</scroll-view>
  </swiper-item>
</swiper>
</view>

wxss代码:

.tabbox{ width: 100%; float: left; }
.tab-h {
height: 65rpx;
width: 100%;
box-sizing: border-box;
overflow: hidden;
white-space: nowrap;
}
.tabbox .tab-item {
color: #999999;
font-size: 28rpx;
display: inline-block;
float: left;
padding-top: 10rpx; padding-left: 22rpx; padding-right: 22rpx; line-height: 32rpx;
position: relative;
}
.tabbox .tab-item view{ display: none; width: 8rpx; height: 8rpx; border-radius: 50%; background: #293844; position: absolute; top: 0rpx; right: 10rpx}
.tabbox .tab-item.active {
color: #293844; font-size: 32rpx;
font-weight: bold;
position: relative;
}
.tabbox .tab-item:nth-child(1){ padding-left: 30rpx;}
.tabbox .tab-item.active view{ display: block;}

.tabbox .tab-content {
margin-top: 0rpx;
}
.tabbox .scoll-h {
height: 100%;
}

js代码:

const app = getApp()
Page({
data: {
// 切换
winHeight: "", //窗口高度
currentTab: 0, //预设当前项的值
scrollLeft: 0, //tab标题的滚动条位置
// 切换
},
// 切换
// 滚动切换标签样式
switchTab: function (e) {
this.setData({
currentTab: e.detail.current
});
this.checkCor();
},
// 点击标题切换当前页时改变样式
swichNav: function (e) {
var cur = e.target.dataset.current;
if (this.data.currentTaB == cur) {
return false;
} else {
this.setData({
currentTab: cur
})
}
},
//判断当前滚动超过一屏时,设置tab标题滚动条。
checkCor: function () {
if (this.data.currentTab > 4) {
this.setData({
scrollLeft: 300
})
} else {
this.setData({
scrollLeft: 0
})
}
},
/**
   * 生命周期函数--监听页面卸载
   */
onLoad: function () {
var that = this;
//  高度自适应
wx.getSystemInfo({
success: function (res) {
var clientHeight = res.windowHeight,
clientWidth = res.windowWidth,
rpxR = 750 / clientWidth;
var calc = clientHeight * rpxR - 180;
console.log(calc)
that.setData({
winHeight: calc
});
}
});
},
})



  • 暂无任何回答