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.
<div class="description">
<div>
<h1>Horizontal "<code>containerAnimation</code>"</h1>
<p>Scroll this page vertically and you'll see a horizontal fake-scrolling section where a container is animated FIRST on the Y-axis and THAN on the X-axis using a ScrollTrigger animation. With <code>containerAnimation</code> you can trigger animations when certain elements <i>inside</i> that container enter the viewport horizontally! It's like a ScrollTrigger inside of a ScrollTrigger. 🤯
</p>
</div>
</div>
<div class="container">
<div class="panel blue">
<h2 class="typing_text-heading">
<span class="typing_text"></span><span class="cursor">_</span>
</h2>
</div>
<section class="panel red">
<div>
<pre class="code-block prettyprint lang-js linenums">gsap.to(".box-1", {
y: -130,
duration: 2,
ease: "elastic",
scrollTrigger: {
trigger: ".box-1",
containerAnimation: scrollTween,
start: "left center",
toggleActions: "play none none reset"
}
});</pre>
<h2 class="typing_text-heading">
<span class="typing_text"></span><span class="cursor">_</span>
</h2>
</div>
<div class="box-1 box">box-1</div>
</section>
<section class="panel gray">
<div>
<pre class="code-block prettyprint lang-js linenums">gsap.to(".box-2", {
y: -120,
backgroundColor: "#1e90ff",
ease: "none",
scrollTrigger: {
trigger: ".box-2",
containerAnimation: scrollTween,
start: "center 80%",
end: "center 20%",
scrub: true
}
});</pre>
...or scrub it back & forth with the scrollbar
</div>
<div class="box-2 box">box-2</div>
</section>
<section class="panel purple">
<div>
<pre class="code-block prettyprint lang-js linenums">ScrollTrigger.create({
trigger: ".box-3",
containerAnimation: scrollTween,
toggleClass: "active",
start: "center 60%"
});</pre>
Toggle a CSS class
</div>
<div class="box-3 box">box-3</div>
</section>
<section class="panel green">
<div>
<pre class="code-block prettyprint lang-js linenums">ScrollTrigger.create({
trigger: ".green",
containerAnimation: scrollTween,
start: "center 65%",
end: "center 51%",
onEnter: () => console.log("enter"),
onLeave: () => console.log("leave"),
onEnterBack: () => console.log("enterBack"),
onLeaveBack: () => console.log("leaveBack"),
onToggle: self => console.log("active", self.isActive)
});</pre>
Use the rich callback system
</div>
</section>
</div>
<div class="final">
<div>
<h1>Wasn't that fun?</h1>
<p>Here are a few caveats to keep in mind:</p>
<ul>
<li>The fake-scrolling animation (just the part that's moving the container horizontally) must have no easing (<code>ease: "none"</code>).</li>
<li>Pinning and snapping won't work on ScrollTriggers with a <code>containerAnimation</code>.</li>
<li>The mapping of scroll position trigger points are based on the trigger element itself not being animated horizontally (inside the container). If you need to animate the trigger, you can either wrap it in a <div> and use that as the trigger instead or just factor the trigger's movement into your end position. For example, if you animate it left 100px, make the <code>end</code> 100px further to the left.</li>
<li>Requires ScrollTrigger 3.8.0 or later</li>
</ul>
</div>
</div>
<header>
<a href="https://greensock.com/scrolltrigger">
<img class="greensock-icon" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/scroll-trigger-logo-light.svg" width="200" height="64" />
</a>
</header>
#typing_text {
display: none;
}
body {
background-color: #222;
color: #ddd;
font-size: 18px;
line-height: 1.4;
font-weight: 300;
}
h1, h2 {
color: white;
font-weight: 400;
margin-bottom: 0;
}
.typing_text-heading {
margin: 0;
font-size: 2rem;
font-weight: 400;
}
.section-typing_text {
padding: 3rem;
}
.typing_text-heading {
padding: 2rem;
width: 100%;
max-width: 24rem;
max-height: none !important;
height: auto !important;
}
.container .cursor {
animation: cursorBlink 0.5s alternate infinite;
}
@keyframes cursorBlink {
from {opacity: 0;}
to {opacity: 100%;}
}
.panel pre.prettyprint {
font-size: 20px;
text-align: left;
width: auto;
font-weight: normal;
margin: 10px;
border: none;
}
.prettyprint .linenums {
padding: 0;
list-style: none;
}
.prettyprint ol li {
background-color: black;
}
.panel.red .prettyprint .linenums > li:nth-child(n+7):nth-child(-n+9),
.panel.gray .prettyprint .linenums > li:nth-child(10),
.panel.purple .prettyprint .linenums > li:nth-child(4),
.panel.green .prettyprint .linenums > li:nth-child(n+6):nth-child(-n+10) {
background-color: #222;
}
.box {
width: 100px;
height: 80px;
text-align: center;
line-height: 80px;
background-color: white;
border-radius: 8px;
color: #222;
font-weight: 700;
margin-left: 20px;
will-change: transform;
}
.box.active {
background-color: orange;
border: 2px solid white;
}
.description, .final {
display: flex;
justify-content: center;
align-items: center;
padding: 10px;
min-height: 80vh;
}
.container {
width: 500%;
height: 100%;
display: flex;
flex-wrap: nowrap;
}
.panel {
font-weight: 300;
}
gsap.registerPlugin(ScrollTrigger);
let sections = gsap.utils.toArray(".panel");
let scrollTween = gsap.to(sections, {
xPercent: -100 * (sections.length - 1),
ease: "none", // <-- IMPORTANT!
scrollTrigger: {
trigger: ".container",
pin: true,
scrub: 0.1,
//snap: directionalSnap(1 / (sections.length - 1)),
end: "+=3000"
}
});
//blue section
gsap.to(".typing_text", {x: 0,
text:"Scroll down to animate horizontally >",
scrollTrigger: {
trigger: ".container",
pin: ".typing_text-heading",
start: "center center",
end: "center top",
scrub: true,
markers: true
}
});
gsap.set(".box-1, .box-2", {y: 100});
ScrollTrigger.defaults({markers: {startColor: "white", endColor: "white"}});
// red section
gsap.to(".box-1 .typing_text", {
y: -130,
duration: 2,
ease: "elastic",
text: "Fire an animation at a particular spot...",
scrollTrigger: {
trigger: ".box-1",
pin: ".typing_text-heading",
containerAnimation: scrollTween,
start: "left center",
toggleActions: "play none none reset",
id: "1",
}
});
// gray section
gsap.to(".box-2", {
y: -120,
backgroundColor: "#1e90ff",
ease: "none",
scrollTrigger: {
trigger: ".box-2",
containerAnimation: scrollTween,
start: "center 80%",
end: "center 20%",
scrub: true,
id: "2"
}
});
// purple section
ScrollTrigger.create({
trigger: ".box-3",
containerAnimation: scrollTween,
toggleClass: "active",
start: "center 60%",
id: "3"
});
// green section
ScrollTrigger.create({
trigger: ".green",
containerAnimation: scrollTween,
start: "center 65%",
end: "center 51%",
onEnter: () => console.log("enter"),
onLeave: () => console.log("leave"),
onEnterBack: () => console.log("enterBack"),
onLeaveBack: () => console.log("leaveBack"),
onToggle: self => console.log("active", self.isActive),
id: "4"
});
// only show the relevant section's markers at any given time
gsap.set(".gsap-marker-start, .gsap-marker-end, .gsap-marker-scroller-start, .gsap-marker-scroller-end", {autoAlpha: 0});
["red","gray","purple","green"].forEach((triggerClass, i) => {
ScrollTrigger.create({
trigger: "." + triggerClass,
containerAnimation: scrollTween,
start: "left 30%",
end: i === 3 ? "right right" : "right 30%",
markers: false,
onToggle: self => gsap.to(".marker-" + (i+1), {duration: 0.25, autoAlpha: self.isActive ? 1 : 0})
});
});
// helper function for causing the sections to always snap in the direction of the scroll (next section) rather than whichever section is "closest" when scrolling stops.
// function directionalSnap(increment) {
// let snapFunc = gsap.utils.snap(increment);
// return (raw, self) => {
// let n = snapFunc(raw);
// return Math.abs(n - raw) < 1e-4 || (n < raw) === self.direction < 0 ? n : self.direction < 0 ? n - increment : n + increment;
// };
// }
// making the code pretty/formatted.
PR.prettyPrint();
Also see: Tab Triggers