52 days ago · Tech · hide · Fediverse post

There's a bunch of libraries to facilitate drag-and-drop functionality on the web, but the native functionality built into modern browsers (and actually really well-supported now) is pretty cool and quite simple to use. The basics To start making something draggable, simply add draggable="true" to the element: PreviewCode<div draggable="true" class="drag-me"> Drag me </div> <style> .drag-me { width: 5rem; height: 5rem; border-radius: 1rem; background-color: forestgreen; color: white; display: flex; justify-content: center; align-items: center; cursor: grab; } </style> You will probably want to assign some data to this. To do this, we listen to the dragstart event, and use the event.dataTransfer.setData() method to set some data. You can even set multiple types of data by using various different mime types for the first parameter of that method. Now we want to allow you to drop somewhere. By listening to the drop event on the target element, we can read out the data using…

Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town