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

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把数据返回出去 !
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Uni-app 是一款能够开发多个终端的跨平台框架,包括小程序、H5、App 等,可以在同一个代码库中开发出具有不同功能的应用。而 Uni-app 小程序源码就是使用 Uni-app 开发小程序的代码。 Uni-app 小程序源码具有以下优势: 1. 高效开发:借助 Uni-app 的跨平台开发框架,Uni-app 小程序源码可以快速开发出具有高质量和高性能的小程序。 2. 多平台统一:Uni-app 小程序源码可以快速发布到多个平台的小程序环境中,如微信小程序、支付宝小程序、百度小程序等。 3. 组件库完整:Uni-app 小程序源码提供了完整的组件库和工具库,开发者可以快速开发小程序。 4. 商业支持:Uni-app 小程序源码由腾讯公司开发,具有较强的商业支持,可以解决开发者在开发过程中的各种问题。 总的来说,Uni-app 小程序源码是一种高效、跨平台的小程序开发方式,可以大大节省开发者的时间和精力。同时,也为多个终端用户提供了更加方便和高效的小程序服务。 ### 回答2: Uni-app是一个跨平台开发框架,支持将一个代码库编译成多个平台的应用程序,其中包括iOS、Android、H5、微信小程序等。而Uni-app小程序源码是指使用Uni-app框架开发出来的小程序代码,通过该源码可以进行二次开发,解析出小程序的各个功能和页面,进行定制化开发,满足不同业务场景的需求。 使用Uni-app小程序源码可以快速实现小程序开发,不仅减少了代码的编写量,同时也兼容了多个平台,方便用户在不同设备上使用该应用。同时,Uni-app小程序源码的可维护性也很高,因为只需要在一个代码库中进行修改和维护,而不需要针对每个平台单独编写代码。 在使用Uni-app小程序源码时,需要先进行环境搭建,安装相关的开发工具和依赖库,然后即可进行开发。源码中包含了小程序的各个功能和页面,开发者可以根据自己的需求进行修改和扩展,同时也可以使用Uni-app提供的组件和API完成自己的开发任务。 需要注意的是,在进行Uni-app小程序源码开发时,需要熟悉Vue.js框架的相关知识,因为Uni-app是基于Vue.js的框架进行开发的。同时,还需要了解小程序开发的相关知识,包括小程序的生命周期、路由、组件等。掌握这些知识后,才能够更好地利用Uni-app小程序源码进行开发实现优秀的小程序应用。 ### 回答3: uni-app 小程序源码是指在 uni-app 开发框架下开发小程序代码。uni-app 是一个多端开发框架,可以让开发者使用一套代码同时开发出跨 iOS、Android、H5 等多个平台的应用程序。在这个框架下,我们可以通过 Vue.js 开发小程序,并且使用 uni-app 提供的开发工具来进行调试和编译。 uni-app 具有许多优点,如跨平台、快速开发、高效编译等,其提供了丰富的组件和插件,让开发者可以轻松地在小程序实现各种功能。开发者只需要按照 Vue.js 的组件开发方式去编写小程序代码,就可以实现复杂的应用逻辑。 uni-app 小程序源码的开发需要一定的技术积累和编程经验,同时还需要对互联网技术有一定的了解。一般来说,开发者需要掌握 Vue.js 基础、小程序开发技能等,才能开发出可靠、高效的 uni-app 小程序源码。 总之,uni-app 小程序源码是一个非常有价值的开发项目,可以帮助更多的开发者快速实现小程序应用功能,同时也可以提高开发效率,降低开发成本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值