zoukankan      html  css  js  c++  java
  • uniApp实现swiper和scroll-view联动

    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";  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 {
    		 100%;
    		height: 90%;
    	}
    
    	.body-view {
    		display: inline-block;
    		 150rpx;
    		height: 60rpx;
    		background-color: #007AFF;
    	}
    
    	.scroll-view {
    		text-align: center;
    		 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>
    

    ————————————————
    版权声明:本文为CSDN博主「满舰饰v」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_40893035/article/details/107977434

  • 相关阅读:
    cocoapods 命令
    开发常用
    ios 定位
    LoadingView
    自定义cell右侧 多按钮
    cocoaPods
    AFNetWorking
    iphone自定义铃声
    升级为iOS9后,默认请求类型为https,如何使用http进行请求会报错(引用他人的)
    理解c语言中的指针
  • 原文地址:https://www.cnblogs.com/axingya/p/14981215.html
Copyright © 2011-2022 走看看