HTML/CSS Draggable elements

Der Zinger

Good day. I am trying to build an application with pretty simple HTML/CSS-based draggable tree representation.

<div class="tree">
  <div class="leaf" draggable=true>
    <div class="leaf-handle">item 1</div>
    <div class="leaf subleaf" draggable=true>
      <div class="leaf-handle">item 1.1</div>
      <div class="leaf subsubleaf" draggable=true>
        <div class="leaf-handle">item 1.1.1</div>
      </div>
    </div>
  </div>
  <div class="leaf" draggable=true>
    <div class="leaf-handle">item 2</div>
    <div class="leaf subleaf" draggable=true>
      <div class="leaf-handle">item 2.1</div>
      <div class="leaf subsubleaf" draggable=true>
        <div class="leaf-handle">item 2.1.1</div>
      </div>
    </div>
  </div>
</div>

Code example reproducing the case can be found here: https://codepen.io/DerZinger/pen/bGQbEmd

Issue I'm facing is that when I try to drag a branch (item with subitems) the "ghost" image (representation of draggable elements) have non-transparent background, which I cannot remove regardless of what I try.

Like this:

enter image description here

And I really need it to be non-visible (ghost image showing only leaf-handles of the branch, that is currently dragged).

Any help or guidance on HOW that can be achieved would be very appreciated.

Edit 1: Someone suggested removing background color. Tried that, and unfortunately that does not work, and that's the whole point. Removing it, setting it opaque, and whatever - all these tricks do not ultimately "hide" that area which I want to hide: enter image description here

underflow

After testing several workarounds with your problem and doing online research about it, I came up finally with a decent solution; not the perfect one but the best available today.

Code:

document.addEventListener("dragstart", function (event) {
  document.querySelector(".tree").style.backgroundColor = "transparent";
  event.target.style.background = "transparent";
  setTimeout(() => {
    document.querySelector(".tree").style.backgroundColor = "red";
    event.target.style.background = "yellow";
  }, 100);
});
.leaf {
  background-color: yellow;
  width: 200px;
}
.leaf-handle {
  background-color: green;
  border: 1px solid black;
}
.tree {
  background-color: red;
  display: flex;
  flex-direction: column;
}

.subleaf {
  padding-left: 30px;
}
.subsubleaf {
  margin-left: 60px;
}
<div class="tree">
  <div class="leaf" draggable="true">
    <div class="leaf-handle">item 1</div>
    <div class="subleaf">
      <div class="leaf-handle">item 1.1</div>
      <div class="subsubleaf">
        <div class="leaf-handle">item 1.1.1</div>
      </div>
    </div>
  </div>
  <div class="leaf" draggable="true">
    <div class="leaf-handle">item 2</div>
    <div class="leaf subleaf" draggable="true">
      <div class="leaf-handle">item 2.1</div>
      <div class="leaf subsubleaf" draggable="true">
        <div class="leaf-handle">item 2.1.1</div>
      </div>
    </div>
  </div>
</div>
<div class="tree">
  <div class="leaf" draggable="true">
    <div class="leaf-handle">item 1</div>
    <div class="leaf subleaf" draggable="true">
      <div class="leaf-handle">item 1.1</div>
      <div class="leaf subsubleaf" draggable="true">
        <div class="leaf-handle">item 1.1.1</div>
      </div>
    </div>
  </div>
  <div class="leaf" draggable="true">
    <div class="leaf-handle">item 2</div>
    <div class="leaf subleaf" draggable="true">
      <div class="leaf-handle">item 2.1</div>
      <div class="leaf subsubleaf" draggable="true">
        <div class="leaf-handle">item 2.1.1</div>
      </div>
    </div>
  </div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Reset jquery draggable elements

Contain draggable to multiple elements

About draggable elements in jquery

Elements in html not draggable even if draggable=true

'jQuery UI Draggable' prevents some clicks on elements inside the draggable container

Programmatically reorder or move elements in Draggable + Packery (Masonry)

jQueryUI Sortable duplicating elements after reinitialising draggable

Restrict drop areas to some draggable elements

newly created dom elements cannot be draggable

How do you make prepended elements draggable

jQuery draggable / sortable on dynamically-created elements

Pure javascript: Set border for draggable elements

How to make draggable elements replicate after drag?

Make elements inside an iframe draggable and resizable

jquery ui draggable sortable elements into an iframe

html5 - Draggable Input Elements

Jquery draggable snap only to outmost elements

Is there a way to make components (not elements) draggable in angular?

How to make two elements draggable in the same class

draggable without jquery ui: how to stop dragging the not-draggable and type-able elements?

How to make dynamically added list elements draggable in jquery?

Draggable floating window in android app : Not focusing on layout elements

How to make draggable div elements not stack upon each other

Change the tooltip value of draggable element when over droppable elements jquery

JQuery-UI draggable elements appear on top of others when they are dropped

Sortable panels, removing draggable functionality from certain elements within

jQuery droppable and draggable elements do not see each other

How to drag elements inside a Stack using Draggable in Flutter?

how do i make dynamically created elements draggable()?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive