uni-app 小程序实现列表拖拽排序

该文章详细介绍了如何在uni-app小程序中使用自定义组件实现列表的拖拽排序功能。通过触屏事件监听(touchstart,touchmove,touchend),动态更新控件的位置,并在手指松开时完成位置交换,实现交互式的列表排序效果。

uni-app 小程序实现列表拖拽排序

一、引入组件

<template>
	<view>
		<drag-and-drop-sort-B style="display: flex; justify-content: center;" :controlsList="controlsList" :containerSize="{width: '200px', height: '100vh'}" :controlsSize="{width: 200, height: 40}" />
	</view>
</template>
 
<script>
export default {
	data() {
		return {
			controlsList: ['#fc5531', '#1481f9', '#1ab519', '#32c0eb', '#e73e60', '#a663c2', '#b26801', '#1c2e6c', '#cdccc8'],
		}
	},
};
</script>
 
<style scoped lang="scss">
	
</style>

组件内的实现

<template>
	<view class="drag-and-drop-sort-B" :style="[containerSize]">
		<template v-if="controlsPositionArray.length !== 0">
			<view v-for="(item, index) in controlsArray" :key="index" class="_item"
				:style="{'background': item, 'transition': (curretnControlsIndex === index ? 'initial' : '.3s'), 'z-index': (curretnControlsIndex === index ? 1 : 0), 'width': controlsSize.width + 'px', 'height': controlsSize.height + 'px', 'top': controlsPositionArray[index].top + 'px',  'left': controlsPositionArray[index].left + 'px'}">
				<view @touchstart="handleTouchstart($event, index)" @touchmove="handleTouchmove" @touchend="handleTouchend" :style="{'background': item}" style="width: 100%; height: 100%;">
					<!-- 自定义内容 -->
					<view style="display: flex; align-items: center; justify-content: center; width: 100%; height: 100%;">
						{{index + 1}}
					</view>
				</view>
			</view>
		</template>
	</view>
</template>
 
<script>
	export default {
		name: "drag-and-drop-sort-B",
		props: {
			// 容器大小
			containerSize: {
				type: Object,
				default: () => ({ wdith: '100vw', height: '100vh' }),
			},
			// 控件的大小
			controlsSize: {
				type: Object,
				default: () => ({ width: 0, height: 0 }),
			},
			// 数据列表
			controlsList: {
				type: Array,
				default: () => [],
			},
		},
		data() {
			return {
				// 控件列表
				controlsArray: [],
				// 每行最大存放的个数
				maxWidthCount: 1,
				// 控件的间距
				margin: {
					margin_x: 0,
					margin_y: 10,
				},
				// 记录所有控件的初始位置
				recordInitControlsPoisitonList: [],
				// 控件的数据
				controlsPositionArray: [],
				// 记录当前手指的位置
				recordPosition: {
					x: 0,
					y: 0,
				},
				// 记录当前操作的控件数据
				recordControlsPositionItem: {},
				// 当前操作的控件的下标
				curretnControlsIndex: -1,
			};
		},
		mounted() {
			// 获取系统信息
			this.systemInfo = uni.getSystemInfoSync();
			// 获取控件列表
			this.controlsArray = this.controlsList;
			
			// 初始化控件的位置
			this.controlsPositionArray = this.initControlsPosition();
		},
		methods: {
			/** 初始化各个控件的位置 */
			initControlsPosition() {
				// 用于返回出去的新数组
				let tempArray = [];
 
				// 设置控件位置
				for(let i = 0, j = 0; i < this.controlsList.length; i++, j++) {
					tempArray[i] = {
						left: this.margin.margin_x,
						top: j * (this.controlsSize.height + this.margin.margin_y) + this.margin.margin_y,
					}
				}
				
				// 记录数据 - 进行深拷贝
				this.recordInitControlsPoisitonList = [...tempArray];
				// 返回数据
				return tempArray;
			},
			
			/** 处理手指触摸后移动 */
			handleTouchmove(event) {
				const { pageX, pageY } = event.touches[0];
				
				// 获取移动的差
				this.controlsPositionArray[this.curretnControlsIndex] = {
					left: this.controlsPositionArray[this.curretnControlsIndex].left + (pageX - this.recordPosition.x),
					top: this.controlsPositionArray[this.curretnControlsIndex].top + (pageY - this.recordPosition.y),
				}
				// 记录位置
				this.recordPosition = { x: pageX, y: pageY };
				// 判断当前移动的位置是否需要进行排序
				// 向下移动
				if(this.curretnControlsIndex !== this.controlsPositionArray.length - 1 && this.controlsPositionArray[this.curretnControlsIndex].top > this.controlsPositionArray[this.curretnControlsIndex + 1].top) {
					// 交换位置
					this._handleChangeControlsPosition(0, this.curretnControlsIndex + 1);
				}
				// 向上移动
				else if(this.curretnControlsIndex !== 0 && this.controlsPositionArray[this.curretnControlsIndex].top < this.controlsPositionArray[this.curretnControlsIndex - 1].top) {
					// 交换位置
					this._handleChangeControlsPosition(0, this.curretnControlsIndex - 1);
				}
			},
			
			/** 处理手指触摸开始事件 */
			handleTouchstart(event, index) {
				const { pageX, pageY } = event.touches[0];
				
				// 记录一些数据
				this.curretnControlsIndex = index;
				this.recordPosition = { x: pageX, y: pageY };
				this.recordControlsPositionItem = this.controlsPositionArray[index];
			},
		
			/** 处理手指松开事件 */
			handleTouchend(event) {
				// 将操控的控件归位
				this.controlsPositionArray[this.curretnControlsIndex] = this.recordInitControlsPoisitonList[this.curretnControlsIndex];
				this.curretnControlsIndex = -1;
			},
			
			/**
			 * 处理交换控件位置的方法 - 
			 * @param {number} index	需要与第几个下标交换位置
			 * */
			_handleChangeControlsPosition(type, index) {
				// 记录当前操控的控件数据
				let tempControls = this.controlsArray[this.curretnControlsIndex];
				
				// 设置原来位置的数据
				this.controlsArray[this.curretnControlsIndex] = this.controlsArray[index];
				// 将临时存放的数据设置好
				this.controlsArray[index] = tempControls;
				
				// 调整控件位置数据
				this.controlsPositionArray[index] = this.controlsPositionArray[this.curretnControlsIndex];
				this.controlsPositionArray[this.curretnControlsIndex] = this.recordControlsPositionItem;
				
				// 改变当前选中的位置
				this.curretnControlsIndex = index;
				
				// 记录新位置的数据
				this.recordControlsPositionItem = this.recordInitControlsPoisitonList[this.curretnControlsIndex];
			},
		}
	}
