Skip to content

Instantly share code, notes, and snippets.

@Huweicai
Created April 16, 2022 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Huweicai/1693786c24fb0f35573d3d4d1227060e to your computer and use it in GitHub Desktop.
Save Huweicai/1693786c24fb0f35573d3d4d1227060e to your computer and use it in GitHub Desktop.
Flip for GSAP
<div class="container final">
<div class="letter F">F</div>
<div class="letter l">l</div>
<div class="letter i">i</div>
<div class="letter p">p</div>
<div class="for">for</div>
<div class="gsap">GSAP</div>
</div>
<a href="https://greensock.com/"><img src="https://assets.codepen.io/16327/hero-logo.svg" class="logo" /></a>
gsap.registerPlugin(Flip);
let layouts = ["final", "plain", "columns", "grid"],
container = document.querySelector(".container"),
curLayout = 0; // index of the current layout
function nextState() {
const state = Flip.getState(".letter, .for, .gsap", {props: "color,backgroundColor", simple: true}); // capture current state
container.classList.remove(layouts[curLayout]); // remove old class
curLayout = (curLayout + 1) % layouts.length; // increment (loop back to the start if at the end)
container.classList.add(layouts[curLayout]); // add the new class
Flip.from(state, { // animate from the previous state
absolute: true,
stagger: 0.07,
duration: 0.7,
ease: "power2.inOut",
spin: curLayout === 0, // only spin when going to the "final" layout
simple: true,
onEnter: (elements, animation) => gsap.fromTo(elements, {opacity: 0}, {opacity: 1, delay: animation.duration() - 0.1}),
onLeave: elements => gsap.to(elements, {opacity: 0})
});
gsap.delayedCall(curLayout === 0 ? 3.5 : 1.5, nextState);
}
gsap.delayedCall(1, nextState);
<script src="https://assets.codepen.io/16327/gsap-latest-beta.min.js"></script>
<script src="https://assets.codepen.io/16327/Flip.min.js"></script>
* {
box-sizing: border-box;
}
body {
background: black;
padding: 0;
margin: 0;
font-family: "Signika Negative", sans-serif, Arial;
font-weight: 300;
height: 100vh;
overflow: hidden;
}
.container {
display: flex;
height: 100%;
width: 100%;
justify-content: center;
align-items: center;
overflow: hidden;
}
.container.grid, .container.columns {
align-content: stretch;
align-items: stretch;
flex-wrap: wrap;
}
.letter {
text-align: center;
color: black;
font-size: 10vmax;
font-weight: 400;
display: flex;
align-items: center;
justify-content: center;
padding: 2px 6px;
}
.container.grid .letter {
flex-basis: 50%;
}
.container.columns .letter {
flex-basis: 25%;
}
.for, .gsap {
font-size: 5vmax;
color: white;
}
.for {
padding: 2px 1.6vmax;
font-weight: 300;
display: none;
}
.gsap {
padding: 2px 0;
font-weight: 600;
display: none;
}
.container.final .for, .container.final .gsap {
display: block;
}
.F {
background: rgba(0, 188, 212, 0.7);
}
.l {
background: rgba(40, 150, 255, 0.7);
}
.i {
background: rgba(153, 80, 220, 0.7);
}
.p {
background: rgba(90, 108, 225, 0.7);
}
.container.plain .letter {
background: transparent;
color: white;
padding: 0;
}
.logo {
position: fixed;
width: 60px;
bottom: 20px;
right: 30px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment