How to realize drag and drop and swap position with vue
This article mainly introduces the relevant knowledge of "how to achieve drag-and-drop location exchange in vue". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve drag-and-drop location exchange in vue" can help you solve the problem.
The specific code is as follows
{{item}} export default {name: "Toolbar", data () {return {items: [{key: 1, color: "# 3883a0"}, {key: 2, color: "# 4883a0"}, {key: 3, color: "# 5883a0"}, {key: 4, color: "# 6883a0"}, {key: 5 Color: "# 7883a0"}, {key: 6, color: "# 8883a0"}, {key: 7, color: "# 9883a0"},], ending: null, dragging: null,} }, methods: {handleDragStart (e, item) {this.dragging = item;}, handleDragEnd (e, item) {if (this.ending.key = this.dragging.key) {return;} let newItems = [... this.items]; const src = newItems.indexOf (this.dragging); const dst = newItems.indexOf (this.ending) NewItems.splice (src, 1,... newItems.splice (dst, 1, newItems [src]); console.log (newItems); this.items = newItems; this.$nextTick (() = > {this.dragging = null; this.ending = null;})) }, handleDragOver (e) {/ / first make div an element that can be placed, that is, rewrite dragenter/dragover / / set e.dataTransfer.dropEffect = "move" in dragenter for the placement target;}, handleDragEnter (e, item) {/ / set the dragstart event e.dataTransfer.effectAllowed = "move"; this.ending = item for the element to be moved },},}; .container {display: flex; flex-wrap: wrap;}. Item {width: 200px; height: 200px; margin: 10px; color: # fff; transition: all linear 0.3s;} .item0 {width: 400px;}
Effect.
This is the end of the introduction to "how to drag and drop and swap locations with vue". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.