</script>
 
<style scoped lang="scss">
	.drag-and-drop-sort-B {
		position: relative;
		
		._item {
			position: absolute;
		}
	}
</style>
tips: 可以根据需求在某个时机自行执行$emit把数据返回出去 !
uni-app 开发的微信小程序实现图片拖拽排序功能,可以通过以下方法进行: ### 3.1 使用插件或自定义组件 - 可以考虑使用已有的插件如 `shmily-drag-image`,它是一个基于 uni-app 的图片拖拽排序插件,允许用户通过拖拽的方式对图片进行排序,并支持自定义添加图片和删除确认功能。该插件已经在 H5 和微信小程序平台上测试通过[^1]。 ### 3.2 自定义实现 如果选择不使用现成插件,则可以自行实现拖拽排序逻辑: #### 3.2.1 模板部分 在模板中定义一个包含所有图片项的列表,并为每个图片项绑定相应的事件处理函数: ```html <view class="image-list"> <view class="image-item" v-for="(image, index) in imageList" :key="index" @longpress="startDrag(index)" @touchmove="onTouchMove" > <image :src="image.url" mode="aspectFill"></image> <view class="button-group"> <!-- 删除按钮 --> <button @click="deleteImage(index)">删除</button> <!-- 替换按钮 --> <button @click="replaceImage(index)">替换</button> </view> </view> </view> ``` #### 3.2.2 脚本部分 在脚本中定义数据模型以及相关的处理函数: ```javascript export default { data() { return { imageList: [], // 图片列表 draggingIndex: null, // 正在拖动的索引 }; }, methods: { startDrag(index) { this.draggingIndex = index; }, onTouchMove(event) { if (this.draggingIndex === null) return; const touch = event.touches[0]; // 这里需要实现具体的拖动逻辑,例如更新位置等 // 注意:uni-app 中触摸事件处理可能需要额外计算位置变化 }, deleteImage(index) { this.imageList.splice(index, 1); }, replaceImage(index) { // 实现替换图片的逻辑 } } }; ``` ### 3.3 注意事项 -uni-app 中处理触摸事件时需要注意平台差异性,特别是对于微信小程序来说,某些 API 或者行为可能会有所不同。 - 对于复杂的拖拽交互,建议参考官方文档或者社区提供的解决方案来确保兼容性和用户体验。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值