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.
-
const CARD_AMOUNT = 50
const PICS = [
'https://images.unsplash.com/photo-1542662565-7e4b66bae529?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=282&q=80',
'https://images.unsplash.com/photo-1525153537143-f88592837fb1?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=332&q=80',
'https://images.unsplash.com/photo-1477346611705-65d1883cee1e?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=750&q=80',
'https://images.unsplash.com/photo-1507400492013-162706c8c05e?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=309&q=80',
'https://images.unsplash.com/photo-1436891620584-47fd0e565afb?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=334&q=80',
'https://images.unsplash.com/photo-1519681393784-d120267933ba?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&auto=format&fit=crop&w=750&q=80'
]
const TITLES = ['Mountain', 'Stars', 'Skies', 'Water', 'Trips']
const TYPE = ['Guide', 'Planner', 'Discuss']
mixin cardContent(idx, reflection)
.card__content.bg-gray-800.overflow-hidden.relative.h-full.w-full.rounded-md.font-sans
.card__details.absolute.inset-0.pt-52.flex.items-center.justify-center.flex-col
h2.card__title.p-2.text-white.font-black.text-5xl.text-center.w-full.pb-12= idx
.card__overlay.z-50.absolute.h-full.w-full.inset-0(class=`${reflection ? 'card__overlay--reflection' : ''}`)
mixin card(idx)
li.gallery__card.card.w-56.h-72.absolute
.card__card.h-full.w-full
+cardContent(idx, false)
.card__reflection.absolute.h-full.w-full.top-8.left-0.rounded-md.overflow-hidden(aria-hidden="true")
+cardContent(idx, true)
.gallery.w-screen.h-screen.absolute
ul.gallery__content.w-56.h-72.transform.absolute.top-50.left-50(class="top-1/2 left-1/2 -translate-y-2/4 -translate-x-2/4")
- let c = 0
while c < CARD_AMOUNT
+card(c)
- c++
.gallery__actions
button.gallery__prev.outline-none.absolute.z-10.rounded-full.transition.left-8.transform-gpu.opacity-50(class="top-1/2 -translate-y-1/2 hover:opacity-100")
span.sr-only Prev
svg.h-12.w-12.fill-current.text-white(viewBox="0 0 256 512" title="Previous")
path(d="M31.7 239l136-136c9.4-9.4 24.6-9.4 33.9 0l22.6 22.6c9.4 9.4 9.4 24.6 0 33.9L127.9 256l96.4 96.4c9.4 9.4 9.4 24.6 0 33.9L201.7 409c-9.4 9.4-24.6 9.4-33.9 0l-136-136c-9.5-9.4-9.5-24.6-.1-34z")
button.gallery__next.outline-none.absolute.z-10.rounded-full.right-8.transform-gpu.transition.opacity-50(class="top-1/2 -translate-y-1/2 hover:opacity-100")
span.sr-only Next
svg.h-12.w-12.fill-current.text-white(viewBox="0 0 256 512" title="Next")
path(d="M224.3 273l-136 136c-9.4 9.4-24.6 9.4-33.9 0l-22.6-22.6c-9.4-9.4-9.4-24.6 0-33.9l96.4-96.4-96.4-96.4c-9.4-9.4-9.4-24.6 0-33.9L54.3 103c9.4-9.4 24.6-9.4 33.9 0l136 136c9.5 9.4 9.5 24.6.1 34z")
*
box-sizing border-box
:root
--bg hsl(220, 34%, 5%)
body
background var(--bg)
min-height 100vh
img
object-fit cover
.card
&:hover
--y -2%
.card__card
transition 0.1s ease
transform translate3d(0, var(--y, 0), 0)
&__overlay
&--reflection
background linear-gradient(var(--bg) 50%, transparent)
clip-path inset(50% 0 0 0)
&__reflection
filter blur(5px)
transition 0.2s ease
transform translate3d(0, 0, 0) rotate(180deg) translate(0, -100%) rotateY(180deg) translate(0, var(--y, 0))
.card__details
background transparent
.card__content
clip-path inset(50% 0 0 0)
import gsap from 'https://cdn.skypack.dev/gsap'
import ScrollTrigger from 'https://cdn.skypack.dev/gsap/ScrollTrigger'
const {
to,
set,
registerPlugin,
utils: { snap, toArray },
} = gsap
registerPlugin(ScrollTrigger)
const NEXT = document.querySelector('.gallery__next')
const PREV = document.querySelector('.gallery__prev')
const AUDIO = {
WHOOSH: new Audio('https://assets.codepen.io/605876/whoosh-two.mp3'),
}
AUDIO.WHOOSH.volume = 0.5
const playWhoosh = () => {
}
const SCRUB_TO = totalTime => {
const PROGRESS = (totalTime - LOOP.duration() * iteration) / LOOP.duration()
if (PROGRESS > 1) {
wrapForward(TRIGGER)
} else if (PROGRESS < 0) {
wrapBackward(TRIGGER)
} else {
TRIGGER.scroll(TRIGGER.start + PROGRESS * (TRIGGER.end - TRIGGER.start))
}
}
const BUILD_LOOP = (CARDS, SPACING) => {
/**
* number of EXTRA animations on either side of the
* start/end to accommodate the seamless looping
*/
const OVERLAP = 10
// The time on the rawSequence at which we'll start the seamless loop
const START = CARDS.length * SPACING + 0.5
// the spot at the end where we loop back to the START
const LOOP_TIME = (CARDS.length + OVERLAP) * SPACING + 1
// this is where all the "real" animations live
const RAW = gsap.timeline({ paused: true })
// this merely scrubs the playhead of the rawSequence so that it appears to seamlessly loop
const LOOP = gsap.timeline({
// paused: true,
repeat: -1, // to accommodate infinite scrolling/looping
onRepeat() {
// works around a super rare edge case bug that's fixed GSAP 3.6.1
// Not sure I need to worry about this then hopefully.
// Although, I'd like to know what the issue is here.
this._time === this._dur && (this._tTime += this._dur - 0.01)
},
})
const L = CARDS.length + OVERLAP * 2
let time = 0
let i
let index
let item
// set initial state of items
// Here's where to use the state map for our positions.
gsap.set(CARDS, { xPercent: 5000, opacity: 1, scale: 1 })
/**
* number of EXTRA animations on either side of the
* start/end to accommodate the seamless looping
*/
for (i = 0; i < L; i++) {
index = i % CARDS.length
item = CARDS[index]
time = i * SPACING
// This the actual animation timeline.
// All cards start off at 0 scale and opacity to the right?
// From what I can tell here, this is calculating all the positions by adding
// a fromTo for every item
// There are two fromTo because the properties have a different duration.
// We want the cards to appear quicker.
RAW
.fromTo(
item,
{ scale: 1 },
{
scale: 1,
// opacity: 1,
zIndex: 100,
duration: 0.5,
yoyo: true,
repeat: 1,
ease: 'none',
immediateRender: false,
onStart: () => {
playWhoosh()
},
},
time
)
.fromTo(
item,
{ xPercent: 400 },
{ xPercent: -400, duration: 1, ease: 'none', immediateRender: false },
time
)
// This is neat for a label jump trick which can be done with .scroll I think.
i <= CARDS.length && LOOP.add('label' + i, time) // we don't really need these, but if you wanted to jump to key spots using labels, here ya go.
}
// here's where we set up the scrubbing of the playhead to make it appear seamless.
RAW.time(START)
LOOP.to(RAW, {
time: LOOP_TIME,
duration: LOOP_TIME - START,
ease: 'none',
}).fromTo(
RAW,
{ time: OVERLAP * SPACING + 1 },
{
time: START,
duration: START - (OVERLAP * SPACING + 1),
immediateRender: false,
ease: 'none',
}
).timeScale(0.1)
return LOOP
}
/**
* gets iterated when we scroll all the way to the end or start and wraps around
* allows us to smoothly continue the playhead scrubbing in the correct direction.
* */
let iteration = 0
const SPACING = 0.1 // Used as a stagger
const CARD_SNAP = snap(SPACING)
const CARDS = toArray('.card')
const LOOP = BUILD_LOOP(CARDS, SPACING)
// Scrub the playhead loop
const SCRUB = to(LOOP, {
totalTime: 0,
duration: 0.5,
ease: 'power3',
paused: true,
})
let TRIGGER
// when the ScrollTrigger reaches the end, loop back to the beginning seamlessly
// Increase the iteration and scroll back to 1
// Keep track of the fact that we're wrapping
const wrapForward = () => {
iteration++
TRIGGER.wrapping = true
TRIGGER.scroll(TRIGGER.start + 1)
}
/**
* to keep the playhead from stopping at the beginning, we jump ahead 10 iterations
*/
const wrapBackward = () => {
iteration--
if (iteration < 0) {
// This makes sense because going lower than 0 will cause a halt
iteration = 9
// Bump the total time in the scrubber
LOOP.totalTime(LOOP.totalTime() + LOOP.duration() * 10)
}
// Kepp track of being wrapped
TRIGGER.wrapping = true
// Scroll to the end with a little breathing room so we don't get trapped
TRIGGER.scroll(TRIGGER.end - 1)
}
TRIGGER = ScrollTrigger.create({
start: 0,
end: '+=3000',
horizontal: false,
pin: '.gallery',
onUpdate: self => {
if (self.progress === 1 && self.direction > 0 && !self.wrapping) {
wrapForward(self)
} else if (self.progress < 1e-5 && self.direction < 0 && !self.wrapping) {
// 1e-5 === 0.00001 Fancy...
wrapBackward(self)
} else {
SCRUB.vars.totalTime = CARD_SNAP(
(iteration + self.progress) * LOOP.duration()
)
SCRUB.invalidate().restart()
self.wrapping = false
}
},
})
// Hook up Next/Prev buttons.
NEXT.addEventListener('click', () => {
SCRUB_TO(SCRUB.vars.totalTime + SPACING)
})
PREV.addEventListener('click', () => {
SCRUB_TO(SCRUB.vars.totalTime - SPACING)
})
Also see: Tab Triggers