Skip to content

Subscribe and get 10% off your next order now — Exclusions apply – see T&Cs

Columns with images

February 2, 2023

Drag and Drop API keynotes

by Leo Huynh

I created an example about Javascript Drag-n-Drop APIs at https://hta218.github.io/dnd-keynotes

Below are some interesting key-notes I've learned about the APIs.

Basic concepts

  • A typical drag operation begins when a user selects a draggable element, drags the element to a dropzone, and then releases the dragged element.

  • Events:

Event Fires when…
drag …a dragged item (element or text selection) is dragged.
dragend …a drag operation ends (such as releasing a mouse button or hitting the Esc key)
dragenter …a dragged item enters a valid drop target.
dragexit …an element is no longer the drag operation's immediate selection target.
dragleave …a dragged item leaves a valid drop target.
dragover …a dragged item is being dragged over a valid drop target, every few hundred milliseconds.
dragstart …the user starts dragging an item.
drop …an item is dropped on a valid drop target.

Keynotes

  • To make an element draggable, add draggable="true" attribute

    <div draggable="true">This element is draggable</div>
    
  • dragstart

    • dragstart is the first event fired when a drag operation starts on a draggable element

    • use e.dataTransfer.setData() method to set any drag's data, this will stay during the drag operation

    // The `dragstart` event fires on the `draggable` element
    dragElem.addEventListener('dragstart', function (e) {
      // We can set data using `e.dataTransfer.setData` method
      e.dataTransfer.setData('text/plain', e.target.id)
      // Use e.dataTransfer.setDragImage() to change the drag image
      // e.dataTransfer.setDragImage(img | element, xOffset, yOffset)
    })
    
    • If you don't want the translucent image generated from the drag target during drag, use e.dataTransfer.setDragImage() to change it
  • The dropEffect

    • The dropEffect property is used to control the feedback the user is given during a drag-and-drop operation
    dragElem.addEventListener('dragstart', function (e) {
      e.dataTransfer.setData('text/plain', e.target.id)
      // The `move` value works on window, not on macOS - It might be the problem of browser along with OS
      e.dataTransfer.dropEffect = 'move' // or "copy"
    })
    
    • dropEffect property could be:

      1. move: dragged data will be moved to the dropzone.
      2. copy: dragged data will be copied to the dropzone.
      3. ..
  • The dropzone

    • To make an element becomes a dropzone, it must have both dragover and drop event handler.

    • Remember to call e.preventDefault() in dragover handler or the browser won't let you drop anything inside

    dropzone.addEventListener('dragover', function handleDragOver(e) {
      // The `dropzone` element must have both `dragover` and `drop` event
      // Remember to preventDefault the behavior or the browser or it will not let you drop anything inside
      e.preventDefault()
      e.dataTransfer.dropEffect = 'move'
    })
    dropzone.addEventListener('drop', function handleDrop(e) {
      // NOTE: there must be a handler for dragover to use drop event
      e.preventDefault()
      // Use `e.dataTransfer.getData` method to retrieve drag's data and process them
      let data = e.dataTransfer.getData('text/plain')
      // NOTE: Keep mind that we can only use the `dataTransfer.getData()` in the `drop-handler`
      // `getData()` will return empty string inside handle dragover or dragenter
    })
    
    • Keep mind that we can only use the dataTransfer.getData() in the drop-handler (it will return empty string inside dragover or dragenter handler)
  • dragend

    • The dragend event fires after a drag operation finished regardless of whether the drag completed or was canceled
    // The `dragend` event fired on the `draggable` element (not the dropzone element)
    dragElem.addEventListener('dragend', function handleDragEnd(e) {
      // We can check if the drag was successful or not by checking the ʻe.dataTransfer.dropEffect` value
      let dropEffect = e.dataTransfer.dropEffect
      // If that fails, the value of `e.dataTransfer.dropEffect` will be "none"
    })
    
    • If the drag operation failed, the value of e.dataTransfer.dropEffect will be "none"
Tags:javascript, leo-blog, technical
Share:

Featured products

Picture of Long-sleeve Maxi Dress
Second picture of Long-sleeve Maxi Dress
Long-sleeve Maxi Dress
₫2,845,000
RRP:
$999
(-60%)
Picture of Tinted Sunglasses
Second picture of Tinted Sunglasses
Tinted Sunglasses
₫269,000
RRP:
$999
(-60%)
Picture of Quilted Shoulder Bag
Second picture of Quilted Shoulder Bag
Quilted Shoulder Bag
₫591,000
RRP:
$999
(-60%)
Picture of Round sunglasses
Second picture of Round sunglasses
Round sunglasses
₫269,000
430,000-37%
RRP:
$999
(-60%)
-37% Off
Picture of Travel Pack
Second picture of Travel Pack
Travel Pack
₫6,709,000
7,380,000-9%
RRP:
$999
(-60%)
-9% OffBest Seller
Picture of Unisex Fila Classic Kicks
Second picture of Unisex Fila Classic Kicks
Unisex Fila Classic Kicks
₫403,000
RRP:
$999
(-60%)
gift card product
₫268,344
RRP:
$999
(-60%)
Books bundle
₫1,208,000
1,342,000-10%
RRP:
$999
(-60%)
Bundle-10% OffBest Seller
Picture of Linen-blend Shirt
Second picture of Linen-blend Shirt
Linen-blend Shirt
₫457,000
RRP:
$999
(-60%)
Picture of Drawstring T-shirt Dress
Second picture of Drawstring T-shirt Dress
Drawstring T-shirt Dress
₫510,000
RRP:
$999
(-60%)
Picture of Boxy Denim Jacket
Second picture of Boxy Denim Jacket
Boxy Denim Jacket
₫671,000
RRP:
$999
(-60%)
Picture of Linen Check Blazer
Second picture of Linen Check Blazer
Linen Check Blazer
₫671,000
RRP:
$999
(-60%)
Picture of Oversized T-shirt
Second picture of Oversized T-shirt
Oversized T-shirt
₫296,000
376,000-21%
RRP:
$999
(-60%)
-21% Off
Picture of Rounded Sunglasses
Second picture of Rounded Sunglasses
Rounded Sunglasses
₫215,000
RRP:
$999
(-60%)
Picture of Polarised Sunglasses
Second picture of Polarised Sunglasses
Polarised Sunglasses
₫484,000
564,000-14%
RRP:
$999
(-60%)
-14% Off
Picture of Quilted Shopper
Second picture of Quilted Shopper
Quilted Shopper
₫591,000
RRP:
$999
(-60%)

Image Gallery

Showcase your chosen images. This visual focus will enhance user engagement and understanding of your offerings.

https://cdn.shopify.com/s/files/1/0838/0052/3057/files/fpo-product-watch-3-1024x1024.svg
https://cdn.shopify.com/s/files/1/0838/0052/3057/files/fpo-product-lamp-2-1024x1024.svg

Testimonials

We are a team of passionate people whose goal is to improve everyone's life through disruptive products. We build great products to solve your business problems.

Shopify Headless Game Changer

I run a Shopify development agency and this is the kind of tool I've been looking for. Clients do not understand why headless is rather expensive to build but having a tool/option like this is a game changer.

Glen P.
Glen P.
Founder, eCom Graduates
Intuitive Tool with Big Plus

I love how intuitive the tool is. It looks very promising for my potential clients, and being able to easily use meta objects with this is a big plus.

Tom H.
Tom H.
Owner, On The Road UK