How To Make Divs Flow Smoothly With CSS

Here is the code snippet I am working with:

/* Coded by @fitri
 This ReactJS project component was created with love 
 https://codepen.io/fitri/full/oWovYj/ */

function enableDragSort(listClass) {
  const sortableLists = document.getElementsByClassName(listClass);
  Array.prototype.map.call(sortableLists, (list) => {enableDragList(list)});
}

function enableDragList(list) {
  Array.prototype.map.call(list.children, (item) => {enableDragItem(item)});
}

function enableDragItem(item) {
  item.setAttribute('draggable', true)
  item.ondrag = handleDrag;
  item.ondragend = handleDrop;
}

function handleDrag(item) {
  const selectedItem = item.target,
        list = selectedItem.parentNode,
        x = event.clientX,
        y = event.clientY;
  
  selectedItem.classList.add('drag-sort-active');
  let swapItem = document.elementFromPoint(x, y) === null ? selectedItem : document.elementFromPoint(x, y);
  
  if (list === swapItem.parentNode) {
    swapItem = swapItem !== selectedItem.nextSibling ? swapItem : swapItem.nextSibling;
    list.insertBefore(selectedItem, swapItem);
  }
}

function handleDrop(item) {
  item.target.classList.remove('drag-sort-active');
}

(()=> {enableDragSort('drag-sort-enable')})();
html,
body {
  height: 100%;
}

.container {
  margin: 20px auto 0;
  max-width: 500px;
  word-wrap:break-word
}

* {
  box-sizing: border-box;
}

ul {
  margin: 0;
  padding: 0;
}

