Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <main>
  <h2>Drag and Drop API Example with<br><code>effectAllowed</code> and <code>dropEffect</code></h2>
  
  <p><b>This seems to work correctly only in Firefox.</b> Each small box can be dragged and dropped in one of three ways: Normal drag, CTRL-Drag (or OPTION-Drag on Mac), and CTRL-SHIFT-Drag (or OPTION-CMD-Drag on Mac). Depending on the drop area you use, the drag operation may not be possible. This is done using the <code>effectAllowed</code> property of the <code>dataTransfer</code> object.</p>
  
  <div class="wrap">
    <div class="box draggable" id="move" draggable="true">Just Drag to the box below</div>
    <div class="box draggable" id="copy" draggable="true">CTRL-Drag (or OPT-DRAG on Mac) to the box below</div>
    <div class="box draggable" id="link" draggable="true">CTRL-SHIFT-Drag (or OPT-CMD-Drag on Mac) to the box below</div>
  </div>
  
  <div class="wrap">
    <div class="dropzone" id="dropzone">"Move" Dropzone</div>
    <div class="dropzone" id="dropzone2">"Copy" Dropzone</div>
    <div class="dropzone" id="dropzone3">"Link" Dropzone</div>
  </div>

</main>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  font-family: Arial, sans-serif;
  font-size: 20px;
  padding: 0 20px;
  line-height: 1.4;
}

main {
  text-align: center;
  margin: 0 auto;
  max-width: 800px;
}

.wrap {
  display: flex;
  justify-content: space-around;
  margin-bottom: 20px;
}

p {
  text-align: left;
  padding: 0 20px;
}

code {
  color: firebrick;
  font-weight: bold;
}

.box {
  width: 200px;
  height: 200px;
  background: #ffd485;
  float: left;
  border-radius: 20px;
  cursor: grab;
  padding: 20px;
}

.box:active {
  cursor: grabbing;
}

.dropzone {
  width: 250px;
  height: 250px;
  background: deepskyblue;
  padding: 20px;
  border-radius: 20px;
}

.droppable {
  background-color: black;  
}


              
            
!

JS

              
                console.clear();

// the IDs are in the global scope so I don't need to define them here, but I've done it anyway, just for clarity
let move = document.getElementById('move'),
    copy = document.getElementById('copy'),
    link = document.getElementById('link'),
    boxes = document.querySelectorAll('.box'),
    dropzones = document.querySelectorAll('.dropzone'),
    dropzone = document.getElementById('dropzone'),
    dropzone2 = document.getElementById('dropzone2'),
    dropzone3 = document.getElementById('dropzone3');

for (i of boxes) {
  i.addEventListener('dragstart', function(e) {
    e.dataTransfer.setData('text/plain', e.target.id);
  });
}

for (i of dropzones) {
  i.addEventListener('dragover', function(e) {
    e.preventDefault();
    e.dataTransfer.effectAllowed = e.dataTransfer.getData('text/plain');
    e.target.classList.add('droppable');
    // below log is determined by the type of
    // drag initated by the user
    console.log(e.dataTransfer.dropEffect);
  });
}

dropzone.addEventListener('drop', function (e) {
  e.preventDefault();
  if (e.dataTransfer.getData('text/plain') === 'move') {
    e.target.appendChild(move);
    move.draggable = false;
  }
});

dropzone2.addEventListener('drop', function (e) {
  e.preventDefault();
  if (e.dataTransfer.getData('text/plain') === 'copy') {
    e.target.appendChild(copy.cloneNode(true));
    copy.draggable = false;
  }
});

dropzone3.addEventListener('drop', function (e) {
  e.preventDefault();
  if (e.dataTransfer.getData('text/plain') === 'link') {
    e.target.appendChild(link.cloneNode(true));
    link.draggable = false;
  }
});

for (i of boxes) {
  i.addEventListener('dragend', function(e) {
    for (j of dropzones) {
      j.classList.remove('droppable');
    }
    for (k of document.querySelectorAll('.box')) {
      k.classList.remove('droppable');
    }
  });
}
              
            
!
999px

Console