Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="wrapper">
  <div id="left-column">
    <h3>Draggable items</h3>
    <div class="draggable-item">Draggable item 1</div>
    <div class="draggable-item">Draggable item 2</div>
    <div class="draggable-item">Draggable item 3</div>
  </div>
  <div id="right-column">
    <h3>Drop areas</h3>
    <div class="drop-area"></div>
    <div class="drop-area"></div>
  </div>
</div>
              
            
!

CSS

              
                /* Sample related CSS */
body {
  background: #ccc;
}

#left-column,
#right-column {
  background: #fff;
  min-height: 300px;
}

#wrapper {
  display: flex;
  width: 100%px;
  justify-content: space-between;
}

#left-column {
  width: 40%;
}

#right-column {
  width: 40%;
}

#left-column h3,
#right-column h3 {
  text-align: center;
}

/* Drag and drop related CSS */
.draggable-item {
  padding: 10px;
  background: #fff;
  box-sizing: border-box;
}

.draggable-item.animatable {
  transition: transform linear 400ms, opacity linear 400ms;
}

.draggable-item.active {
  position: fixed;
  pointer-events: none;
  left: 0;
  top: 0;
  will-change: transform;
  z-index: 1;
}

.keeper {
  opacity: 0;
}

.drop-area {
  width: 100%;
  margin-top: 10px;
  min-height: 44px;
  position: relative;
}

.drop-area:after {
  position: absolute;
  width: 100%;
  height: 100%;
  content: '';
  border: 1px dashed black;
  box-sizing: border-box;
  opacity: 0;
  top: 0;
  left: 0;
}

.drop-area.affordance:after {
  opacity: .3;
  position: absolute;
}

.drop-area.affordance.active:after {
  opacity: 1;
}
              
            
!

JS

              
                /* Creates an array of draggable items */
let draggableItems = document.querySelectorAll('.draggable-item');
let dropAreas = document.querySelectorAll('.drop-area');

let initialX;
let initialY;
let offsetX = 0;
let offsetY = 0;
let startingX;
let startingY;
let targetElement;
let lastActiveDropArea = null;

/* Creates and appends an element to keep the space of the moving item */
let createKeeper = function(draggableItem) {
  /* Define variables */
  let draggableItemRect = draggableItem.getBoundingClientRect();
  let keeper = document.createElement('div');

  /* Sets element properties */
  keeper.classList.add('keeper');
  keeper.style.width = draggableItemRect.width + 'px';
  keeper.style.height = draggableItemRect.height + 'px';
  
  /* Appends the elment before the active draggable item */
  draggableItem.parentElement.insertBefore(keeper, draggableItem);
};

let isNewActiveArea = function(currentActiveArea, newActiveArea) {
  if(newActiveArea && !currentActiveArea || newActiveArea && currentActiveArea !== newActiveArea) {
    return true;
  }
};

let setLastActiveDropArea = function(targetDropArea) {
  unsetLastActiveDropArea();
  if(targetDropArea && !targetDropArea.classList.contains('active')) {
    targetDropArea.classList.add('active');
    lastActiveDropArea = targetDropArea;
  }
};

let unsetLastActiveDropArea = function() {
  if(lastActiveDropArea && lastActiveDropArea.classList.contains('active')) {
    lastActiveDropArea.classList.remove('active');
    lastActiveDropArea = null;
  }
};

let resetTransitionEndHandler = function() {
  // Removes the animatable class
  if(this.classList.contains('animatable')) {
    this.classList.remove('animatable');
  }
  
  // Removes the style
  this.removeAttribute('style');
  
  // Removes the keeper
  if(this && this.parentElement && this.parentElement.querySelector('.keeper')) {
    this.parentElement.removeChild(this.parentElement.querySelector('.keeper'));
  }
  
  // Removes active class
  if(this.classList.contains('active')) {
    this.classList.remove('active');
  }
  
  // Moves the element if it was dropped inside a drop area
  if(targetElement && targetElement.classList.contains('drop-area') && this.parentElement) {
    lastActiveDropArea.appendChild(this);
  }
  
  // Removes the transition end event listener
  this.removeEventListener('transitionend', resetTransitionEndHandler);
};

