Example of using javascript to drag and swap div positions

Example of using javascript to drag and swap div positions

1 Implementation Principle

This is done using the dragstart/ondragover/ondrop events of the DOM element. The dragged element is obtained when the drag starts, and then dragging is allowed. Finally, the mouse is lifted and dropped to the new position. The event.preventDefault() method is used here. Many people may be confused. Here is a brief introduction

event.preventDefault(): This method prevents the browser from executing the default action associated with the event.

We use it in the dragover event, because the default action associated with dragover is to prevent data or elements from being placed in other elements; so we need to prohibit the default event through event.preventDefault(), so that we can allow the element to be dragged to a new position.

<!-- Drag div to change the order, applicable to switching layer order in gis -->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title></title>
  <style type="text/css">
        body{
            display: flex;
            padding: 100px;
            flex-direction: column;
        }
        div{
            width: 100px;
            height: 100px;
            text-align: center;
        }
    </style>
 
</head>
<body>
   <div style="background-color: red;width:200px;height:200px;" draggable="true">Red</div>
    <div style="background-color: green;width:100px" draggable="true">Green</div>
    <div style="background-color: blue;" draggable="true">Blue</div>
</body>
<script type="text/javascript">
    let div = document.getElementsByTagName("div");
    let container = null;
    // Traverse and bind dragstart, dragover and drop events to each div for(let i=0;i<div.length;i++){
        div[i].ondragstart=function(){  
            container=this
        }
        div[i].ondragover=function(){
            event.preventDefault();
        }
        div[i].ondrop=function(){
            debugger;
            if(container!=null&&container!=this){
                // The specific idea is the same as variable value exchange let temp=document.createElement("div");
                document.body.replaceChild(temp,this); //Use the newly created div to occupy the destination positiondocument.body.replaceChild(this,container); //The destination div is placed at the starting positiondocument.body.replaceChild(container,temp) //The starting div is placed at the destination positiondebugger; 
                console.log('Execute business logic')
            }
        }
    }
</script>
</html> 

This is the end of this article about the implementation example of javascript dragging and swapping div positions. For more relevant javascript dragging and swapping div content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of how to integrate swagger components in java
  • Optimization of common sorting algorithms in Java
  • Use of jackson and fastjson, common parsing tools in Java
  • How to use Filter in Java
  • Java integrated swagger document component

<<:  Detailed explanation of how MySQL solves phantom reads

>>:  Detailed explanation of Nginx rewrite jump application scenarios

Recommend

Sample code for deploying Spring-boot project with Docker

1. Basic Spring-boot Quick Start 1.1 Quick start ...

How to modify the default submission method of the form

The default submission method of html is get inste...

Detailed explanation of mixed inheritance in Vue

Table of contents The effect of mixed inheritance...

HTML table markup tutorial (10): cell padding attribute CELLPADDING

Cell padding is the distance between the cell con...

Tomcat configuration and how to start it in Eclipse

Table of contents How to install and configure To...

Two implementation codes of Vue-router programmatic navigation

Two ways to navigate the page Declarative navigat...

Detailed tutorial on Docker pulling Oracle 11g image configuration

Without further ado Start recording docker pullin...

How to compile and install opencv under ubuntu

Easy installation of opencv2: conda install --cha...

Do you know the difference between empty value and null value in mysql

Preface Recently I found that my friend's met...

Java uses Apache.POI to export HSSFWorkbook to Excel

Use HSSFWorkbook in Apache.POI to export to Excel...

Share the pitfalls of MySQL's current_timestamp and their solutions

Table of contents MySQL's current_timestamp p...

JavaScript custom plug-in to implement tab switching function

This article shares the specific code of JavaScri...

HTML table markup tutorial (4): border color attribute BORDERCOLOR

To beautify the table, you can set different bord...

MySQL 8.0.22 winx64 installation and configuration method graphic tutorial

The database installation tutorial of MySQL-8.0.2...

Analysis and solution of a MySQL slow log monitoring false alarm problem

Previously, for various reasons, some alarms were...