Hasty Briefsbeta

Bilingual

Beej's Bit Bucket

a day ago
  • This demo explains building a custom drag-and-drop system with JavaScript and CSS, using jQuery for ease while implementing core functionality manually.
  • The system moves elements by setting their CSS 'position' to 'absolute' and updating 'left' and 'top' properties relative to the container.
  • Drag mode is initiated on mousedown, items follow the mouse during move, and on mouseup, the drop is processed with collision detection on droptargets.
  • To avoid the element's corner sticking to the mouse, the system remembers the mouse offset on the element at click and adjusts positioning accordingly.
  • Mouse coordinates are converted from document space to the container's coordinate system for proper absolute positioning within the container.
  • Elements with class 'stayinparent' are restricted to their parent container, while others can move freely across the document.
  • Drop containers are marked with class 'droptarget'; on drop, collisions are checked and a 'drop' event is dispatched with the draggable element.
  • Usage involves setting 'draggable' and 'droptarget' classes, calling 'beejdnd.init()', and binding a drop event handler with a draggable reference.
  • Suggestions for improvements include bounding drag areas, axis restriction, clone creation, scroll-on-edge, multitouch support, and jQuery plugin conversion.
  • The code is licensed under the MIT open source license.