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

2019-11-19   阅读:2778   分类:后端    标签: 微信小程序

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


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
});
}
});
},
})


【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://tpxhm.com/adetail/258.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×