/* Creates a handler for each event listener */
let touchStartHandler = function(event) {
  interactionStart(event.target, event.touches[0].pageX, event.touches[0].pageY);
};

let touchMovetHandler = function(event) {
  interactionMove(event.target, event.touches[0].pageX, event.touches[0].pageY);
};

let touchEndtHandler = function(event) {
  interactionEnd(event.target, event.changedTouches[0].pageX, event.changedTouches[0].pageY);
};

/* Create handlers to each interaction */
let interactionStart = function(element, coordX, coordY) {
  
  // Adds affordance in Drop Areas
  dropAreas.forEach(function(dropArea) {
    if(!dropArea.classList.contains('affordance')) {
      dropArea.classList.add('affordance');
    }
  });
  
  // Defines variables
  let draggableItemRect = element.getBoundingClientRect();
  
  /* Sets initial values */
  initialX = coordX;
  initialY = coordY;
  startingX = draggableItemRect.left;
  startingY = draggableItemRect.top;
  
  /* Configures the size and position of the draggable-item */
  element.style.width = draggableItemRect.width + 'px';
  element.style.height = draggableItemRect.height + 'px';
  element.style.transform = "translateX(" + draggableItemRect.left + "px) translateY( " + draggableItemRect.top + "px) translateZ(0)";
  
  /* Creates the keeper */
  createKeeper(element);
  
  /* Adds active class */
  if(!element.classList.contains('active')) {
    element.classList.add('active');
  }
};

let interactionMove = function(element, coordX, coordY) {
  /* Sets offset values */
  offsetX = coordX - initialX;
  offsetY = coordY - initialY;
  
  /* Transforms the UI to move the element */
  element.style.transform = "translateX(" + (startingX + offsetX) + "px) translateY(" + (startingY + offsetY) + "px) translateZ(0) scale(1)";
  
  // Gets target active drop area
  targetElement = document.elementFromPoint(coordX, coordY);
  
  if(targetElement && targetElement.closest('.drop-area')) {
    targetElement = targetElement.closest('.drop-area');
    setLastActiveDropArea(targetElement);
  } else {
    unsetLastActiveDropArea();
  }
};

let interactionEnd = function(element, coordX, coordY) {
  // Removes affordances
  dropAreas.forEach(function(dropArea) {
    // Removes active class
    if(dropArea.classList.contains('active')) {
      dropArea.classList.remove('active');
    }
    
    // Removes the affordance class
    if(dropArea.classList.contains('affordance')) {
      dropArea.classList.remove('affordance');
    }
  });
  
  // Adds animatable class
  if(!element.classList.contains('animatable')) {
    element.classList.add('animatable');
  }
  
  // Checks if the drop was done inside or outside a drop area
  if(targetElement && targetElement.classList.contains('drop-area')) {
    element.style.transform = "translateX(" + (startingX + offsetX) + "px) translateY(" + (startingY + offsetY) + "px) translateZ(0) scale(0)";
  } else {
    element.style.transform = "translateX(" + startingX + "px) translateY(" + startingY + "px) translateZ(0)";
  }
  
  element.addEventListener('transitionend', resetTransitionEndHandler);
  
};

/* Creates the necessary functions */
let addEventListeners = function(element) {
  /* Touch related events */
  element.addEventListener('touchstart', touchStartHandler);
  element.addEventListener('touchmove', touchMovetHandler);
  element.addEventListener('touchend', touchEndtHandler);
};

/* Executes a function to create all event listeners */
draggableItems.forEach(function(draggableItem) {
  addEventListeners(draggableItem);
});
              
            
!
999px

Console