This article mainly introduces the simple implementation of vue drag and drop, as follows: Rendering No duplication is determined and old data is not deleted. Data body <MyShuttle:dataOrigin='[ { Name: "Data 001", Id: "Number 001", }, { Name: "Data 002", Id: "Number 001", }, { Name: "Data 003", Id: "Number 001", }]' :space='[{ Name:"Right 001", Id:"Right 001", }]' /> Code draggable Enable draggable @dragenter.prevent @dragover.prevent // Prevent the browser from defaulting to the behavior, otherwise a cross will be displayed, which is not pretty Preventing default behavior @dragleave.stop="dragleave($event, 'main')" Entering and leaving the current element will trigger @dragend.stop="dragEnd($event, item)" Drop the drag content trigger Drag events and properties Mark This is very important!!! This determines the behavior of the drag event. When you click to start dragging, the location where the mouse is clicked is the marker. H5 drag attribute draggable draggable: When an element needs to be draggable, it needs to be set to true. The default value is false. Selected text, images, and links can be dragged by default. When the mouse moves to the target div box, it will be added. The simple one can best illustrate the problem. <template> <div class="MyShuttle"> <div class="MyShuttleLeft"> <div class="title">Data Source</div> <div class="dataBox" @dragleave.stop="dragleave($event, 'main')"> <div v-for="(item, i) in dataOrigin" :key="i" class="dataList" draggable @dragenter.prevent @dragover.prevent @dragstart.stop="dragstart($event, item)" @dragend.stop="dragEnd($event, item)"> {{ item.Name}} </div> </div> </div> <div class="MyShuttleCenter"></div> <div class="MyShuttleRight"> <div class="title">Data Source</div> <div ref="MyShuttleRight" class="dataBox"> <div v-for="(item, i) in spaceList" :key="i" class="dataList" draggable @dragenter.prevent @dragover.prevent> {{ item.Name}} </div> </div> </div> </div> </template> <script> export default { name: '', components: {}, props: { dataOrigin: { type: Array }, space: { type: Array } }, data() { return { spaceList: this.space, isDragStatus: false } }, computed: {}, watch: {}, created() { }, mounted() { }, methods: { dragleave(e, item) { // console.log(e, item) if (item === 'main') { this.isDragStatus = true } else { this.isDragStatus = false } // console.log(this.isDragStatus) }, dragstart(e, item) { // console.log(e, item) }, dragEnd(e, item) { const top = this.$refs.MyShuttleRight.getBoundingClientRect().top const right = this.$refs.MyShuttleRight.getBoundingClientRect().right const bottom = this.$refs.MyShuttleRight.getBoundingClientRect().bottom const left = this.$refs.MyShuttleRight.getBoundingClientRect().left console.log(top, right, bottom, left) console.log(e.clientX, e.clientY, item) if (this.isDragStatus && e.clientY > top && e.clientY < bottom && e.clientX > left && e.clientX < right) { this.spaceList.push(item) console.log(this.spaceList.indexOf(item)) } } } } </script> <style scoped lang="scss"> .MyShuttle { width: 100%; height: 308px; display: flex; justify-content: space-between; // Common style for left and right boxes.MyShuttleLeft, .MyShuttleRight { border: 1px solid #dddddd; border-collapse: collapse; .title { box-sizing: border-box; width: 100%; height: 40px; background: #f5f5f5; border-radius: 4px 4px 0px 0px; border-bottom: 1px solid #dddddd; padding: 10px 16px; font-size: 14px; font-family: PingFangSC-Regular, PingFang SC; font-weight: 400; color: #333333; line-height: 20px; } .dataBox { width: 100%; height: 228px; overflow:auto; padding: 6px 0; .dataList { width: 96%; height: 40px; box-sizing: border-box; font-size: 14px; font-family: PingFangSC-Regular, PingFang SC; font-weight: 400; color: #666; line-height: 20px; margin: 0 2% 10px; padding: 10px 16px; border: 1px solid #ddd; border-radius: 4px; user-select: none; cursor: pointer; &:hover { color: #01bc77; background: rgba(1, 188, 119, 0.1); } } } } .MyShuttleLeft { width: 362px; height: 100%; background: #ffffff; border-radius: 4px; } .MyShuttleCenter { width: 64px; height: 100%; } .MyShuttleRight { width: 362px; height: 100%; background: #ffffff; border-radius: 4px; } } </style> This is the end of this article about the simple implementation of vue drag-and-drop addition. For more relevant vue drag-and-drop addition content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Implementation example of specifying container ip when creating a container in docker
>>: The marquee tag in HTML achieves seamless scrolling marquee effect
Recently, there have been many database-related o...
This article shares the specific code of JavaScri...
Table of contents Effect demonstration:Main JS co...
Purpose of using Nginx Using Alibaba Cloud ECS cl...
The accessibility of web pages seems to be somethi...
1. Demand The base has 300 new servers, and needs...
describe: Install VM under Windows 10, run Docker...
Table of contents Various ways to merge objects (...
Table of contents 1. Source code 1.1 Monorepo 1.2...
Table of contents 1. Build the operating environm...
Preface For security reasons, the root user of My...
Preface ActiveMQ is the most popular and powerful...
1. Install dependency packages yum -y install gcc...
First, let's talk about why we need to divide...
3. MySQL Data Management The first method: not re...