uniApp实现swiper和scroll-view联动,滑动swiper时改变scroll-view,点击scroll-view时切换到指定的swiper(初学者)

uniApp实现swiper和scroll-view联动,滑动swiper时改变scroll-view,点击scroll-view时切换到指定的swiper(初学者)

因为这里只是一个简单的示例,提供一下思路,具体需求自己加以改动。
代码效果如图,运行环境为HBuilderX,运行在Chrome浏览器中的样子
基本效果为,点击顶部导航的数字,下面swiper区域会滑动到相应区域,滑动swiper区域,顶部导航也会跟着改变,这边使用三种颜色进行区分
在这里插入图片描述
代码如下

<template>
	<!-- 这里的注意点,使用 scroll-view时,如果想让元素横向也就是X轴排列的话
	要注意以下几点:
	1、scroll-view不支持flex,默认block;
	2、scroll-view设置scroll-x="true"; width: 100%; white-space: nowrap;(这个属性很重要,能不能滑动都看这个属性)
	3、子元素设置display: inline-block;
	4、子元素内容宽度要超出scroll-view的宽。
	-->
	<view>
		<!-- 设置scroll-view的scroll-x="true",为X轴滑动 -->
		<scroll-view scroll-x="true" class="scroll-view">
			<!-- 子元素选择用v-for进行循环遍历,因为后面要用到index -->
			<view class="body-view" v-for="(item,index) in scrollViewList" :key="index" @click="changeSwiper(index)">
				<!-- 这里是一个小提醒点,动态绑定class的值,一个三元表达式 -->
				<view :class="[currentTab==index ? 'menu-one-act' : 'menu-one']">
					{{item}}
				</view>
			</view>
		</scroll-view>
		<!-- swiper这边设置禁用指示点,禁止自动轮播,动态设置swiper的current属性,以便和scroll-view进行对接,在设置一个轮播切换事件
		 当swiper改变时,触发函数
		 -->
		<swiper :indicator-dots="false" :autoplay="false" class="swiper" :current="currentTab" ref="swiper" @change="changeScroll">
			<block v-for="(item,index) in scrollViewList" :key="index">
				<swiper-item>
					<scroll-view scroll-y="true" class="swiper-scroll">
						<view class="swiper-item">{{item}}</view>
					</scroll-view>
				</swiper-item>
			</block>
		</swiper>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//定义swiper的初始值为0,也就是第一页
				currentTab: 0,
				//再定义一个数组,存放数据
				scrollViewList: ["频道", "分区"]
			}
		},
		onLoad() {
			uni.getSystemInfo({	//获取系统信息
				success: (res) => {
					this.swiperHeight = res.windowHeight + 'px'
				},
				fail: (res) => {
					console.log('error')
				}
			})
		},
		methods: {
			// 切换swiper时,改变scroll的函数
			changeScroll: function(e) {
				// 令data中定义的currentTab等于当前swiper的current的值,来改变scroll
				this.currentTab = e.target.current;
			},
			changeSwiper: function(index) {
				// 点击scroll,将返回的参数赋值给currentTab
				if (this.currentTab == index) {
					return false;
				} else {
					this.currentTab = index;
				}
			}
		}
	}
</script>

<style>
	page {
		width: 100%;
		height: 90%;
	}

	.body-view {
		display: inline-block;
		width: 150rpx;
		height: 60rpx;
		background-color: #007AFF;
	}

	.scroll-view {
		text-align: center;
		width: 100%;
		white-space: nowrap;
	}

	.menu-one {
		background-color: green;
		height: 100%;
	}

	.menu-one-act {
		background-color: blue;
		height: 100%;
	}

	.swiper {
		height: 2000px;
		text-align: center;
		background-color: #DD524D;
	}

	.swiper-scroll {
		height: 100%;
	}
</style>

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用swiperscroll-view实现长图左右切换和图片滚动功能的示例代码: HTML部分: ```html <swiper class="swiper-container"> <swiper-item> <scroll-view scroll-x="true" class="scroll-view"> <view class="image-wrapper"> <image src="image1.jpg" class="image"></image> </view> <view class="image-wrapper"> <image src="image2.jpg" class="image"></image> </view> <view class="image-wrapper"> <image src="image3.jpg" class="image"></image> </view> <!-- 添加更多图片 --> </scroll-view> </swiper-item> </swiper> ``` CSS部分: ```css .swiper-container { width: 100%; height: 300px; } .scroll-view { white-space: nowrap; } .image-wrapper { display: inline-block; width: 100%; height: 100%; } .image { width: 100%; height: 100%; } ``` JavaScript部分(使用了swiper的初始化代码): ```javascript import Swiper from 'swiper'; new Swiper('.swiper-container', { slidesPerView: 'auto', spaceBetween: 10, }); ``` 在这个示例中,我们首先创建一个swiper容器,并在其中嵌套一个scroll-view组件。scroll-view设置`scroll-x`属性为true,使其可以横向滚动。然后,我们在scroll-view中添加多个图片,每个图片都被包裹在一个image-wrapper中。为了保持图片的宽高比例,我们在CSS中设置了相应的样式。 最后,我们使用了swiper的初始化代码来初始化swiper容器,使其支持左右切换和图片滚动的功能。你可以根据需要自定义swiper的配置选项。 请注意,上述示例中的图片路径和样式仅供参考,你需要根据实际情况进行修改。另外,需要确保正确引入swiperscroll-view的相关资源文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值