Simple implementation of vue drag and drop

Simple implementation of vue drag and drop

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.
dragstart: Executed when the mouse is clicked and moved.
drag: After dragstart is executed, it is triggered continuously while the mouse is moving.
dragend: triggered when the dragging behavior ends, that is, when the mouse is released.
dragenter: Triggered when the marker of the element being dragged enters a DOM element, and it will be triggered first. The entered DOM element triggers this event.
dragover: Triggered when the dragged element's marker moves on the incoming DOM element, and also when it moves itself.
dragleave: Triggered when the dragged element leaves the entered DOM.

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.
DataTransfer object: This property is used to save the dragged and dropped data and interaction information. This component does not use it and is temporarily ignored.

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:
  • Implementing drag and drop function based on Vue
  • Realizing drag effect based on Vue
  • How to configure and use the Vue.Draggable drag function
  • Vue.Draggable realizes the drag effect
  • Implementing smooth transition drag and drop sorting function based on Vue
  • Vue custom directive drag function example
  • Example of drag and drop function implemented in vue2.0 using Sortable.js
  • Detailed explanation of draggable tree table example based on Vue
  • Vue develops drag progress bar sliding component

<<:  Implementation example of specifying container ip when creating a container in docker

>>:  The marquee tag in HTML achieves seamless scrolling marquee effect

Recommend

Super detailed MySQL usage specification sharing

Recently, there have been many database-related o...

JavaScript implements random generation of verification code and verification

This article shares the specific code of JavaScri...

How to use Nginx to prevent IP addresses from being maliciously resolved

Purpose of using Nginx Using Alibaba Cloud ECS cl...

Some indicators of excellent web front-end design

The accessibility of web pages seems to be somethi...

Detailed tutorial on how to automatically install CentOS7.6 using PXE

1. Demand The base has 300 new servers, and needs...

Solve the problem of docker pull image error

describe: Install VM under Windows 10, run Docker...

Detailed explanation of various ways to merge javascript objects

Table of contents Various ways to merge objects (...

A brief discussion on several advantages of Vue3

Table of contents 1. Source code 1.1 Monorepo 1.2...

Basic steps to use Mysql SSH tunnel connection

Preface For security reasons, the root user of My...

Docker learning method steps to build ActiveMQ message service

Preface ActiveMQ is the most popular and powerful...

MySQL 5.7.10 Installation Documentation Tutorial

1. Install dependency packages yum -y install gcc...

MySQL database table and database partitioning strategy

First, let's talk about why we need to divide...