Vue implements drag and drop function
It is a popular JavaScript framework for building user interfaces. One of the common needs is to implement drag and drop function in Vue, allowing users to interact by dragging elements.
Today, let’s learn how to implement this feature in Vue.
First of all, we need to understand the basic principles of the drag function: listen to mouse events, update the drag element position in real time, and finally stop dragging and update the element position at the right time.
In Vue, we can implement this function by binding related events.
Step 1: Create a draggable element
First, create an element in the Vue component, and we give this element adraggable
Attribute, this attribute is used to indicate whether this element can be dragged or not.
At the same time, we can bind this element to events such as mouse pressing, moving and releasing.
<template> <div class="draggable" :style="{ top: posY + 'px', left: posX + 'px' }" @mousedown="startDragging" @mousemove="dragging" @mouseup="stopDragging" > Drag me! </div> </template> <script> export default { data() { return { dragging: false, // Is it dragging or not offsetX: 0, // The offset from the upper left corner of the element when the mouse is pressed offsetY: 0, // The offset from the upper left corner of the seed element when the mouse is pressed posX: 0, // The position of the upper left corner of the element relative to the parent element posY: 0 // The position of the upper left corner of the element relative to the parent element }; }, methods: { startDragging(e) { = true; = - ; = - ; }, dragging(e) { if () { = - ; = - ; } }, stopDragging() { = false; } } }; </script> <style scoped> .draggable { position: absolute; cursor: move; border: 1px solid #ccc; padding: 10px; background-color: #f0f0f0; } </style>
In this sample code, we create a draggable<div>
element and listen for drag and drop by binding mouse events.
When the mouse is pressed, record the offset between the mouse and the upper left corner of the element; during the movement, the element position is constantly updated; when the mouse is released, stop dragging.
Step 2: Add styles and interactive effects
To make drag and drop more intuitive, we can add some styles and interaction effects to drag elements.
Can bedragging
Add logic to the event to limit the drag range, or instartDragging
Add some animation effects to the event.
<style scoped> .draggable { position: absolute; cursor: move; border: 1px solid #ccc; padding: 10px; background-color: #f0f0f0; transition: transform 0.3s; } . { transform: scale(1.1); } </style>
In the above example code, we add an enlarged animation effect to the drag element, making the drag process more vivid.
Step 3: Add drag and drop restrictions
Sometimes, we may need to limit the range of drag elements to avoid dragging out of bounds.
We can achieve this function by judging the position of the element.
methods: { dragging(e) { if () { let posX = - ; let posY = - ; if (posX > 0 && posY > 0) { = posX; = posY; } } }, }
In the above example code, we restrict the drag element from moving inside the parent element to avoid exceeding the boundary. You can also customize the logic of drag and drop restrictions according to your needs.
Through the above three steps, we have implemented the drag and drop function in Vue.
When the user presses the element with the mouse, the element can be dragged freely. This interaction method can increase the user experience and make the interface more dynamic and easy to use.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.