HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<article class="page">
<article class="main">
<h1>Native Drag and Drop<br>Data transfer on a single page</h1>
<h3>Shows how data can be transfered from<br>one location to another on a single page</h3>
<article class="dnd-image-drag cf">
<div class="container">
<h3>Drag the puzzle pieces below</h3>
<div class="inner gallery-list cf">
<img draggable="true" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/ie_big_image_2.jpg" class="drag" data-value="2" />
<img draggable="true" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/ie_big_image_3.jpg" class="drag" data-value="3" />
<img draggable="true" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/ie_big_image_1.jpg" class="drag" data-value="1" />
<img draggable="true" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/ie_big_image_4.jpg" class="drag" data-value="4" />
</div>
</div>
<div class="container">
<h3>Drop puzzle pieces here</h3>
<div class="inner gallery-painting cf">
<div class="drop" data-value="1"></div>
<div class="drop" data-value="2"></div>
<div class="drop" data-value="3"></div>
<div class="drop" data-value="4"></div>
</div>
</div>
<div class="message-container"></div>
<button class="reset-button">Reset Puzzle</button>
<h4 class="ie-message">You appear to be on Internet Explorer. IE doesn't support the 'text/html' type (only the 'text' and 'URL' types) so jQuery will be used to copy the dragged item instead of the API (instead of copying the HTML)</h4>
<p class="p">Demo by Simon Codrington. <a href="http://www.sitepoint.com/html5-native-drag-and-drop-api" target="_blank">See article</a>.</p>
</article>
</article>
</article>
html {
font-size: 10px;
}
body {
font-family: Arial, sans-serif;
font-size: 1.5rem;
line-height: 1.5rem;
padding: 10px 20px;
}
h1,
h2,
h3,
h4,
h5,
h6 {
font-weight: 600;
line-height: 1.4;
margin: 1.5rem 0;
}
h1 {
font-size: 2.8rem;
margin: .85rem 0rem;
text-align: center;
}
h2 {
font-size: 2.8rem;
margin: 2.1rem 0rem;
}
h3 {
font-size: 2.1rem;
margin: 1.57rem 0rem;
text-align: center;
}
h4 {
font-size: 1.8rem;
}
h5 {
font-size: 1.3rem;
}
h6 {
font-size: 1.0rem;
}
p {
margin: 0rem 0rem 2.4rem 0rem;
line-height: 150%;
text-align: center;
}
ul,
ol {
margin-left: 25px;
margin-bottom: 15px;
line-height: 150%;
}
/*clearfixes*/
.cf:before,
.cf:after {
content: " ";
display: table;
}
.cf:after {
clear: both;
}
*,
*:before,
*:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section,
div {
margin: 0;
}
div {
margin: 0rem;
}
.page {
max-width: 690px;
margin: 0px auto;
}
/*drag and drop styles*/
.dnd-image-drag .container {
float: left;
margin: 0 10px 20px 10px;
}
.dnd-image-drag .gallery-list {
width: 320px;
min-height: 317px;
background: #eee;
border: solid 1px #ccc;
padding: 5px 0 5px 5px;
}
.dnd-image-drag .gallery-list .drag {
width: 150px;
display: inline-block;
height: 150px;
border: solid 1px #ccc;
cursor: -webkit-grab;
cursor: -moz-grab;
border: solid 1px transparent;
}
.dnd-image-drag .gallery-list .drag.drag-active {
border: solid 1px #2c3e50;
}
.dnd-image-drag .gallery-painting {
width: 320px;
background: #eee;
background-position: center center;
background-size: cover;
border: solid 1px #ccc;
padding: 5px 0 5px 5px;
}
.dnd-image-drag .gallery-painting .drop {
position: relative;
height: 150px;
width: 150px;
display: inline-block;
background: rgba(50, 50, 50, 0.7);
border: solid 1px transparent;
overflow: hidden;
}
.dnd-image-drag .gallery-painting .drop.drop-active {
border: solid 1px #f1c40f;
}
.dnd-image-drag .gallery-painting .drop.correct {
border: solid 1px #32ce74;
}
.dnd-image-drag .gallery-painting .drop.incorrect {
border: solid 1px #c0392b;
}
.dnd-image-drag .gallery-painting .drop img {
max-width: 100%;
height: auto;
}
.reset-button {
background: #eee;
border: solid 1px #ccc;
padding: 12px 15px;
display: block;
cursor: pointer;
margin: 0 auto;
min-width: 100px;
clear: both;
}
.message-container {
clear: both;
}
.ie-message {
display: none;
padding: 15px;
background: #e74c3c;
color: #fff;
border: dashed 2px #c0392b;
margin-bottom: 15px;
}
.p {
font-size: 15px;
padding-top: 150px;
clear: both;
}
jQuery(document).ready(function($) {
var drag_items = $('.dnd-image-drag .drag');
var drop_items = $('.dnd-image-drag').find('.drop');
//set up drag and drop event listeners
function setUpEventListeners() {
drag_items.each(function() {
var thisDrag = $(this);
thisDrag[0].addEventListener('dragstart', dragStart);
thisDrag[0].addEventListener('drag', drag);
thisDrag[0].addEventListener('dragend', dragEnd);
});
drop_items.each(function() {
var thisDrop = $(this);
thisDrop[0].addEventListener('dragenter', dragEnter);
thisDrop[0].addEventListener('dragover', dragOver);
thisDrop[0].addEventListener('dragleave', dragLeave);
thisDrop[0].addEventListener('drop', drop);
});
}
setUpEventListeners();
var dragItem;
//called as soon as the draggable starts being dragged
//used to set up data and options
function dragStart(event) {
drag = event.target;
dragItem = event.target;
//set the effectAllowed for the drag item
event.dataTransfer.effectAllowed = 'copy';
var imageSrc = $(dragItem).prop('src');
var imageHTML = $(dragItem).prop('outerHTML');
//check for IE (it supports only 'text' or 'URL')
try {
event.dataTransfer.setData('text/uri-list', imageSrc);
event.dataTransfer.setData('text/html', imageHTML);
} catch (e) {
event.dataTransfer.setData('text', imageSrc);
}
$(drag).addClass('drag-active');
}
//called as the draggable enters a droppable
//needs to return false to make droppable area valid
function dragEnter(event) {
var drop = this;
//set the drop effect for this zone
event.dataTransfer.dropEffect = 'copy';
$(drop).addClass('drop-active');
event.preventDefault();
event.stopPropagation();
}
//called continually while the draggable is over a droppable
//needs to return false to make droppable area valid
function dragOver(event) {
var drop = this;
//set the drop effect for this zone
event.dataTransfer.dropEffect = 'copy';
$(drop).addClass('drop-active');
event.preventDefault();
event.stopPropagation();
}
//called when the draggable was inside a droppable but then left
function dragLeave(event) {
var drop = this;
$(drop).removeClass('drop-active');
}
//called continually as the draggable is dragged
function drag(event) {
}
//called when the draggable has been released (either on droppable or not)
//may be called on invalid or valid drop
function dragEnd(event) {
var drag = this;
$(drag).removeClass('drag-active');
}
//called when draggable is dropped on droppable
//final process, used to copy data or update UI on successful drop
function drop(event) {
drop = this;
$(drop).removeClass('drop-active');
var dataList, dataHTML, dataText;
//collect our data (based on what browser support we have)
try {
dataList = event.dataTransfer.getData('text/uri-list');
dataHTML = event.dataTransfer.getData('text/html');
} catch (e) {;
dataText = event.dataTransfer.getData('text');
}
//we have access to the HTML
if (dataHTML) {
$(drop).empty();
$(drop).prepend(dataHTML);
var drag = $(drop).find('.drag');
}
//only have access to text (old browsers + IE)
else {
$(drop).empty();
$(drop).prepend($(dragItem).clone());
var drag = $(drop).find('.drag');
}
//check if this element is in the right spot
checkCorrectDrop(drop, drag);
//see if the final image is complete
checkCorrectFinalImage();
event.preventDefault();
event.stopPropagation();
}
//check to see if this dropped item is in the correct spot
function checkCorrectDrop(drop, drag) {
//check if this drop is correct
var imageValue = $(drag).attr('data-value');
var dropValue = $(drop).attr('data-value');
if (imageValue == dropValue) {
$(drop).removeClass('incorrect').addClass('correct');
//make the dropped item no longer draggable (removing the attr)
$(drag).attr('draggable', 'false');
//hide the original drag item (set during dragStart), we don't need it anymore
$(dragItem).hide();
} else {
$(drop).removeClass('correct').addClass('incorrect');
}
}
//checks to see if the dropped images are in the correct locations
function checkCorrectFinalImage() {
var correctItems = drop_items.filter('.correct');
if (correctItems.length == drop_items.length) {
$('.message-container').empty();
$('.message-container').append('<h3>You solved the puzzle!</h3>');
$('.message-container').append('<p>Thanks for putting Internet Explorer back together again!</p>');
} else {
$('.message-container').empty();
}
}
//Reset the drop containers
$('.reset-button').on('click', function() {
$('.dnd-image-drag').find('.drop').children('img').remove();
$('.dnd-image-drag').find('.drop').removeClass('correct incorrect');
$('.message-container').empty();
$('.dnd-image-drag .drag').show();
});
// check for ie
var userAgent = window.navigator.userAgent;
if (userAgent.indexOf('MSIE') != -1) {
$('.ie-message').show();
}
});
Also see: Tab Triggers