Uniapp开发中,nvue文件内实现上下滑动+左右滑屏效果(尝试不给swiper赋高度,通过flex实现展示所有)

<template>
	<view class="isflex">
		<view :style="{'height':x}"></view>
		<view class="pd16_32">
			<view class="flex_1">
				<view v-for="(item,index) in center_list" :key="index">
					<view class="scroll_box" @click="active1(index)">
						<text class="font_18" v-if="activeIndex==index">{{item}}</text>
						<text class="font_16" v-else>{{item}}</text>
						<view class="chuantou" v-if="activeIndex==index"></view>
					</view>
				</view>
			</view>
		</view>

		<swiper :current="activeIndex" @change="newSwiper" class="swiper">
			<swiper-item v-for="(item,index) in center_list" :key="index" class="swiper_item">
				<template v-if="index!=0">
					<view class="pd16_32">
						<view class="flex_2">
							<view v-for="(item,index) in center_list2" :key="index">
								<view class="scroll_box" @click="active2(index)">
									<text class="font_16" :class="{'color_purple':activeIndex2==index}">{{item}}</text>
								</view>
							</view>
						</view>
					</view>

					<swiper class="swiper" :current="activeIndex2" @change="newSwiper2">
						<swiper-item v-for="(item,index) in center_list2" :key="index" class="swiper_item">
							<list class="list" show-scrollbar="false">
								<cell v-for="(item,index) in newList" :key="item.id">
									<template v-if="newList.length>0">
										<view class="pd32" :class="{'border_bottom':index!=newList.lenght-1}">
											<view class="flex_row flex_between">
												<view class="flex_row">
													<image :src="item.headerImg" mode="" style=""
														class="newList_headImg" />
													<view class="flex_between newList_person">
														<text class="newList_text1">{{item.UserName}}</text>
														<view class="flex_row">
															<view class="newList_box1">
																<text class="newList_text2">{{item.age}}</text>
															</view>
															<view class="newList_box2">
																<text class="newList_text2">{{item.voice_duration}}</text>
															</view>
															<view class="newList_box3"
																v-if="item.isfriend &&activeIndex2==1">
																<text class="newList_text3">好友</text>
															</view>
														</view>
													</view>
												</view>
												<view class="flex_alitem flex_jusCenter" v-if="activeIndex3==1">
													<image src="/static/predestination_Img/chat2_icon.png"
														class="newList_rightImg" />
													<text class="newList_text4">{{item.heat}}</text>
												</view>
											</view>
										</view>
									</template>

									<template v-else>
										<view class="phpto_box">
											<image src="/static/news_Img/Noclose_icon.png" mode="" class="img_size" />
											<text class="phpto_text">你还没有亲密的人哦~</text>
										</view>
										<view class="flex_alitem flex_jusCenter">
											<view class="Btn_container">
												<text class="Btn_text">去聊聊</text>
											</view>

										</view>
									</template>
								</cell>
							</list>
						</swiper-item>
					</swiper>

				</template>
				<template v-else>
					<view class="phpto_box">
						<image src="/static/news_Img/Noclose_icon.png" mode="" class="img_size" />
						<text class="phpto_text">你还没有收到过消息哦~</text>
					</view>
					<view class="flex_alitem flex_jusCenter">
						<view class="Btn_container">
							<text class="Btn_text">去聊聊</text>
						</view>
					</view>
				</template>


			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				x: uni.getSystemInfoSync().statusBarHeight, //通讯栏高度
				center_list: ['消息', '好友'],
				center_list2: ['亲密', '好友', '关注', '粉丝'],

				newList: [{
						headerImg: '/static/predestination_Img/voice_bg.png',
						UserName: '文质彬彬的圆鼓鼓',
						age: 26, //年龄
						voice_duration: '11s', //语音时长
						isfriend: false, //是朋友吗 true
						heat: 33.7,
						id: '001'
					},
					{
						headerImg: '/static/predestination_Img/voice_bg.png',
						UserName: '弄假成真的催逝员',
						age: 29, //年龄
						voice_duration: '8s', //语音时长
						isfriend: true, //是朋友吗 true
						heat: 30.6,
						id: '002',
					},
					{
						headerImg: '/static/predestination_Img/voice_bg.png',
						UserName: '好学不内卷的卷心超人',
						age: 23, //年龄
						voice_duration: '16s', //语音时长
						isfriend: true, //是朋友吗 true
						heat: 33.3,
						id: '003',
					},
					{
						headerImg: '/static/predestination_Img/voice_bg.png',
						UserName: '喜欢吃芒果的榴莲',
						age: 22, //年龄
						voice_duration: '12s', //语音时长
						isfriend: false, //是朋友吗 true
						heat: 32.7,
						id: '004',
					},
					{
						headerImg: '/static/predestination_Img/voice_bg.png',
						UserName: '玻璃心刀子嘴',
						age: 22, //年龄
						voice_duration: '12s', //语音时长
						isfriend: true, //是朋友吗 true
						heat: 32.7,
						id: '005',
					},
					{
						headerImg: '/static/predestination_Img/voice_bg.png',
						UserName: '小明隔壁的小刚',
						age: 12, //年龄
						voice_duration: '12s', //语音时长
						isfriend: true, //是朋友吗 true
						heat: 32.7,
						id: '006',
					},
					{
						headerImg: '/static/predestination_Img/voice_bg.png',
						UserName: '仙界漏网之鱼',
						age: 22, //年龄
						voice_duration: '12s', //语音时长
						isfriend: true, //是朋友吗 true
						heat: 32.7,
						id: '007',
					},
					{
						headerImg: '/static/predestination_Img/voice_bg.png',
						UserName: '战场达人普洛斯',
						age: 22, //年龄
						voice_duration: '12s', //语音时长
						isfriend: true, //是朋友吗 true
						heat: 32.7,
						id: '008',
					},
					{
						headerImg: '/static/predestination_Img/voice_bg.png',
						UserName: '北欧神话椰子树',
						age: 22, //年龄
						voice_duration: '12s', //语音时长
						isfriend: true, //是朋友吗 true
						heat: 32.7,
						id: '009',
					},
					{
						headerImg: '/static/predestination_Img/voice_bg.png',
						UserName: '新西兰大白菜',
						age: 22, //年龄
						voice_duration: '12s', //语音时长
						isfriend: true, //是朋友吗 true
						heat: 32.7,
						id: '010',
					},
					{
						headerImg: '/static/predestination_Img/voice_bg.png',
						UserName: '乔治和他的猫',
						age: 22, //年龄
						voice_duration: '12s', //语音时长
						isfriend: true, //是朋友吗 true
						heat: 32.7,
						id: '010',
					},
					{
						headerImg: '/static/predestination_Img/voice_bg.png',
						UserName: '欧洲黑道教父',
						age: 18, //年龄
						voice_duration: '7s', //语音时长
						isfriend: true, //是朋友吗 true
						heat: 32.7,
						id: '011',
					}
				],

				activeIndex: 0, //消息与好友的下标
				activeIndex2: 0, //好友--亲密、好友、关注、粉丝 下标
			}
		},


		onPullDownRefresh() {
			console.log('触发下拉刷新了')
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1000);
		},
		methods: {
			// 最外层滑屏(消息、好友)
			curChange(e) {
				console.log('滑屏', e)
				let val = e.detail.current
				this.activeIndex=val
			},

			// 内层滑屏 (亲密、好友、关注、粉丝)
			curChange2(e) {
				console.log('里面的齿轮滑动', e)
				let val = e.detail.current
				this.activeIndex2 = val
			},

			// 外层点击(消息、好友)
			active1(val) {
				console.log('val'.val)
				this.activeIndex = val
			},

			// 内层点击(亲密、好友、关注、粉丝)
			active2(val) {
				console.log('亲密', val)
				this.activeIndex2 = val
			},
			
			// 外层  不借助获取高度,来实现滑屏
			newSwiper(e) {
				this.activeIndex = e.detail.current
				console.log('eeeee', e)
			},
			
			// 内层  不借助获取高度,来实现滑屏
			newSwiper2(e) { 
				this.activeIndex2 = e.detail.current
				console.log('eeeee2222', e)
			},

		},
		mounted() {

		},
	}
