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 id="root"></div>
video,
img {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
object-fit: cover;
will-change: transform;
}
.slides {
width: 100%;
height: 100vh;
overflow: hidden;
position: relative;
}
.slide {
position: absolute;
top: 0;
width: 100%;
height: 100vh;
clip-path: polygon(0 100%, 100% 100%, 100% 100%, 0 100%);
&:first-child {
clip-path: polygon(0% 100%, 100% 100%, 100% 0%, 0% 0%);
}
a {
position: relative;
display: grid;
height: 100vh;
text-decoration: none;
overflow: hidden;
}
}
.outer,
.inner {
width: 100%;
height: 100%;
overflow-y: hidden;
}
.container {
display: grid;
position: relative;
height: 100vh;
background-color: orange;
}
.slides,
.nav,
.bg {
grid-area: 1 / 1;
}
.bg {
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
will-change: transform;
perspective: 1px;
}
.nav {
z-index: 1;
display: flex;
flex-direction: column;
align-self: center;
justify-self: flex-start;
list-style-type: none;
> li {
&:first-child {
padding-left: 24px;
@media (min-width: 799px) {
padding-left: 64px;
}
}
button {
display: inline-flex;
align-items: center;
cursor: pointer;
}
svg {
width: 35px;
margin-right: 12px;
color: #fff;
display: none;
}
h2 {
color: blue;
text-align: start;
}
&.is-active {
svg {
display: block;
}
}
}
}
.line {
width: 100%;
height: 10px;
position: absolute;
top: 0;
z-index: 1;
display: inline-block;
background-color: chocolate;
}
.menu {
border: 1px solid red;
position: absolute;
top: 0;
left: 0;
width: 100vw;
z-index: 1000;
ul {
display: flex;
gap: 2em;
}
}
const { useEffect, useState, useRef } = React;
gsap.registerPlugin(ScrollToPlugin, ScrollTrigger);
const sliderArr = [
{
id: 1,
href: "/",
src:
"https://images.unsplash.com/photo-1526409049865-5b0b188372f1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1632&q=50",
navTitle: "Slide 1"
},
{
id: 2,
href: "/",
src:
"https://images.unsplash.com/photo-1425913397330-cf8af2ff40a1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1074&q=50",
navTitle: "Slide 2"
},
{
id: 3,
href: "/",
src:
"https://images.unsplash.com/photo-1551986782-d0169b3f8fa7?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=688&q=50",
navTitle: "Slide 3"
},
{
id: 4,
href: "/",
src:
"https://images.unsplash.com/photo-1608478497783-158b45d36042?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=50",
navTitle: "Slide 4"
}
];
function App() {
const containerRef = useRef(null);
const slidesWrapperRef = useRef(null);
const slides = useRef([]);
const navItems = useRef([]);
const bg = useRef([]);
const lineRef = useRef([]);
const slidesTl = useRef();
const duration = useRef(1);
const [ctx] = useState(gsap.context(() => {}, containerRef));
const [activeNavItem, setActiveNavItem] = useState(0);
useEffect(() => {
ctx.add(() => {
// const slides = gsap.utils.toArray(".slide");
let breakPoint = 800;
let mm = gsap.matchMedia();
slidesTl.current = gsap.timeline({
id: "hero-animation",
defaults: {
ease: "none",
duration: 1
},
scrollTrigger: {
trigger: containerRef.current,
pin: true, // locked the slidesWrapperRef div with position fixed
start: "top top", // init slides at the top of the viewport
end: `+=${sliderArr.length * 100}%`, // intead of using "bottom top" move end marker to the end of 3rd slide to slow down scroll animation
scrub: true,
invalidateOnRefresh: true, // to make it responsive
markers: true
}
});
// each slide
slides.current.forEach((slide, index) => {
// exclude the first slide
// since we want to start animation from the second slide (index 1)
if (index > 0) {
if (!slidesTl.current) return;
const position = duration.current / 2; // add active class at the center of the viewport
console.log(bg.current[index - 1]);
mm.add("(min-width: 799px)", () => {
// this setup code only runs when viewport is at least 800px wide
slidesTl.current
.to(navItems.current[index - 1], { paddingLeft: 0 }, "<")
.to(navItems.current[index], { paddingLeft: 50 }, "<");
});
mm.add("(max-width: 800px)", () => {
// this setup code only runs when viewport is at least 800px wide
slidesTl.current
.to(navItems.current[index - 1], { paddingLeft: 0 })
.to(navItems.current[index], { paddingLeft: 100 });
});
slidesTl.current
.to(slide, {
clipPath: "polygon(0% 100%, 100% 100%, 100% 0%, 0% 0%)"
})
.fromTo(
bg.current[index - 1].firstElementChild,
{
y: 0
},
{
y: () => -slide.offsetHeight * 0.3
},
"<"
)
.fromTo(
bg.current[index].firstElementChild,
{
y: () => slide.offsetHeight * 0.3
},
{
y: () => 0
},
"<"
)
.call(
() => {
const st = slidesTl.current?.scrollTrigger;
//console.log(slidesTl.current?.scrollTrigger.progress);
if (st && st.direction !== undefined) {
const _d = st.direction;
setActiveNavItem(index + (_d > 0 ? 0 : _d));
}
},
undefined,
`-=${String(position)}`
)
.fromTo(
lineRef.current[index],
{
value: 0
},
{
value: 100,
ease: "none"
},
"<"
);
}
// ScrollTrigger.create({
// trigger: slide,
// start: "top center",
// end: "bottom center",
// markers: true
// });
});
});
return () => {
ctx.revert();
};
}, []);
const slideToSection = (index) => {
const navItemsCount = navItems.current.length - 1;
const st = slidesTl.current?.scrollTrigger;
if (st && st.start !== undefined && st.end !== undefined) {
// calculate the y-coordinate based on the scrollTrigger
const y = st.start + (st.end - st.start) * ((1 / navItemsCount) * index);
console.log({ navItemsCount, index });
gsap.to(window, {
scrollTo: { y },
duration: duration.current
});
}
};
return (
<>
<header className="menu">
<div className="inner-menu">
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
</div>
</header>
<section className="container" ref={containerRef}>
<div className="slides" ref={slidesWrapperRef}>
{sliderArr.map((slide, index) => {
return (
<div
className={`slide`}
id={`slide-${index}`}
aria-hidden={index === activeNavItem ? false : true}
ref={(element) => {
// check if the element exists and if it's not already present
if (element && !slides.current.includes(element)) {
slides.current.push(element);
}
}}
>
<div className="outer">
<div className="inner">
<progress
className="line"
max="100"
value="0"
ref={(element) => {
if (element && !lineRef.current.includes(element)) {
lineRef.current.push(element);
}
}}
></progress>
<a href={slide.href}>
<div
className="bg"
ref={(element) => {
if (element && !bg.current.includes(element)) {
bg.current.push(element);
}
}}
>
<img src={slide.src} alt="" />
</div>
</a>
</div>
</div>
</div>
);
})}
</div>
<ul className="nav">
{sliderArr.map(({ navTitle, id }, idx) => {
return (
<li
key={id}
ref={(element) => {
if (element && !navItems.current.includes(element)) {
navItems.current.push(element);
}
}}
className={idx === activeNavItem ? "is-active" : ""}
>
<button
onClick={() => slideToSection(idx)}
data-href={`#slide-${idx}`}
type="button"
>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path d="M165.013 288.946h75.034c6.953 0 12.609 5.656 12.609 12.608v26.424c0 7.065 3.659 9.585 7.082 9.585 2.106 0 4.451-.936 6.78-2.702l90.964-69.014c3.416-2.589 5.297-6.087 5.297-9.844 0-3.762-1.881-7.259-5.297-9.849l-90.964-69.014c-2.329-1.766-4.674-2.702-6.78-2.702-3.424 0-7.082 2.519-7.082 9.584v26.425c0 6.952-5.656 12.608-12.609 12.608h-75.034c-8.707 0-15.79 7.085-15.79 15.788v34.313c0 8.706 7.082 15.79 15.79 15.79z" />
<path d="M256 0C114.842 0 .002 114.84.002 256S114.842 512 256 512s255.998-114.84 255.998-256S397.158 0 256 0zm0 66.785c104.334 0 189.216 84.879 189.216 189.215S360.334 445.215 256 445.215 66.783 360.336 66.783 256 151.667 66.785 256 66.785z" />
</svg>
<h2>{navTitle}</h2>
</button>
</li>
);
})}
</ul>
</section>
<section className="container">
<div className="inner">
<h1>HELLO!!!</h1>
</div>
</section>
<section className="container">
<div className="inner">
<h1>HELLO!!! AGAIN</h1>
</div>
</section>
</>
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);
// console.clear();
// gsap.registerPlugin(ScrollTrigger);
// const slides = gsap.utils.toArray(".slide");
// const slidesTl = gsap.timeline({
// defaults: {
// ease: "none"
// },
// scrollTrigger: {
// trigger: ".container",
// pin: true,
// start: "top top",
// end: "bottom top",
// scrub: true,
// markers: true
// }
// });
// slides.forEach((slide, i) => {
// console.log("fired", slide, i);
// slidesTl.to(slide, {
// clipPath: "polygon(0% 100%, 100% 100%, 100% 0%, 0% 0%)"
// });
// });
Also see: Tab Triggers