uniapp 拖拽排序

1.拖拽排序 使用 sortablejs库

复制代码
npm install sortablejs --save-dev

<template>
	<view id="list">
		<view v-for="(item, index) in list" :key="item.id" class="item">
			{{ item.name }}
		</view>
	</view>
</template>

<script>
	import Sortable from "sortablejs";

	export default {
		data() {
			return {
				list: [{
						id: 1,
						name: "列表项1"
					},
					{
						id: 2,
						name: "列表项2"
					},
					{
						id: 3,
						name: "列表项3"
					},
					{
						id: 5,
						name: "列表项5"
					},{
						id: 6,
						name: "列表项6"
					},{
						id: 7,
						name: "列表项7"
					},{
						id: 8,
						name: "列表项8"
					},{
						id: 9,
						name: "列表项9"
					},{
						id: 10,
						name: "列表项10"
					},{
						id: 11,
						name: "列表项11"
					},{
						id: 12,
						name: "列表项12"
					},{
						id: 13,
						name: "列表项13"
					},{
						id: 14,
						name: "列表项14"
					}
				]
			};
		},
		mounted() {
			this.initSortable();
		},
		methods: {
			initSortable() {
				const list = document.getElementById("list");
				new Sortable(list, {
					animation: 150,
					onUpdate: this.handleUpdate
				});
			},
			handleUpdate(event) {
				const {
					oldIndex,
					newIndex
				} = event;
				const item = this.list.splice(oldIndex, 1)[0];
				this.list.splice(newIndex, 0, item);
			}
		}
	};
</script>

<style scoped>
	.container {
		display: flex;
		flex-direction: column;
	}

	.item {
		padding: 10px;
		margin: 5px 0;
		background-color: #f0f0f0;
		border: 1px solid #ccc;
		cursor: pointer;
	}
</style>

2.拖拽到指定位置并停留

复制代码
<template>
  <view class="container">
    <movable-area class="area">
      <movable-view
        v-for="(item, index) in list"
        :key="item.id"
        :x="0"
        :y="item.y"
        direction="vertical"
        @change="handleMove(index, $event)"
        class="item"
      >
        {{ item.name }}
      </movable-view>
    </movable-area>
  </view>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { id: 1, name: "列表项1", y: 0 },
        { id: 2, name: "列表项2", y: 50 },
        { id: 3, name: "列表项3", y: 100 },
        { id: 4, name: "列表项4", y: 150 }
      ]
    };
  },
  methods: {
    handleMove(index, event) {
      const { y } = event.detail;
      this.list[index].y = y;
      this.sortList();
    },
    sortList() {
      this.list.sort((a, b) => a.y - b.y);
    }
  }
};
</script>

<style scoped>
.container {
  height: 300px;
}
.area {
  width: 100%;
  height: 100%;
}
.item {
  width: 100%;
  height: 50px;
  background-color: #f0f0f0;
  border: 1px solid #ccc;
  text-align: center;
  line-height: 50px;
}
</style>
相关推荐
2501_916008892 小时前
没有源码如何加密 IPA 实战流程与多工具组合落地指南
android·ios·小程序·https·uni-app·iphone·webview
LXA08094 小时前
UniApp 小程序中使用地图组件
小程序·uni-app·notepad++
QuantumLeap丶6 小时前
《uni-app跨平台开发完全指南》- 07 - 数据绑定与事件处理
vue.js·ios·uni-app
2501_9159090611 小时前
Flutter 应用怎么加固,多工具组合的工程化实战(Flutter 加固/Dart 混淆/IPA 成品加固/Ipa Guard + CI)
android·flutter·ios·ci/cd·小程序·uni-app·iphone
xiaaaa.z13 小时前
macos HbuildX 使用cli脚本创建uniapp 运行时报错“cli项目运行依赖本地的Nodejs环境,请先安装并配置到系统环境变量后重试。”
macos·uni-app
2501_9159090614 小时前
深入理解HTTPS和HTTP的区别、工作原理及安全重要性
安全·http·ios·小程序·https·uni-app·iphone
Q_Q5110082851 天前
python+uniapp基于微信小程序的垃圾分类信息系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
你的眼睛會笑1 天前
uniapp 鸿蒙元服务 真机调试流程指南
华为·uni-app·harmonyos
2501_915921431 天前
查看iOS App实时日志的正确方式,多工具协同打造高效调试与问题定位体系(2025最新指南)
android·ios·小程序·https·uni-app·iphone·webview
雯0609~1 天前
uni-app:实现快递的节点功能
uni-app