</script>

<style>
	.flex_row {
		flex-direction: row;
	}

	.flex_between {
		justify-content: space-between;
	}

	.flex_jusCenter {
		justify-content: center;
	}

	.flex_alitem {
		align-items: center;
	}

	.isflex {
		flex: 1;
	}

	.pd32 {
		padding: 32rpx;
	}

	.pd16_32 {
		padding: 16rpx 32rpx;
	}

	.font_18 {
		margin-left: 6rpx;
		font-size: 36rpx;
		font-weight: bold;
	}

	.font_16 {
		margin-left: 6rpx;
		font-size: 32rpx;
		font-weight: 200;
	}

	.color_purple {
		color: #C958FF;
	}

	.flex_1,
	.flex_2 {
		flex-direction: row;
		align-items: center;
	}

	.flex_2 {
		justify-content: space-between;
	}

	.scroll_box {
		width: 110rpx;
		align-items: flex-start;
	}

	.chuantou {
		background: linear-gradient(to bottom, #C55FF5, #FFFFFF);
		border-radius: 12rpx;
		width: 80rpx;
		height: 24rpx;
		margin-top: -16rpx;
		opacity: 0.5;
		pointer-events: none;
	}

	.border_bottom {
		border-bottom: 2rpx solid #eaeaea;
	}

	.newList_headImg {
		height: 100rpx;
		width: 100rpx;
		border-radius: 50rpx;
	}

	.newList_rightImg {
		height: 60rpx;
		width: 60rpx;
	}

	.newList_person {
		margin-left: 24rpx;
		padding-top: 10rpx;
	}

	.newList_text1 {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}

	.newList_text2 {
		font-size: 24rpx;
		color: #fff;
	}

	.newList_text3 {
		font-size: 24rpx;
		color: #BF4FF4;
	}

	.newList_text4 {
		font-size: 22rpx;
		color: #FF5496;
	}


	.newList_box1,
	.newList_box2,
	.newList_box3 {
		width: 66rpx;
		height: 34rpx;
		background-color: #FF758D;
		border-radius: 18rpx;

		margin-right: 10rpx;
		align-items: center;
		justify-content: center;
	}

	.newList_box2 {
		height: 34rpx;
		width: 86rpx;
		background-color: #D782FF;
	}

	.newList_box3 {
		height: 34rpx;
		width: 70rpx;
		background-color: #fff;
		border: 2rpx solid #D782FF;
	}

	.swiper,
	.swiper_item,
	.list {
		flex: 1;
	}

	.phpto_box {
		margin: 200rpx auto;
		align-items: center;
		justify-content: center;
	}

	.img_size {
		width: 446rpx;
		height: 286rpx;
	}

	.phpto_text {
		margin-top: 69rpx;
		font-size: 26rpx;
		color: #999999;
		font-weight: bold;
	}

	.Btn_container {
		background: linear-gradient(to right, #FE8FF9, #CA56FF);
		width: 576rpx;
		height: 96rpx;
		border-radius: 48rpx;
		align-items: center;
		justify-content: center;
	}

	.Btn_text {
		color: #fff;
		font-size: 30rpx;
		font-weight: bold;
	}
</style>

在这里,我得事先声明一下,这里swiper不赋值给高度,是由于class已经给了flex:1  自填充,而swiper-item也是同理的。相当于,我在swiper拿到flex:1 一直走到list 再给list flex:1   借助list 的渲染机制,超出的内容在内容滚动时得到渲染,而向上划走的内容又被机制重新回收了。

补充:这里的案例,只适用于tab切换栏设死为吸顶部分。(通常为  消息列表页)  

 

### 使用 NVUE 实现自由滑动效果UniApp 中使用 NVUE 页面实现自由滑动效果可以通过 `list` 和 `scroller` 组件组合完成。具体来说,为了达到良好的性能表现并上下滑动以及左右滑屏效果,建议采用如下方法: #### 1. 设置页面布局结构 创建一个 nvue 文件,在其中定义根节点为 `<template>` 并设置其样式属性 `display: flex; height: 100%; width: 100%` 来确保整个页面能够自适应屏幕尺寸[^3]。 ```html <template> <div class="container"> <!-- 内容区域 --> </div> </template> <style scoped> .container { display: flex; height: 100%; width: 100%; } </style> ``` #### 2. 添加 scroller 容器 为了让内容区域能够滚动,需在外层包裹一层 `scroller` 组件,并为其指定方向参数 `scroll-direction="vertical"` 或者 `"horizontal"` 控制滚动的方向;同时也可以开启惯性滚动特性以提升用户体验。 ```html <div class="content-wrapper"> <scroller scroll-direction="vertical" show-scrollbar="false" bounce="true"> <!-- 可滚动的内容项 --> </scroller> </div> ``` #### 3. 构建可滑动的内容模块 对于每一个独立的滑动单元(如视频卡片),可以将其放置于 `swiper` 组件内部作为子元素存在。值得注意的是,为了避免固定高度带来的局限性,应当利用 CSS Flexbox 布局方式让各个组件自动调整大小。 ```html <swiper class="video-list" :autoplay="false" interval="5000" duration="500" circular="true"> <block v-for="(item, index) in videoList" :key="index"> <swiper-item class="video-item"> <video id="myVideo" class="video-player"></video> </swiper-item> </block> </swiper> ``` #### 4. 配置样式规则 最后一步是对上述 HTML 结构应用相应的 CSS 样式表,特别是针对 `.video-list`,`.video-item` 两个类名下的对象予合适的宽高比例及定位关系,从而保证最终呈现出来的界面既美观又实用。 ```css .video-list { flex-grow: 1; } .video-item { position: relative; padding-top: 56.25%; /* 保持16:9的比例 */ } .video-player { position: absolute; top: 0; left: 0; bottom: 0; right: 0; border-radius: 8px; } ``` 通过以上步骤即可实现UniAppNVUE 页面上构建具有优秀交互体验的自由滑动效果的应用程序。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值