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.
<!-- SCROLL CONTAINER -->
<div class="smooth-scroll">
<div class="section-wrapper" data-scroll-section>
<section id="section1">
<div id="box1" data-scroll data-scroll-speed="2" data-scroll-position="top"></div>
<div id="box2" data-scroll data-scroll-speed="5" data-scroll-position="top"></div>
<div id="box3" data-scroll data-scroll-speed="4" data-scroll-position="top"></div>
<div id="overlay" data-scroll data-scroll-speed="2" data-scroll-position="top"></div>
</section>
<section id="section2">
<div id="box4" data-scroll data-scroll-speed="2"></div>
<div id="box5" class="parallax" data-speed=".5"></div>
<div id="box6" data-scroll data-scroll-speed="4"></div>
</section>
<section id="sectionPin">
<div class="pin-wrap">
<!-- <div class="animation-wrap to-right" id="animateThis-0"> -->
<div class="pin-box">pin box 1</div>
<div class="pin-box">pin box 2</div>
<div class="pin-box">pin box 3</div>
<!-- </div> -->
</div>
</section>
<section id="section3">
<div id="box7" class="parallax" data-speed=".2"></div>
<div id="box8" data-scroll data-scroll-speed="1">
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur eget laoreet magna, eu ornare lorem. Integer id commodo nunc, at maximus neque. Aliquam eu orci ac mi ornare maximus. Vestibulum ultricies sollicitudin erat, sed interdum enim eleifend sed. Mauris consequat sapien nibh, et convallis lacus eleifend ac. Curabitur ipsum nunc, placerat et mattis ut, molestie non mauris. Ut ultricies est a justo fringilla, vel ultrices mi fringilla. Maecenas vel ultricies nisl, vel feugiat est. Etiam sit amet magna id ante mollis commodo quis a ipsum.</p>
<p>Sed congue nisi dolor, quis dapibus lacus pharetra nec. Proin metus odio, dapibus sit amet erat ac, semper rutrum ipsum. Sed in lacinia eros, non ultrices mi. Cras tempus ultricies malesuada. Quisque diam mi, ornare ac odio sit amet, fermentum sagittis nibh. Donec lectus orci, suscipit a lorem nec, mollis sagittis metus. Suspendisse vel tristique lorem. Aenean enim leo, consectetur in tristique quis, tincidunt sed ex.</p>
<p>Integer et tellus at enim faucibus facilisis id ut est. Nulla facilisi. Sed orci orci, interdum at sagittis at, vehicula vel purus. Vivamus ac ante tempus, laoreet nibh at, tempus massa. Fusce sit amet dui purus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut imperdiet velit eu libero tempor molestie.</p>
<p>Pellentesque consectetur, ligula et mollis posuere, diam ipsum maximus nisi, et fermentum elit erat id arcu. Donec non lobortis velit, eget accumsan dolor. Ut tristique dapibus mi. Donec suscipit, risus eu euismod mollis, ligula neque ultrices metus, ac fringilla est ligula sed lectus. Vivamus viverra a ante id tincidunt. Nam luctus lacus vel leo accumsan egestas. Etiam at pretium leo. Maecenas eu ultrices magna, sed pellentesque sapien. Phasellus ut purus eu massa hendrerit bibendum. Donec a eleifend urna. Curabitur pellentesque felis in mauris posuere, eget semper leo blandit. Donec maximus posuere commodo. Cras dictum erat erat. Pellentesque vulputate diam consequat nulla ultricies, in vehicula elit rhoncus. Donec ullamcorper, mauris at tristique elementum, erat lectus volutpat enim, non varius diam sem vitae erat. Duis at neque quis neque iaculis imperdiet nec quis leo.</p>
</div>
</section>
<section id="section4">
<div id="box9" class="parallax" data-speed=".3"></div>
</section>
</div><!-- section wrapper -->
</div> <!--smooth scroll-->
body {
background: red;
margin: 0;
}
#overlay {
mix-blend-mode: multiply;
background-color: #1D3140;
width: 45%;
height: 100%;
position: absolute;
right: 0;
top: 0;
}
#section1 {
background: url("https://images.unsplash.com/photo-1557878499-95663e82c9d1?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1052&q=80");
background-position: center center;
background-size: cover;
background-repeat: no-repeat;
height: 100vh;
display: flex;
justify-content: space-around;
align-items: center;
position: relative;
}
#section2 {
background: brown;
height: 100vh;
display: flex;
justify-content: space-around;
align-items: center;
}
#sectionPin {
height: 100vh;
width: 100vw;
overflow: hidden;
}
.pin-wrap {
background: pink;
height: 100vh;
display: flex;
justify-content: flex-start;
align-items: center;
}
.pin-box {
height: 100%;
width: 100vw;
background: green;
border: 5px solid #fff;
}
#section3 {
background: orange;
padding: 5%;
display: flex;
justify-content: space-around;
}
#section4 {
background: grey;
padding: 10%;
height: 100vh;
display: flex;
justify-content: center;
}
#box1 {
height: 100px;
width: 100px;
background: red;
}
#box2 {
height: 150px;
width: 150px;
background: blue;
}
#box3 {
height: 250px;
width: 250px;
background: green;
z-index: 1;
}
#box4 {
height: 200px;
width: 200px;
background: orange;
}
#box5 {
height: 200px;
width: 200px;
background: orange;
}
#box6 {
height: 200px;
width: 200px;
background: orange;
}
#box7 {
height: 200px;
width: 400px;
background: grey;
}
#box8 {
width: 50%;
}
#box9 {
width: 50%;
height: 300px;
background: purple;
}
gsap.registerPlugin(ScrollTrigger);
// Using Locomotive Scroll
const locoScroll = new LocomotiveScroll({
el: document.querySelector(".smooth-scroll"),
smooth: true
});
// each time Locomotive Scroll updates, tell ScrollTrigger to update too (sync positioning)
locoScroll.on("scroll", ScrollTrigger.update);
// tell ScrollTrigger to use these proxy methods for the ".smooth-scroll" element since Locomotive Scroll is hijacking things
ScrollTrigger.scrollerProxy(".smooth-scroll", {
scrollTop(value) {
return arguments.length ? locoScroll.scrollTo(value, 0, 0) : locoScroll.scroll.instance.scroll.y;
}, // we don't have to define a scrollLeft because we're only scrolling vertically.
getBoundingClientRect() {
return {top: 0, left: 0, width: window.innerWidth, height: window.innerHeight};
}
});
// GSAP PARALLAX - INDIVIDUAL ELEMENTS (while sharing class)
// get sections
var parallaxElements = Array.prototype.slice.call(document.querySelectorAll("section"));
var self = this;
// get box or parallax element inside each section
parallaxElements.forEach(function(self) {
var boxTop = self.querySelectorAll(".parallaxTop");
var box = self.querySelectorAll(".parallax");
// animate boxes at top of page (section already in viewport)
gsap.to(boxTop, {
scrollTrigger: {
scroller: ".smooth-scroll",
scrub: true,
trigger: self,
start: "top 0%",
end: "bottom 0%",
},
y: (i, target) => -innerHeight * target.dataset.speed,
ease: "none"
});
// animate boxes when the relevant section comes into viewport
gsap.to(box, {
scrollTrigger: {
scroller: ".smooth-scroll",
scrub: true,
trigger: self,
start: "top 100%",
end: "bottom 0%",
},
y: (i, target) => -innerHeight * target.dataset.speed,
ease: "none"
});
});
////////////////////////////////////
// get pinned boxes and box width
var pinBoxes = $('.pin-box');
var pinBoxWidth = pinBoxes.width();
// screen width 100vw equivalent
var windowWidth = $(window).innerWidth();
// get pin box wrpper and calc width based on window width X number of boxes
var pinWrap = $('.pin-wrap');
var pinWrapWidth = windowWidth * pinBoxes.length;
var horizontalScrollLength = (pinBoxes.length - 1) * windowWidth;
// give pin wrap a set width
$('.pin-wrap').width(pinWrapWidth);
console.log('pin box width', pinBoxWidth);
console.log('pin wrap total width', pinWrapWidth);
console.log('horizontal scroll', horizontalScrollLength);
// Pinning and horizontal scrolling
gsap.to(".pin-wrap", {
scrollTrigger: {
scroller: ".smooth-scroll",
scrub: true,
trigger: "#sectionPin",
pin: "#sectionPin",
// anticipatePin: 1,
start: "top top",
end: pinWrapWidth
},
x: -horizontalScrollLength,
ease: "none"
});
////////////////////////////////////
//other animations
gsap.to("#box5", {
scrollTrigger: {
scroller: ".smooth-scroll",
scrub: true,
trigger: "#box5",
start: "top bottom",
end: "bottom top"
},
x: 500,
ease: "none"
});
//timeline test
var tl = gsap.timeline({
scrollTrigger: {
trigger: "#section3",
scroller: ".smooth-scroll",
// scrub: true,
start: "top 50%",
// end: "+=100%",
// markers: true,
// id: "text panel"
}
});
tl.from("#box7", {scale: 0.3, x: "-100%", autoAlpha: 0})
.from("#box8", {autoAlpha: 0, ease: "power2"})
// .to("#box7", {scale: 1.2, ease: "bounce"})
//toggle test
gsap.from("#box9", {
scrollTrigger: {
trigger: "#section4",
scroller: ".smooth-scroll",
toggleActions: "restart none none none",
start: "top top",
id: "bottom panel"
// end: "top top"
},
rotation: 360,
ease: "none"
});
// each time the window updates, we should refresh ScrollTrigger and then update LocomotiveScroll.
ScrollTrigger.addEventListener("refresh", () => locoScroll.update());
// after everything is set up, refresh() ScrollTrigger and update LocomotiveScroll because padding may have been added for pinning, etc.
ScrollTrigger.refresh();
Also see: Tab Triggers