uni-app轮播图组件实现手势滑动

定义swiper-list组件, 两个页面, 一个是swiper-list, 一个是swiper-list-item

<template>
	<swiper class="swiper-list" @change="change" :current='tabIndex'>
		<swiper-item class="swiper-list-item" v-for="(item,index) in tabLsit" :key="index">
			<swiper-list-item :list='article[index]'></swiper-list-item>
		</swiper-item>
	</swiper>
</template>

<script>
	import swiperListItem from './swiper-list-item.vue';
	export default {
		name: "swiper-list",
		components: {
			swiperListItem
		},
		props: {
			tabLsit: {
				type: Array,
				default () {
					return []
				}
			},
			tabIndex: {
				type: Number,
				default: 0
			}
		},
		watch: {
			tabLsit(newVal) {
				if (newVal.length === 0) return
				this.loadData(this.tabIndex)
			}
		},
		// 组件只能用created(), 页面用onLoad()
		created() {},
		data() {
			return {
				articleList: [],
				article: {}
			};
		},
		methods: {
			// 监听swiper滑动事件
			change(e) {
				let current = e.detail.current
				this.$emit('swiperListChange', current)
				this.loadData(current)
			},
			loadData(current) {
				this.$api.getArticle({
					name: this.tabLsit[current].name,
				}).then(res => {
					let {
						data
					} = res
					// 懒加载,强制vue刷新页面
					this.$set(this.article, current, data)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.swiper-list {
		height: 100%;
		flex: 1;

		.swiper-list-item {
			// height: 100%;
			// overflow: hidden;

			.scroll-list {
				height: 100%;
			}
		}
	}
</style>
<template>
	<scroll-list class="scroll-list">
		<card-list :item='item' v-for="(item,index) in list" :key="item._id"></card-list>
	</scroll-list>
</template>

<script>
	export default {
		props: {
			list: {
				type: Array,
				default () {
					return []
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.scroll-list {
		height: 100%;
	}
</style>

使用

<swiper-list :tabLsit='labelList' :tabIndex='tabIndex' @swiperListChange='swiperListChange'></swiper-list>

		methods: {
			getLabel() {
				this.$api.getLabel().then(res => {
					this.labelList = res.data
				})
			},
			tab({item,index}) {
				this.tabIndex = index
			},
			swiperListChange(current){
				this.swiperIndex = current
				this.tabIndex = current
			}
		}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值