一个基于Sortable的简单列表拖拽
先安装依赖,在需要用到的页面引入
安装依赖
npm install sortablejs --save
引入页面
import Sortable from "sortablejs";
<template>
<div class="demo">
<div class="List" v-for="item in dataList" :key="item.id">
{{ item.name }}
</div>
</div>
</template>
<script>
import Sortable from "sortablejs";
export default {
data() {
return {
dataList: [
{
id: 1,
name: "我是数字1",
},
{
id: 2,
name: "我是数字2",
},
{
id: 3,
name: "我是数字3",
},
{
id: 4,
name: "我是数字4",
},
],
};
},
mounted() {
this.drag();
},
methods: {
drag() {
let drag = document.querySelector(".demo");
Sortable.create(drag, {
animation: 150,
sort: true,
});
},
},
};
</script>
<style scoped>
.List {
width: 500px;
line-height: 30px;
background: pink;
margin: 5px 0;
}
</style>