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.
<h1>Advanced staggers</h1>
<div id="container"></div>
<div id="instructions">
<div id="controls">
<div class="control">
<div class="control-label">from:</div>
<div class="control-vertical">
<label for="center"><input type="radio" id="center" name="from" value="center" checked> <code>"center"</code></label>
<label for="end"><input type="radio" id="end" name="from" value="end"> <code>"end"</code></label>
<label for="edges"><input type="radio" id="edges" name="from" value="edges"> <code>"edges"</code></label>
<label for="random"><input type="radio" id="random" name="from" value="random"> <code>"random"</code></label>
<label for="index"><input type="radio" id="index" name="from" value="11"> <code id="fromIndex">11 (index)</code></label>
</div>
</div>
<div class="control">
<div class="control-label">axis:</div>
<div class="control-vertical">
<label for="null"><input type="radio" id="null" name="axis" value="null" checked> <code>null</code> (both)</label>
<label for="x"><input type="radio" id="x" name="axis" value="x"> <code>"x"</code></label>
<label for="y"><input type="radio" id="y" name="axis" value="y"> <code>"y"</code></label>
</div>
</div>
<div class="control">
<div class="control-label">ease:</div>
<div class="control-vertical">
<label for="linear"><input type="radio" id="linear" name="ease" value='"none"' checked> <code>"none"</code></label>
<label for='"easeInOut"'><input type="radio" id="easeInOut" name="ease" value='"power3.inOut"'> <code>"power3.inOut"</code></label>
<label for="easeOut"><input type="radio" id="easeOut" name="ease" value='"power2.in"'> <code>"power2.in"</code></label>
</div>
</div>
</div>
<p>Play with the options above to see how it affects the animation and the code below. Click any box to have the stagger emanate outward from that specific index number, using the <code class="featured">from:[index]</code> syntax.</p>
<div class="code">
<code>gsap.to(".box", {</code><br>
<code> duration: 1,</code><br>
<code> scale: 0.1,</code><br>
<code> y: 40,</code><br>
<code> ease: "power1.inOut,</code><br>
<code class="featured"> stagger: {</code><br>
<code class="featured"> grid: [7,15],</code><br>
<code class="featured"> from: <span id="from">"center"</span>,<br></code>
<code id="axisCode" class="featured" style="display:none;"> axis: <span id="axis">null</span>,<br></code>
<code id="easeCode" class="featured" style="display:none;"> ease: <span id="ease">Linear.easeNone</span>,<br></code>
<code class="featured"> amount: 1.5</code><br>
<code class="featured"> }</code><br>
<code>});</code>
</div>
<h2>How does it work?</h2>
<p>All tweens recognize a <code class="featured">stagger</code> special property which can be an object, a number, or a function as described below:</p>
<h3>Object</h3>
<p>An object makes configuration simple. It may contain any of the following properties:</p>
<ul>
<li><strong>amount</strong> [number]: The total amount of time (in seconds) that gets split up among all the staggers. So if <code>amount</code> is <code>1</code> and there are 100 elements that stagger linearly, there would be 0.01 seconds between each tween's start time. If you prefer to specify a certain amount of time between each tween, use the <code>each</code> property <i>instead</i>. A negative <code>amount</code> will invert the timing effect which can be very handy. So <code>{amount:-1, from:"center"}</code> would cause staggers to go from the outer edges in toward the center.</li>
<li><strong>each</strong> [number]: The amount of time (in seconds) between each animation's start time. So if <code>each</code> is <code>1</code> (regardless of how many elements there are), there would be 1 second between each tween's start time. If you prefer to specify a <strong>total</strong> amount of time to split up among the staggers, use the <code>amount</code> property <i>instead</i>. A negative <code>each</code> will invert the timing effect which can be very handy. So <code>{each:-1, from:"center"}</code> would cause staggers to go from the outer edges in toward the center.</li>
<li><strong>from</strong> [string | integer]: The position in the array from which the stagger will emanate. To begin with a particular element, for example, use the number representing that element's index in the target array. So <code>from:4</code> begins staggering at the 5th element in the array (because arrays use zero-based indexes). The animation for each element will begin based on the element's proximity to the "from" value in the array (the closer it is, the sooner it'll begin). You can also use the following string values: <code>"start"</code>, <code>"center"</code>, <code>"edges"</code>, <code>"random"</code>, or <code>"end"</code> ("random" was added in version 3.1.0). Default: 0.</li>
<li><strong>grid</strong> [array | "auto"]: If the elements are being displayed in a grid visually, indicate how many rows and columns there are (like <code>grid:[9,15]</code>) so that GSAP can calculate proximities accordingly. Or use <code>grid:"auto"</code> to have GSAP automatically calculate the rows and columns using <code>element.getBoundingClientRect()</code> (great for responsive layouts). Grids are assumed to flow from top left to bottom right layout-wise (like text that wraps at the right edge). Or if your elements aren't arranged in a uniform grid, check out the <a href="https://codepen.io/GreenSock/pen/gyWrPO?editors=0010" target="_top">distributeByPosition() helper function</a> we created.</li>
<li><strong>axis</strong> [string]: If you define a <code>grid</code>, staggers are based on each element's total distance to the "from" value on both the x and y axis, but you can focus on just one axis if you prefer (<code>"x"</code> or <code>"y"</code>). Use the demo above to see the effect (it makes more sense when you see it visually).</li>
<li><strong>ease</strong> [Ease]: The ease that distributes the start times of the animation. So <code>"power2"</code> would start out with bigger gaps and then get more tightly clustered toward the end. Default: <code>"none"</code>.</li>
</ul>
<h3>Number</h3>
<p>A value of <code>stagger: 0.1</code> would cause there to be 0.1 second between the start times of each tween. A negative value would do the same but backwards so that the last element begins first. We commonly call this a "simple" stagger.</p>
<h3>Function</h3>
<p>The function is called once for each target/element in the array and should return the total delay from the starting position (not the amount of delay from the previous tween's start time). The function receives the following parameters:</p>
<ol>
<li><strong>index</strong> [integer] - The index value from the list</li>
<li><strong>target</strong> [object] - The target in the list at that index value</li>
<li><strong>list</strong> [array | NodeList] - The targets array (or NodeList)</li>
</ol>
<h2>Repeat / Yoyo</h2>
<p>If you put your <code>repeat</code> in the top level of the vars object of your tween like <code>gsap.to(... {repeat:-1, stagger:{...})</code>, it waits for <strong>all</strong> of the sub-tweens to finish before repeating the <i>WHOLE</i> sequence, but if you prefer to have each sub-tween repeat independently (so that as soon as each one completes, it immediately repeats itself), simply nest the <code>repeat</code> (and <code>yoyo</code> if necessary) <strong>inside</strong> the advanced stagger object, like <code>gsap.to(... {stagger:{repeat:-1, ...}});</code> Think of it almost like the advanced stagger object is a vars object for the sub-tweens.</p>
<h2>Video Explanation</h2>
<span class="fr-video"><iframe width="560" height="315" src="https://www.youtube.com/embed/wDnUtY5KcOc" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></span>
<h2>FAQ</h2>
<p>
<span class="question">Is this only useful for grids?</span>
Absolutely not! You can get some slick effects by leveraging <code>ease</code> to add more organic spacing, or use the <code>from</code> property to have things emanate outward from a specific element index or the <code>"center"</code>. Even just using <code>amount</code> can be useful because it gives you tight control over when all the staggering will finish regardless of how many elements are in the target array.
</p>
<p>
<span class="question">Does <code>grid: "auto"</code> work for responsive layouts after resize?</span>
No, stagger values get calculated immediately when the stagger method is called, so if the layout changes after that, you'd need to handle that logic on your own. For example, you could use a timeline's stagger method so that everything is in one instance, then have a resize listener that rewinds the timeline to its starting values, clears it, and then redoes the stagger. <code>timeline.time(0).clear(); timeline.to(...);</code> Ask in the <a href="https://greensock.com/forums/">forums</a> if you need any help with that.
</p>
<p>
<span class="question">What if my elements aren't in a uniform grid (gaps, different sizes, etc.) - can I stagger them based on position?</span>
Sure, that's the perfect time to use a function-based value. In fact, we've created a <a href="https://codepen.io/GreenSock/pen/gyWrPO?editors=0010" target="_top">distributeByPosition() helper function</a> for this very case that should make it crazy simple for you!.
</p>
<p>
<span class="question">It's not working for me! Why not?</span>
Make sure you're loading <a href="https://greensock.com/?download=GSAP-JS">GSAP 3</a> or higher (you might need to empty your cache). Otherwise feel free to ask about your issue <a href="https://greensock.com/forums/">in the GreenSock forums</a>.
</p>
</div>
/* Typography */
@font-face {
font-display: block;
font-family: Mori;
font-style: normal;
font-weight: 400;
src: url(https://assets.codepen.io/16327/PPMori-Regular.woff) format("woff");
}
@font-face {
font-display: block;
font-family: Mori;
font-style: normal;
font-weight: 600;
src: url(https://assets.codepen.io/16327/PPMori-SemiBold.woff) format("woff");
}
@font-face {
font-display: block;
font-family: Fraktion Mono;
font-style: normal;
font-weight: 400;
src: url(https://assets.codepen.io/16327/PPFraktionMono-Bold.woff)
format("woff");
}
:root {
--color-shockingly-green: #0ae448;
--color-just-black: #0e100f;
--color-surface-white: #fffce1;
--color-pink: #fec5fb;
--color-shockingly-pink: #f100cb;
--color-orangey: #ff8709;
--color-lilac: #9d95ff;
--color-lt-green: #abff84;
--color-blue: #00bae2;
--color-grey: gray;
--color-surface75: #bbbaa6;
--color-surface50: #7c7c6f;
--color-surface25: #42433d;
}
:root {
--dark: var(--color-just-black);
--grey-dark: #42433d;
--light: var(--color-surface-white);
--mid: #7c7c6f;
--grey: #gray;
--gray: #gray;
--green: var(--color-shockingly-green);
--green-dark: #0ae448;
--green-light: var(--color-lt-green);
--blue: var(--color-blue);
--purple: var(--color-lilac);
--red: #cd237f;
--orange: var(--color-orangey);
accent-color: var(--color-shockingly-green);
}
body {
color: var(--color-surface-white);
background-color: var(--dark);
font-family: Mori, sans-serif;
font-weight: 400;
margin: 0;
font-size: var(--step-0);
line-height: 1.45;
}
body {
text-align: center;
font-weight: 300;
font-size: 18px;
padding: 10px 0;
}
h1 {
margin: 0;
font-size: 50px;
text-align: left;
}
h2 {
font-size: 38px;
margin-bottom: 0;
}
h1, h2, h3 {
text-align: center;
color: var(--color-surface-white);
font-weight: 400;
}
h3 {
text-align: left;
margin-bottom: -12px;
}
#header {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
a {
color: var(--color-shockingly-green);
text-decoration: none;
font-weight: 400;
}
a:hover {
text-decoration: underline;
}
#container {
display: flex;
align-items: center;
justify-content: center;
overflow: visible;
}
.box {
background-color: var(--color-shockingly-green);
position: relative;
border-radius: 5px;
}
.box:before {
padding-top:100%;
content:"";
display:block;
}
#instructions {
margin-top: 3rem;
padding:0 16px 16px 16px;
text-align: left;
max-width: 1000px;
display: inline-block;
}
strong {
font-weight: 400;
font-size: 20px;
}
.question {
font-size: 21px;
font-weight: 400;
display: block;
}
#controls {
text-align: center;
display:flex;
flex-wrap: wrap;
justify-content: center;
align-items: start;
}
.control {
display: flex;
align-items: flex-start;
margin-bottom: 10px;
}
.control-label {
margin-top: 10px;
}
.control-vertical {
display: flex;
flex-direction: column;
align-content: flex-start;
text-align: left;
justify-content: flex-start;
padding: 6px 8px;
border-radius: 9px;
background-color: #111;
margin-right: 18px;
}
.fr-video {
display: block;
position: relative;
width: 100%;
height: 0;
float: left;
padding-bottom: 56.25%;
margin: 12px 0 35px 0;
}
.fr-video iframe {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
border: 2px solid #444;
}
#instructions li {
margin-bottom: 10px;
}
@media only screen and (max-width: 640px) {
h1 {
font-size: 30px;
line-height: 30px;
}
#controls {
align-items: flex-start;
justify-content: flex-start;
}
}
code {
background-color: #1d1e22;
border-radius: 10px;
}
.code {
background-color: #1d1e22;
border-radius: 10px;
padding: 1rem;
}
//GSAP 3 introduces advanced stagger values
var grid = [7,15], //[rows, columns]
tl = gsap.timeline({repeat: -1, repeatDelay: 0.5});
function animateBoxes(from, axis, ease) {
//one stagger call does all the animation:
tl.to(".box", {
duration: 1,
scale: 0.1,
y: 60,
yoyo: true,
repeat: 1,
ease: "power1.inOut",
stagger: {
amount: 1.5,
grid: grid,
axis: axis,
ease: ease,
from: from
}
}
);
}
//builds a grid of <div class="box"> elements, dropped into #container (unrelated to animation code)
buildGrid({grid: grid, className: "box", width: 1000, gutter: 15, parent: "#container", onCellClick: onCellClick});
animateBoxes("center");
//---- the rest of the code below just handles all the interactivity ----
var options = document.querySelectorAll('input[name="from"], input[name="axis"], input[name="ease"]'),
_select = function(selector) {
return document.querySelector(selector);
},
axisCodeEl = _select("#axisCode"),
axisEl = _select("#axis"),
easeCodeEl = _select("#easeCode"),
easeEl = _select("#ease"),
fromEl = _select("#from"),
fromIndexEl = _select("#fromIndex"),
indexEl = _select("#index"),
selections = {from: "center", axis: null, ease: "none"},
i;
//add change listeners
for (i = 0; i < options.length; i++) {
options[i].addEventListener("change", onOptionChange);
}
function onOptionChange(e) {
var group = e.target.getAttribute("name"),
value = e.target.getAttribute("value");
if (group === "from") {
updateFrom(value);
} else if (group === "axis") {
selections.axis = (value === "null") ? null : value;
axisCode.style.display = (value === "null") ? "none" : "inline";
axisEl.textContent = '"' + value + '"';
} else if (group === "ease") {
easeEl.textContent = value;
easeCodeEl.style.display = (value === '"none"') ? "none" : "inline";
selections.ease = value.split('"').join("");
}
updateAnimation();
}
function updateFrom(value) {
var current = selections.from,
parsedVal = value,
newIsNumber = !isNaN(value),
oldIsNumber = !isNaN(current);
if (newIsNumber) {
parsedVal = parseInt(value, 10);
} else if (value === "end") {
parsedVal = grid[0] * grid[1] - 1;
newIsNumber = true;
}
if (current !== parsedVal) {
selections.from = parsedVal;
fromEl.textContent = (value === "end") ? '"end"' : newIsNumber ? value : '"' + value + '"';
if (newIsNumber && !oldIsNumber) {
gsap.to(".box", {duration: 0.3, backgroundColor: "#abff84"});
} else if (!newIsNumber && oldIsNumber) {
gsap.to(".box", {duration: 0.3, backgroundColor: "#0ae448"});
}
if (newIsNumber) {
if (value !== "end") {
indexEl.checked = true;
indexEl.setAttribute("value", parsedVal);
fromIndexEl.textContent = parsedVal + " (index)";
}
gsap.fromTo("[data-index='" + parsedVal + "']", {rotation: 0}, {duration: 0.4, rotation: 360, backgroundColor: "#0ae448", ease: "power1.inOut"});
if (oldIsNumber) {
gsap.to("[data-index='" + current + "']", {duration: 0.3, backgroundColor: "#abff84"});
}
}
}
}
function onCellClick(e) {
updateFrom(e.target.index);
updateAnimation();
}
function updateAnimation() {
tl.seek(0).clear();
animateBoxes(selections.from, selections.axis, selections.ease);
}
//helper function to build a grid of <div> elements
function buildGrid(vars) {
vars = vars || {};
var container = document.createElement("div"),
rows = vars.grid[0] || 5,
cols = vars.grid[1] || 5,
width = vars.width || 100,
gutter = vars.gutter || 1,
className = vars.className || "",
w = (width - cols * gutter) / cols,
parent = (typeof(vars.parent) === "string") ? document.querySelector(vars.parent) : vars.parent ? vars.parent : document.body,
css = "display: inline-block; margin: 0 " + (gutter / width * 100) + "% " + (gutter / width * 100) + "% 0; width: " + (w / width * 100) + "%;",
l = rows * cols,
i, box;
for (i = 0; i < l; i++) {
box = document.createElement("div");
box.style.cssText = css;
box.setAttribute("class", className);
box.index = i;
box.setAttribute("data-index", i);
if (vars.onCellClick) {
box.addEventListener("click", vars.onCellClick);
}
container.appendChild(box);
}
container.style.cssText = "width:" + width + "px; line-height: 0; padding:" + gutter + "px 0 0 " + gutter + "px; display:inline-block;";
parent.appendChild(container);
return container;
}
//this just helps avoid the pixel-snapping that some browsers do.
gsap.set(".box", {rotation: 0.5, force3D: true});
Also see: Tab Triggers