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.
<main>
<section id="top-bar" drop-zone="main" data-sorted="true">
<div id="clone-container" class="clone-container">
<div id="scroll-box">
<div id="tile-container" class="letter-container">
<div class="tile-wrapper">
<div class="tile">A</div>
</div>
<div class="tile-wrapper">
<div class="tile">B</div>
</div>
<div class="tile-wrapper">
<div class="tile">C</div>
</div>
<div class="tile-wrapper">
<div class="tile">D</div>
</div>
<div class="tile-wrapper">
<div class="tile">E</div>
</div>
<div class="tile-wrapper">
<div class="tile">F</div>
</div>
<div class="tile-wrapper">
<div class="tile">G</div>
</div>
<div class="tile-wrapper">
<div class="tile">H</div>
</div>
<div class="tile-wrapper">
<div class="tile">I</div>
</div>
<div class="tile-wrapper">
<div class="tile">J</div>
</div>
<div class="tile-wrapper">
<div class="tile">K</div>
</div>
<div class="tile-wrapper">
<div class="tile">L</div>
</div>
<div class="tile-wrapper">
<div class="tile">M</div>
</div>
<div class="tile-wrapper">
<div class="tile">N</div>
</div>
<div class="tile-wrapper">
<div class="tile">O</div>
</div>
<div class="tile-wrapper">
<div class="tile">P</div>
</div>
<div class="tile-wrapper">
<div class="tile">Q</div>
</div>
<div class="tile-wrapper">
<div class="tile">R</div>
</div>
<div class="tile-wrapper">
<div class="tile">S</div>
</div>
<div class="tile-wrapper">
<div class="tile">T</div>
</div>
<div class="tile-wrapper">
<div class="tile">U</div>
</div>
<div class="tile-wrapper">
<div class="tile">V</div>
</div>
<div class="tile-wrapper">
<div class="tile">W</div>
</div>
<div class="tile-wrapper">
<div class="tile">X</div>
</div>
<div class="tile-wrapper">
<div class="tile">Y</div>
</div>
<div class="tile-wrapper">
<div class="tile">Z</div>
</div>
</div>
</div>
</div>
</section>
<section class="content">
<div id="center-panel" drop-zone="center">
<div class="clone-container">
<div class="letter-container"></div>
</div>
</div>
</section>
<section id="bottom-panel" drop-zone="bottom" data-sorted="true">
<div class="clone-container">
<div class="letter-container"></div>
</div>
</section>
</main>
body {
background-color: #eee;
height: 100vh;
margin: 0;
position: relative;
overflow: hidden;
}
* {
box-sizing: border-box;
}
main {
height: 100%;
display: flex;
flex-direction: column;
user-select: none;
}
#top-bar {
margin: 10px;
padding: 10px;
background-color: white;
box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.3);
min-height: 119px;
}
#center-panel {
background: white;
min-height: 180px;
min-width: 180px;
max-width: 420px;
padding: 10px;
box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.3);
}
.content {
flex: 1;
display: flex;
align-items: center;
flex-direction: row;
justify-content: center;
}
#bottom-panel {
background: white;
padding: 10px;
display: block;
min-height: 180px;
margin: 10px;
box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.3);
}
#scroll-box {
border: 1px solid #ccc;
white-space: nowrap;
overflow-x: scroll;
}
#scroll-box .letter-container {
display: inline-flex;
flex-wrap: nowrap;
}
.letter-container {
display: block;
display: flex;
flex-wrap: wrap;
}
.tile-wrapper {
width: 80px;
height: 80px;
align-items: center;
display: flex;
justify-content: center;
position: relative;
}
.tile {
background-color: mediumvioletred;
color: #fafafa;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
position: relative;
width: 60px;
height: 60px;
cursor: move;
box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.5);
overflow: hidden;
}
.tile[clone] {
position: absolute;
visibility: hidden;
opacity: 0;
}
.tile.bottom {
background: cornflowerblue;
}
.tile.center {
background-color: seagreen;
}
/*
///////////////////////////////////////////////////////////////////////////////////
// DRAGGING AN ELEMENT OUTSIDE A SCROLLABLE CONTAINER
///////////////////////////////////////////////////////////////////////////////////
HOW IT WORKS...
The app starts off by creating clones for each tile using the scope listed below.
When the user clicks on a tile, startDraggable is called, which enables the
draggable instance to be dragged. When this happens, the real tile is hidden,
with the clone taking its place. Because the clone is not actually inside the
overflow container, it can be dragged anywhere. When the dragging stops, the clone
goes back to a hidden state.
SCOPE
- element: the tile element located in the #scroll-box
- wrapper: the element's parent, used to animate the space collapsing around a tile
- clone: a clone of the element that gets appended to the #clone-container
- dropped: is true when the tile is appended to the #drop-panel
- moved: is true when the tile has been dragged outside of its wrapper
- draggable: the draggable instance used by the clone
- x,y: getters that return the start position of the element
- width: the width of the wrapper
START DRAGGABLE
- moves the clone to the tile's position
- toggles the visibility between the element and clone
- starts the draggable instance by passing in the pointer event to its startDrag method
ON DRAG
- checks if the clone is outside of the wrapper using hitTest
- if true, it animates the space collapsing where the tile used to be
ON RELEASE
- checks if the clone is inside the drop panel using hitTest
- if it's inside and not already dropped, the wrapper is appended to the panel
MOVE BACK
- animates the wrapper space expanding
- animates the clone moving back to its starting position
- toggles the visibility between the clone and tile
///////////////////////////////////////////////////////////////////////////////////
// CHANGES
///////////////////////////////////////////////////////////////////////////////////
ATTRIBUTES
Added attributes to elements to create drop zones, which are added to the
dropZones obejct.
<div drop-zone="name" sorted="true>
<div class="clone-container"">
<div class="letter-container"></div>
</div>
</div>
SCOPE
All the letter scopes are added to the letters array. The getPrevious function
uses the array to return a filtered list of all the letters that are in the same
zone and have a lower index. The array is reverse order, so the first letter
returned in the filterd list will be the previous element. If no results are
returned, then it's either the first element or the zone is empty. If sorted
is set to false, the getPrevious just returns values to do a normal append to
the letter container.
///////////////////////////////////////////////////////////////////////////////////
*/
var threshold = "60%";
var dropZones = {};
var letters = [];
$("[drop-zone]").each(function() {
var zone = $(this);
var name = zone.attr("drop-zone");
dropZones[name] = {
element : zone,
name : name,
sorted : zone.data("sorted"),
clones : zone.find(".clone-container"),
letters : zone.find(".letter-container")
};
});
$(".tile").each(function(index) {
var element = $(this);
var wrapper = element.parent();
var offset = element.position();
var zone = dropZones.main;
var scope = {
clone : element.clone().attr("clone", "").prependTo(zone.clones),
element : element,
wrapper : wrapper,
width : wrapper.outerWidth(),
height : wrapper.outerHeight(),
moved : false,
index : index,
zone : zone,
get x() { return getPosition(wrapper, this.zone.clones, offset).x; },
get y() { return getPosition(wrapper, this.zone.clones, offset).y; },
get cloneX() { return getPosition(this.clone, this.zone.clones).x; },
get cloneY() { return getPosition(this.clone, this.zone.clones).y; },
get previous() { return getPrevious(this, this.zone); }
};
scope.draggable = createDraggable(scope);
// reversed order
letters.unshift(scope);
element.on("mousedown touchstart", scope, startDraggable);
});
// START DRAGGABLE :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function startDraggable(event) {
var letter = event.data;
// Maak element onzichtbaar
// Maak kloon zichtbaar en verplaats deze naar de coordinaten van het element
TweenLite.set(letter.element, { autoAlpha: 0 });
TweenLite.set(letter.clone, { x: letter.x, y: letter.y, autoAlpha: 1 });
letter.draggable.startDrag(event.originalEvent);
}
// GET PREVIOUS :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function getPrevious(letter, zone) {
if (!zone.sorted) {
return { target: zone.letters, insert: "appendTo"};
}
var values = letters.filter(function(value) {
return value.zone === letter.zone && value.index < letter.index;
});
return {
target: values[0] ? values[0].wrapper : zone.letters,
insert: values[0] ? "insertAfter" : "prependTo"
};
}
// CREATE DRAGGABLE :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function createDraggable(letter) {
var clone = letter.clone;
var wrapper = letter.wrapper;
letter.draggable = new Draggable(clone, {
onPress : setActive,
onDrag : collapseSpace,
onRelease : dropTile
});
return letter.draggable;
///////
function setActive() {
TweenLite.to(clone, 0.15, { scale: 1.2, autoAlpha: 0.75 });
}
function collapseSpace() {
if (!letter.moved) {
if (!this.hitTest(wrapper)) {
letter.moved = true;
TweenLite.to(wrapper, 0.3, { width: 0, height: 0 });
}
}
}
function dropTile() {
var name = undefined;
var self = this;
$.each(dropZones, function(key, zone) {
if (self.hitTest(zone.element, threshold) && letter.zone !== zone) {
name = "tile " + zone.name;
letter.zone = zone;
// Get the previous element and the insert method
var previous = letter.previous;
wrapper[previous.insert](previous.target);
// Position the clone inside its new container
TweenLite.set(clone, { x: letter.cloneX, y: letter.cloneY });
zone.clones.prepend(clone);
}
});
moveBack(letter, name);
}
}
// MOVE BACK ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function moveBack(letter, className) {
var clone = letter.clone;
var element = letter.element;
var wrapper = letter.wrapper;
TweenLite.to(wrapper, 0.2, { width: letter.width, height: letter.height });
TweenLite.to(clone, 0.3, { scale: 1, autoAlpha: 1, x: letter.x, y: letter.y, onComplete: done, delay: 0.02 });
if (className) TweenLite.to([element, clone], 0.3, { className: className });
function done() {
letter.moved = false;
TweenLite.set(clone, { autoAlpha: 0 });
TweenLite.set(element, { autoAlpha: 1 });
}
}
// GET POSITION :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function getPosition(target1, target2, offset) {
var position1 = target1.offset();
var position2 = target2.offset();
offset = offset || { left: 0, top: 0 };
return {
x: position1.left - position2.left + offset.left,
y: position1.top - position2.top + offset.top
};
}
Also see: Tab Triggers