<section id="flip" data-state="initial">
<button id="prev">‹</button>
<div class="window">
<div class="boxPosition initialPosition"></div>
<div class="box">Flip</div>
<div class="finalContainer">new container<div class="boxPosition finalPosition"></div></div>
</div>
<div class="steps">
<p>If we reparent the black <div> into a new rotated & scaled container (altering the DOM structure), how could we animate there seamlessly? Use the "FLIP" animation technique! Press the right arrow to begin.</p>
<p><strong>"First":</strong> Get the initial state.<br><code>const state = <a href="https://greensock.com/docs/v3/Plugins/Flip/static.getState()">Flip.getState(targets)</a></code></p>
<p><strong>"Last":</strong> Put elements in their final state. In this case we'll place the element in the new container <div> which results in a totally different position, scale, and rotation.</p>
<p><strong>"Invert":</strong> Make the element <i>appear</i> in its previous position/size by applying offsets instantly. That way, it <i>looks</i> like it hasn't moved yet. <i>Note: we animate this step in the demo to illustrate what normally happens immediately.</i></p>
<p><strong>"Play":</strong> Animate the removal of those offsets.<br>
<code><a href="https://greensock.com/docs/v3/Plugins/Flip/static.from()">Flip.from(state, options)</a></code> returns a GSAP timeline so you can add other animations, control it, etc.
</p>
<p>GSAP's <a href="https://greensock.com/docs/v3/Plugins/Flip">Flip Plugin</a> makes this a breeze! </p>
</div>
<button id="next">›</button>
</section>
<a href="https://greensock.com/"><img src="https://assets.codepen.io/16327/hero-logo.svg" class="logo" /></a>
* {
box-sizing: border-box;
}
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-weight: 300;
font-size: 1.15em;
font-family: "Signika Negative", sans-serif, Arial;
}
body {
background-image: linear-gradient(to bottom right, #444, #222);
color: #ccc;
}
strong {
color: white;
font-size: 1.15em;
}
.window {
position: relative;
height: 70vh;
width: 40vw;
border-top: 2rem solid #C4C7C8;
border-radius: .5rem;
background-color: #fff;
box-shadow: 0 0 5vw 0 rgba(black, 0.2);
overflow: hidden;
}
body, section {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
#flip {
width: 100%;
}
section > .steps, section > .window {
flex-basis: 50%;
}
button {
appearance: none;
background: transparent;
border: none;
color: #ccc;
font-size: 30vmin;
font-weight: 300;
cursor: pointer;
}
button:focus {outline:0;}
a {
color: #a7ff00;
}
.steps {
padding: 0 2rem;
font-size: 1.1em;
}
.steps p:not(:first-child) {
display: none;
}
.box, .boxPosition {
position: absolute;
top: 8px;
left: 8px;
height: 10vmin;
width: 10vmin;
box-sizing: content-box;
}
.boxPosition {
top: 6px;
left: 6px;
visibility: hidden;
opacity: 0;
}
.box {
background-color: black;
text-align: center;
line-height: 10vmin;
font-size: 3.5vmin;
color: #88ce02;
font-weight: 400;
border-radius: 1vmin;
}
.initialPosition {
border: 2px dashed green;
}
.finalPosition {
border: 2px dashed red;
}
.finalContainer {
width: 36vmin;
height: 36vmin;
position: absolute;
bottom: 0;
right: 0;
background-color: #bfaecd;
transform: translate(15vmin, -12vmin) scale(1.5) rotate(-30deg);
color: white;
padding: 1vmin;
font-size: 3vmin;
overflow: visible;
}
.logo {
position: absolute;
width: 60px;
bottom: 20px;
right: 30px;
}
@media (max-width: 700px) {
body {
font-size: 0.9em;
}
}
View Compiled
gsap.registerPlugin(Flip);
let states = [ toInitState, toFirstState, toLastState, toInvertState, toPlayState, toEndState ],
stateIndex = 0,
stateIndexWrap = gsap.utils.wrap(0, states.length),
stepBlurbs = gsap.utils.toArray('.steps p'),
boxEl = document.querySelector(".box"),
windowEl = document.querySelector(".window"),
finalContainerEl = document.querySelector(".finalContainer");
gsap.defaults({ duration: 0.4, overwrite: 'auto' });
document.querySelector('#prev').addEventListener('click', () => goToState(stateIndex - 1));
document.querySelector('#next').addEventListener('click', () => goToState(stateIndex + 1));
let transition;
function resize() {
transition && transition.kill();
// reset (put back in original container and remove any inline styles)
windowEl.appendChild(boxEl);
boxEl.style.cssText = "";
// grab the original state
const state = Flip.getState(boxEl);
// put into the new container
finalContainerEl.appendChild(boxEl);
// FLIP!
transition = Flip.from(state, {
scale: true,
duration: 1,
repeat: -1,
repeatDelay: 1.5,
ease: "power1.inOut",
paused: true
}).progress(1).progress(0); // lock in the starting/ending values.
if (stateIndex === 2 || stateIndex === 3) {
transition.progress(1);
} else if (stateIndex === 4 || stateIndex === 5) {
transition.play();
}
}
function goToState(newState) {
stateIndex = stateIndexWrap(newState);
states[stateIndex]();
}
function updateBlurb() {
const textState = Flip.getState(stepBlurbs);
gsap.set(stepBlurbs, { display: 'none' });
gsap.set(stepBlurbs[stateIndex], { display: 'block' });
Flip.from(textState, {
duration: 0.4,
absolute: true,
onLeave: elements => gsap.to(elements, {opacity: 0, duration: 0.3}),
onEnter: elements => gsap.fromTo(elements, {opacity: 0}, {opacity: 1, duration: 0.3})
});
}
function toInitState() {
transition.pause(0);
gsap.to(boxEl, {opacity: 1});
gsap.to(".boxPosition", {autoAlpha: 0});
updateBlurb();
}
function toFirstState() {
transition.pause(0);
gsap.to(boxEl, {opacity: 0.5});
gsap.to(".initialPosition", {autoAlpha: 1});
gsap.to(".finalPosition", {autoAlpha: 0});
updateBlurb();
}
function toLastState() {
gsap.killTweensOf(transition);
gsap.set(boxEl, {opacity: 1});
transition.progress(1).pause();
gsap.to(".finalPosition", {autoAlpha: 1});
updateBlurb();
}
function toInvertState() {
transition.tweenFromTo(">", 0, {duration: 1, overwrite: true});
updateBlurb();
}
function toPlayState() {
gsap.killTweensOf(transition);
gsap.to('.boxPosition', { autoAlpha: 1 });
transition.play();
updateBlurb();
}
function toEndState() {
gsap.to(boxEl, { opacity: 1 });
gsap.to('.boxPosition', { autoAlpha: 1 });
transition.play();
updateBlurb();
}
function init() {
window.addEventListener('resize', resize);
resize();
}
init();
// document.addEventListener("click", e => {
// if (e.target.tagName.toLowerCase() === "button") {
// return;
// }
// if (e.pageX < window.innerWidth / 4) {
// goToState(stateIndex - 1);
// } else {
// goToState(stateIndex + 1);
// }
// });
This Pen doesn't use any external CSS resources.