Arrange list items using drag and drop jquery

Reorder list items using drag and drop jquery

Arrange list items using drag and drop jquery

Hello, Today I am going to show you one amazing jquery example for Arrange list items using drag and drop jquery. DragonflyJS is a tiny vanilla JavaScript library that enables sorting (dragging and dropping) functionality with zero dependencies.

Create a DOM structure, based on your desired elements:

<div class="drag-container">
    <div class="drag-box" id="item1">Item #1</div>
    <div class="drag-box" id="item2">Item #2</div>
    <div class="drag-box" id="item3">Item #3</div>
    <div class="drag-box" id="item4">Item #4</div>
    <div class="drag-box" id="item5">Item #5</div>
    <div class="drag-box" id="item6">Item #6</div>
    <div class="drag-box" id="item7">Item #7</div>
    <div class="drag-box" id="item8">Item #8</div>
    <div class="drag-box" id="item9">Item #9</div>
    <div class="drag-box" id="item10">Item #10</div>
    <div class="drag-box" id="item11">Item #11</div>
    <div class="drag-box" id="item12">Item #12</div>
</div>

 

Call it using the dragonfly(element) function:

document.addEventListener("DOMContentLoaded", function () {
    'use strict';
    dragonfly('.drag-container', function () {
        console.log('This is a callback');// you can do whatever you want
    });
});

 

Let's see a full example:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Drag & Drop list item</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<style>
.drag-container {
    box-sizing: border-box;
    border: 1px solid #cccccc;
    border-radius: 3px;
    width: 400px;
    padding: 8px;
    margin: 0 auto;
}
.drag-box {
    box-sizing: border-box;
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: 3px;
    padding: 4px 8px;
    margin: 4px;
    cursor: pointer;
    background-color: #eeeeee;
}
.drag-box:hover {
    background-color: #ffff99;
}

.drag-helper {
    box-sizing: border-box;
    position: absolute;
    display: none;
    top: auto;
    left: auto;
}
.drag-box-dragging .drag-box {
    background-color: #ff99cc;
    opacity: 0.5;
    transform: scale(1.5);
}
</style>
</head>
<body>

<div class="drag-container">
    <div class="drag-box" id="item1">Item #1</div>
    <div class="drag-box" id="item2">Item #2</div>
    <div class="drag-box" id="item3">Item #3</div>
    <div class="drag-box" id="item4">Item #4</div>
    <div class="drag-box" id="item5">Item #5</div>
    <div class="drag-box" id="item6">Item #6</div>
    <div class="drag-box" id="item7">Item #7</div>
    <div class="drag-box" id="item8">Item #8</div>
    <div class="drag-box" id="item9">Item #9</div>
    <div class="drag-box" id="item10">Item #10</div>
    <div class="drag-box" id="item11">Item #11</div>
    <div class="drag-box" id="item12">Item #12</div>
</div>

<script src="dragonfly.js"></script>
</body>
</html>
Top