学无先后,达者为师

网站首页 编程语言 正文

uview 使用scroll-view以及swiper 做tabs

作者:bug生产小能手 更新时间: 2022-01-09 编程语言
<!-- 热销榜单列表 -->
<template>
	<view class="body-view">
		<view class="header-wrap">
			<view class="titleBg"></view>
			<scroll-view class="top-menu-view" scroll-x="true" :scroll-left="scrollLeft">
				<block v-for="(menuTab,index) in menuTabs" :key="index">
					<view class="menu-one-view" v-bind:id="'tabNum'+index" @click="swichMenu(index)">
						<view :class="[currentTab==index ? 'menu-one-act' : 'menu-one']">
							<view class="menu-one-txt">{{menuTab.name}}</view>
							<view class="menu-one-bottom"><view class="menu-one-bottom-color"></view></view>
						</view>
					</view>
				</block>
			</scroll-view>
		</view>
		<swiper :current="currentTab" class="swiper-box-list" duration="300" @change="swiperChange">
			<block v-for="(swiperDate,index1) in menuTabs" :key="index1">
				<swiper-item class="u-p-t-38">
					<scroll-view class="swiper-one-list" scroll-y="true" @scrolltolower="loadMore(index1)">
						<view class="swiper-list-entity">
							<!-- <view>{{swiperDate.name}}</view> -->
							<productCard></productCard>
						</view>
					</scroll-view>
				</swiper-item>
			</block>
		</swiper>
	</view>
</template>

<script>
	import productCard from "./product-card.vue"  //滚动区域列表
	export default {
		data() {
			return {
				scrollLeft: 0,
				isClickChange: false,
				currentTab: 0,
				menuTabs: [{
					name: '热销榜'
				}, {
					name: '口碑榜'
				}, {
					name: '收藏榜'
				}, {
					name: '销量榜'
				}],
				swiperDateList: [] //给滚动列表传list
			}
		},
		components:{productCard},
		onLoad: function() {
			//初始化数据
			// for (var i = 0; i < this.swiperDateList.length; i++) {
			// 	this.getDateList(i);
			// }
		},
		methods: {
			swichMenu: async function(current) { //点击其中一个 menu
				if (this.currentTab == current) {
					return false;
				} else {
					this.currentTab = current;
					this.setScrollLeft(current);
				}
			},
			swiperChange: async function(e) {
				let index = e.target.current;
				this.setScrollLeft(index);
				this.currentTab = index;
			},
			setScrollLeft: async function(tabIndex) {
				let leftWidthSum = 0;
				for (var i = 0; i <= tabIndex; i++) {
					let nowElement = await this.getWidth('tabNum' + i);
					leftWidthSum = leftWidthSum + nowElement.width;
				}
				let winWidth = uni.getSystemInfoSync().windowWidth;
				this.scrollLeft = leftWidthSum > winWidth ? (leftWidthSum - winWidth) : 0
			},
			getWidth: function(id) { //得到元素的宽高
				return new Promise((res, rej) => {
					uni.createSelectorQuery().select("#" + id).fields({
						size: true,
						scrollOffset: true
					}, (data) => {
						res(data);
					}).exec();
				})
			},
			loadMore: function(tabIndex) {
				console.log('正在加载更多数据。。。')
				this.getDateList(tabIndex);
			},
			getDateList: function(tabIndex) {
				console.log(tabIndex)
				// for (var i = 0; i < 20; i++) {
				// 	var entity = this.menuTabs[tabIndex].name + (this.swiperDateList[tabIndex].length);
				// 	this.swiperDateList[tabIndex].push(entity);
				// }
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		width: 100%;
		height: 100%;
		display: flex;
		flex-wrap: wrap;
		align-items: flex-start;
		justify-content: center;
	}
	.body-view {
		display: flex;
		flex: 1;
		flex-direction: column;
		overflow: hidden;
		height: 100%;
		width: 100%;
		align-items: flex-start;
		justify-content: center;
	}
	.header-wrap{
		display: flex;
		flex-direction:column;
		white-space: nowrap;
		width: 100%;
		height: 300rpx;
		background-color: #8799A3;
		// background:url(../../../imgs/hotProduct/headerBg.png) no-repeat;
		// background-size: 100% 100%;
		.titleBg{
			width: 100%;
			height: 160rpx;
		}
	}
	.top-menu-view {
		display: flex;
		white-space: nowrap;
		width: 100%;
		height: 80rpx;
		/* 在这里设置导航条高度 */
	}
	.top-menu-view .menu-one-view {
		display: inline-block;
		white-space: nowrap;
		height: 100%;
	}
	.top-menu-view .menu-one-view .menu-one {
		/* 在这里写 单个按钮样式 */
		margin-right: 74rpx;
		position: relative;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30px;
		font-family: "PingFangSC-Regular, PingFang SC";
		font-weight: 400;
		color: #D8D8D8;
	}
	.top-menu-view .menu-one-view .menu-one:nth-child(1) {
		margin-left: 60rpx;
	}
	.top-menu-view .menu-one-view .menu-one .menu-one-txt {
		height: 40rpx;
		font-size: 30rpx;
		font-family: "PingFangSC-Medium, PingFang SC";
		font-weight: 500;
		color: #D8D8D8;
		line-height: 40rpx;
	}
	.top-menu-view .menu-one-view .menu-one .menu-one-bottom {
		position: absolute;
		bottom: 0;
		width: 100%;
	}
	.top-menu-view .menu-one-view .menu-one .menu-one-bottom .menu-one-bottom-color {
		width: 60%;
		height: 4rpx;
	}
	.top-menu-view .menu-one-view .menu-one-act {
		/* 在这里写 单个按钮样式 */
		margin-right: 74rpx;
		position: relative;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.top-menu-view .menu-one-view .menu-one-act:nth-child(1) {
		margin-left: 60rpx;
	}
	.top-menu-view .menu-one-view .menu-one-act .menu-one-txt {
		height: 40rpx;
		font-size: 30rpx;
		font-family: "PingFangSC-Medium, PingFang SC";
		font-weight: 500;
		color: #FFFFFF;
		line-height: 40rpx;
	}
	.top-menu-view .menu-one-view .menu-one-act .menu-one-bottom {
		position: absolute;
		bottom: 0;
		width: 100%;
		display: flex;
		justify-content: center;
	}
	.top-menu-view .menu-one-view .menu-one-act .menu-one-bottom .menu-one-bottom-color {
		width: 60rpx;
		height: 8rpx;
		background: linear-gradient(90deg, #EBCA97 0%, #D2AF7A 68%, #C99C58 100%);
		border-radius: 4rpx;
	}
	.swiper-box-list {
		flex: 1;
		width: 100%;
		height: auto;
		background-color: #F3F2F0;		
		border-radius: 38rpx 38rpx 0rpx 0rpx;
		margin-top:-15px;
	}
	.swiper-one-list {
		height: 100%;
		width: 100%;
	}
	.swiper-one-list .swiper-list-entity {
		width: 100%;
	}
</style>

原文链接:https://blog.csdn.net/WWW_wwwww/article/details/122109643

栏目分类
最近更新