Pen Settings



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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


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


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.


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.


                <div id="game">
  drag and drop pointermove
  <div id="hand" class="hand">
    <div id="card_1" class="card"></div>
    <div id="card_2" class="card"></div>
    <div id="card_3" class="card"></div>


                .card {
   border-radius: 5%;
   width: 10em;
   height: 13.5em;
   box-shadow: 0.1em 0.1em 0.2em 0.1em #555;

#card_1 {
  background-color: red;
#card_2 {
  background-color: blue;
#card_3 {
  background-color: green;

.hand .card {
  display: inline-block;

.hand {  
  background-color: #ccc;
  padding: 10px;

/* d&d specific*/

.hand {
  user-select: none; 
  touch-action: none;

.drag-active {
  /* opacity: 0.2; */
  outline: solid 1px black;
.drag-active-container {
    background-color: #fcc;




function userPressed(event) {
  element =;
  if (element.classList.contains('card')) {
    startX = event.clientX;
    startY = event.clientY;

    container.addEventListener('pointermove', userMoved, { passive: true });
    container.addEventListener('pointerup', userReleased, { passive: true });
    container.addEventListener('pointercancel', userReleased, { passive: true });

function userMoved(event) {
  // if no previous request for animation frame - we allow js to proccess 'move' event:
  if (!raf) {
    deltaX = event.clientX - startX;
    deltaY = event.clientY - startY;
    raf = requestAnimationFrame(userMovedRaf);

function userMovedRaf() { = "translate3d("+deltaX+"px,"+deltaY+"px, 0px)";
  // once the paint job is done we 'release' animation frame variable to allow next paint job:
  raf = null;

function userReleased(event) {
  container.removeEventListener('pointermove', userMoved);
  container.removeEventListener('pointerup', userReleased);
  container.removeEventListener('pointercancel', userReleased);
  // if animation frame was scheduled but the user already stopped interaction - we cancel the scheduled frame:
  if (raf) {
    raf = null;

  dropItemOntoXY(element,  deltaX + startX, deltaY + startY); 

  deltaX = deltaY = null;

function dropItemOntoXY(selectedItem,x,y) {


  const hand = selectedItem.parentNode;
  //console.log(selectedItem + " (" + x + "," + y + ")");
  const pointsTo = document.elementFromPoint(x, y);


  if (pointsTo === selectedItem || pointsTo === null) {
    // do nothing
  } else if (hand === pointsTo) {
  } else if (pointsTo === hand.children[0]) {
    hand.insertBefore(selectedItem, pointsTo);
  } else if (hand === pointsTo.parentNode) {
    hand.insertBefore(pointsTo, selectedItem);
  } else {
    console.error('Cannot determin target for drop',;

const container = document.querySelector('#hand');
container.addEventListener('pointerdown', userPressed, { passive: true });

var element,  startX, startY, deltaX, deltaY, raf;