uniapp实现左右滑动

在这里插入图片描述
在这里插入图片描述

<template>
	<view class="content">
		<view class="model_box">
			<view class="model_title">推广营销</view>
			<view class="model_scrollx flex_row">
				<scroll-view class="uni-swiper-tab" scroll-x :style="'height:'+scrollH+'px'">
					<view class="scrollx_items tg_zdtg">
						<image src="/static/my/icon_tg1.png" mode=""></image>
						<view class="tgyx_title">置顶推广</view>
						<view class="tgyx_desc">搜索页最强特权</view>
					</view>
					<view class="scrollx_items tg_hot">
						<image src="/static/my/icon_tg2.png" mode=""></image>
						<view class="tgyx_title">我要上热门</view>
						<view class="tgyx_desc">多十倍商机</view>
					</view>
					<view class="scrollx_items tg_zshy">
						<image src="/static/my/icon_tg3.png" mode=""></image>
						<view class="tgyx_title">钻石会员</view>
						<view class="tgyx_desc">抢占无限商机</view>
					</view>
					<view class="scrollx_items tg_hjhy">
						<image src="/static/my/icon_tg4.png" mode=""></image>
						<view class="tgyx_title">黄金会员</view>
						<view class="tgyx_desc">提供供应排名</view>
					</view>
				</scroll-view>
				
			</view>
		</view>
		
	</view>
</template>
<script>
	export default {
		data() {
			return {
				scrollH: 130
			}
		},
		onLoad() {
			uni.getSystemInfo({
			  success: function(res) { // res - 各种参数
			      let info = uni.createSelectorQuery().select(".scrollx_items");
			      info.boundingClientRect(function(data) { //data - 各种参数
			      console.log(data.height)  // 获取元素宽度
						this.scrollH = data.height
			      }).exec()
			       }
			});
		},
		onShow() {
			
			
		},
		methods: {
			
		}
	}
</script>

<style>

	/* 左右滑动 */
	.uni-swiper-tab{
		white-space: nowrap;
	}
	.model_scrollx{
		justify-content: space-between;
		padding: 40rpx 0;
	}
	.scrollx_items{
		padding: 30rpx 20rpx;
		text-align: center;
		display: inline-block;
		width: 210rpx;
		box-sizing: border-box;
		margin-left: 30rpx;
	}
	.scrollx_items:last-child{
		margin-right: 30rpx;
	}
	.scrollx_items image{
		width: 70rpx;
		height: 66rpx;
	}
	.tgyx_title{
		font-size: 28rpx;
		color: #333333;
		margin-top: 30rpx;
	}
	.tgyx_desc{
		font-size: 24rpx;
		margin-top: 10rpx;
	}
	.tg_zdtg{
		background: linear-gradient(bottom right, #d5e4fa, #dae7fb);
		color: #477cd3;
	}
	.tg_hot{background: linear-gradient(bottom right, #fffcec, #fbf6d9);color: #ffa800;}
	.tg_zshy{background: linear-gradient(bottom right, #fdf7fe, #f9e5fe);color: #c559de;}
	.tg_hjhy{background: linear-gradient(bottom right, #fff8f0, #fff0df);color: #f3791f;}
	
	
</style>

  • 6
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现左右滑动切换页面,可以使用uni-app提供的swiper组件,具体步骤如下: 1. 在页面中添加swiper组件: ```html <swiper :current="current" @change="swiperChange" :duration="500"> <swiper-item> <!-- 第1个页面内容 --> </swiper-item> <swiper-item> <!-- 第2个页面内容 --> </swiper-item> <swiper-item> <!-- 第3个页面内容 --> </swiper-item> </swiper> ``` 2. 在data中定义current变量,默认值为0表示当前显示第1个页面: ```js data() { return { current: 0 } } ``` 3. 在methods中添加swiperChange方法,监听swiper组件的change事件,更新current变量的值: ```js methods: { swiperChange(event) { this.current = event.detail.current; } } ``` 4. 在样式中设置swiper的宽度和高度,以及swiper-item的宽度和高度为100%: ```css swiper { width: 100%; height: 100%; } swiper-item { width: 100%; height: 100%; } ``` 5. 最后,使用touch事件监听用户的滑动操作,根据滑动的方向更新current变量的值,实现页面切换: ```js methods: { // 左右滑动切换页面 touchstart(event) { this.touchStartX = event.touches[0].pageX; }, touchend(event) { this.touchEndX = event.changedTouches[0].pageX; if (this.touchEndX - this.touchStartX > 50 && this.current > 0) { this.current--; } else if (this.touchEndX - this.touchStartX < -50 && this.current < 2) { this.current++; } } } ``` 完整代码如下: ```html <template> <swiper :current="current" @change="swiperChange" :duration="500"> <swiper-item> <!-- 第1个页面内容 --> </swiper-item> <swiper-item> <!-- 第2个页面内容 --> </swiper-item> <swiper-item> <!-- 第3个页面内容 --> </swiper-item> </swiper> </template> <script> export default { data() { return { current: 0, touchStartX: 0, touchEndX: 0 } }, methods: { swiperChange(event) { this.current = event.detail.current; }, touchstart(event) { this.touchStartX = event.touches[0].pageX; }, touchend(event) { this.touchEndX = event.changedTouches[0].pageX; if (this.touchEndX - this.touchStartX > 50 && this.current > 0) { this.current--; } else if (this.touchEndX - this.touchStartX < -50 && this.current < 2) { this.current++; } } } } </script> <style> swiper { width: 100%; height: 100%; } swiper-item { width: 100%; height: 100%; } </style> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值