li {
  margin: 5px 0;
  padding: 0 20px;
  height: 40px;
  line-height: 40px;
  border-radius: 3px;
  background: #136a8a;
background: -webkit-linear-gradient(to right, #267871, #136a8a);
background: linear-gradient(to right, #267871, #136a8a);
  color: #fff;
  list-style: none;
}

li.drag-sort-active {
  background: transparent;
  color: transparent;
  border: 1px solid #4ca1af;
}

span.drag-sort-active {
  background: transparent;
  color: transparent;
}
<div class="container">
  <h3>Drag and Sort :</h3>
  <div class="drag-sort-enable">
  </div>
  <hr>
  <ul class="drag-sort-enable">
    <li>Application</li>
    <li>Blank</li>
    <li>Class</li>
    <li>Data</li>
    <li>Element</li>
  </ul>
</div>

Is there a way to add animation to the drag-and-drop feature so that when an item is dragged over another, it smoothly moves aside to make space? If not, how can this be achieved through CSS or JS code?

Any suggestions would be greatly appreciated! Thanks in advance!

Answer №1

Have you come across any specific resource, gif, or website demonstrating that particular behavior? Sharing the intended animation with us could allow for more accurate assistance. I want to make sure I grasp the situation correctly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The computed value in Vue.js does not execute during initialization

Every time my vm is created, the computed values get function fails to run, leaving the value unassigned. Oddly enough, it does run when I attempt to access the value, just not during the initial app startup. Essentially, the goal is to calculate the ski ...

Angular tutorial on splitting a JSON array

I am looking to extract a portion of a JSON array in Angular. The array looks like the one in the image below.https://i.stack.imgur.com/w0hqC.png Below is the code snippet: export class AppComponent { color: string = 'green'; public stocklis ...

limiting the number of HTTP requests within a JavaScript forEach loop

In my current coding situation, I am facing an issue where the HTTP requests are being made simultaneously within a forEach loop. This leads to all the requests firing off at once. const main = async () => { items.forEach(async (i: Item) => ...

Issues arise with Ajax/PHP not displaying subsequent posts after the initial one

Need help with a settings page for users? One issue I'm encountering is that after submitting the form once and receiving an error message like "Please fill in all fields," subsequent submissions do not display any additional errors or success message ...

Tips for sending a JavaScript object to a Java Servlet

I'm facing a challenge with passing a JavaScript object to a Java Servlet. I've experimented with a few approaches, but nothing has worked so far. Below is the code snippet I've been working on: $.ajax({ url: 'TestUrl', d ...

Encountering a 500 internal server error when utilizing jQuery and Ajax with Laravel 5.2

I have been working on a post editing system using jQuery and Ajax in Laravel 5.2. But when I try to save the changes by clicking on the button inside my Bootstrap modal, an error pops up: Error: POST http://localhost:8000/edit 500 (Internal Server Error) ...

Step-by-step guide to automatically submitting a form after obtaining geolocation without the need for manual button-clicking

I am looking for a way to automatically call a page to get geolocation data, and then submit the form to my PHP page with the values of getlat and getlon without the need to click a submit button. I have tried implementing the code below, however, despit ...

Want to know how to center the value of a slider range input in your React project using NextJS and Tailwind

Can someone help me with the issue I'm having with the offset? Is there a way to link a value to the thumb? I've tried two methods, but one gives a significant offset and the other gives less. However, it seems more like a workaround than a solu ...

Encountering a SyntaxError: Unexpected token < in the initial position of JSON during JSON parsing within Node.js

const express = require("express"); const bodyParser = require("body-parser"); const request = require("request"); const https = require("https") ; const app = express(); // store static files (e.g css,image files) ...

Error message received from Express middleware: "Unable to modify headers after they have been sent."

I have created middleware for my Node Express app to perform the following tasks: Checking all incoming requests to determine if they are from a bot Allowing requests for raw resources to proceed Serving an HTML snapshot if the request is from a bot How ...

What is the best way to track the initial rendering speed of a React application as it evolves?

My goal is to assess the performance of our react app by running a node script on our CI server with production size data. The objective is to track the performance of our project as the codebase evolves over time. After researching, I was surprised to fi ...

Is there a way to extract a single row from a database with each button click?

I am working on a system for selecting items and need assistance with iterating through a table one item at a time upon clicking a button. Currently, the button only displays the first item in the table. How can I modify it to cycle through each item one b ...

What are the reasons behind individuals using relative directories with a dot or dot slash, while others do not employ this method?

I've always wondered why there are three different ways to write the same thing. It may be a silly question, but it's been on my mind. For example, displaying an image named photo1.jpg from the img directory: index.html <img src="images/img ...

Is it better to conceal modals or completely eliminate them from the DOM when working with React?

I am currently developing a React application that involves multiple modals, with only one active at a time and no nested modals. I am torn between two approaches for handling the showing and hiding of these modals: The first approach involves having a b ...

Expanding sidebar height to match parent using Flexbox

Looking for the best way to adjust a child element to match the height of its parent? I've experimented with various techniques, but haven't had much success. I'm leaning towards using flexbox as it seems to be the latest trend. Here's ...

What are the steps to showcase a webcam stream in GLSL by leveraging Three.js?

One method of displaying a webcam video using ThreeJS involves creating a video texture, as shown below: video = document.getElementById( 'video' ); const texture = new THREE.VideoTexture( video ); texture.colorSpace = THREE.SRGBColorSpace; const ...

Do we still need Client Side Validation if Ajax is not being implemented?

I recently came across an interesting point made here regarding the primary purpose of client side validation: allowing the user to correct input errors before submitting the form The argument is that if the client side validation relies solely on Java ...

The initial update of ng-model is not occurring

I am currently working on a checkbox functionality in my project that is bound to an ng-model: <input type="checkbox" ng-change="toggleAll(globalChecked)" ng-model="globalChecked"> The toggleAll function is responsible for accessing the globalCheck ...

Optimizing Bootstrap 4 Flex-Row Dimensions

Issue: I'm currently working on a relatively intricate ListGroup using bootstrap v4. The .flex-row div is displaying some mysterious white space at the bottom, and I need assistance in eliminating it. Attempts Made So Far: I've experimented w ...

performing an AJAX request to the controller method in a Rails application

I am currently facing an issue with making an ajax call to my controller class PatientRecordController < ApplicationController def export .... end end Within my javascript file, the code snippet is as follows: $(document).ready(function(){ ...