Fly Away
The element crouches for a moment, then tilts and flies off the top, drifting the way it leans as it picks up speed. A looping demo leans the other way each time.
Boarding pass sentFlight 274 · Gate 12
How to use it
- Paste the snippet just before
</head>. - Add
data-fly-awayto any element.
Options
Add any of these attributes to the same element.
data-tilt | How far it leans as it flies, in degrees. Negative leans left. Default 14. |
data-distance | Fly this many px, fading out, instead of leaving the screen. |
data-duration | How long the flight takes. Default 900 ms. |
data-delay | How long it stays in view before it flies. Default 1200 ms. |
data-loop | Fade back in, hold and fly away again, leaning the other way. |
data-hold | How long it stays between exits, when looping. Default 1400 ms. |
It leaves past the edge of the nearest container that clips its overflow (overflow: hidden or clip), or of the window. Put it in a clipping wrapper, or give the page overflow-x: clip, so it can’t make the page scroll sideways on its way out. It ends hidden (visibility: hidden) but still taking up its space. To play it when you choose, leave the attribute off and call animacious.flyAway(el).
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Fly Away: the element dips for a moment, then tilts and flies off the
// top, drifting the way it leans as it picks up speed.
// Usage: <div data-fly-away>…</div>
// It plays once the element has been in view for data-delay ms. It flies
// past the top of the nearest container that clips its overflow
// (overflow: hidden or clip), or of the window, and ends hidden
// (visibility: hidden) but still taking up its space.
// Optional: data-tilt (how far it leans, in degrees; negative leans left,
// default 14; a looping demo leans the other way each time),
// data-distance (px to fly, fading out, instead of leaving the screen),
// data-duration (ms, default 900), data-delay (ms in view before it
// leaves, default 1200), data-loop (fade back in, hold and fly away
// again), data-hold (ms to stay between exits when looping, default 1400).
// Script use: animacious.flyAway(el, done) plays it on any element now,
// using its data-* options, and calls done afterwards.
var DEFAULTS = { tilt: 14, duration: 900, delay: 1200, hold: 1400 };
function reduced() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
// The top edge the element must fly past to be "off screen".
function clipTop(el) {
for (var p = el.parentElement; p && p !== document.body; p = p.parentElement) {
var s = getComputedStyle(p);
if (s.overflowX !== 'visible' || s.overflowY !== 'visible') return p.getBoundingClientRect().top;
}
return 0;
}
function leave(el, dir, done) {
var d = el.dataset;
var t = +d.duration || DEFAULTS.duration;
var tilt = (d.tilt === undefined ? DEFAULTS.tilt : +d.tilt || 0) * dir;
if (reduced()) {
anime.utils.set(el, { opacity: 0 });
if (done) done();
return;
}
anime.utils.set(el, { x: 0, y: 0, rotate: 0, scale: 1 });
var r = el.getBoundingClientRect();
// Clear the edge even with the lean dropping one corner.
var rise = +d.distance || r.bottom - clipTop(el) + r.width * Math.abs(Math.sin(tilt * Math.PI / 180)) + 8;
var flyProps = {
y: { to: -rise, ease: 'in(3)' },
x: { to: rise * Math.tan(tilt * Math.PI / 180) * 0.6, ease: 'in(3)' },
rotate: { to: tilt, duration: t * 0.5, ease: 'out(2)' },
scale: { to: 0.9, ease: 'out(2)' },
duration: t
};
if (d.distance) flyProps.opacity = { to: 0, delay: t * 0.3, duration: t * 0.7, ease: 'in(2)' };
anime.createTimeline({
onComplete: function () {
if (done) done();
}
})
// A small crouch, leaning back, before take-off.
.add(el, { y: 8, rotate: -tilt * 0.3, scale: 0.97, duration: 260, ease: 'out(2)' })
.add(el, flyProps);
}
function cycle(el) {
var d = el.dataset;
var s = el._fyw;
var dir = s.n++ % 2 ? -1 : 1;
leave(el, dir, function () {
if (d.loop === undefined || d.loop === 'false') {
el.style.visibility = 'hidden';
return;
}
// Come back quietly in place, then go again once it's on screen.
setTimeout(function () {
anime.utils.set(el, { x: 0, y: 0, rotate: 0, scale: 1, opacity: 0 });
anime.animate(el, {
opacity: 1,
duration: 400,
ease: 'out(2)',
onComplete: function () {
setTimeout(function () {
if (s.visible) cycle(el);
else s.waiting = true;
}, +d.hold || DEFAULTS.hold);
}
});
}, 400);
});
}
window.animacious = window.animacious || {};
window.animacious.flyAway = function (el, done) {
leave(el, 1, function () {
el.style.visibility = 'hidden';
if (done) done(el);
});
};
function init() {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
var s = e.target._fyw;
s.visible = e.isIntersecting;
if (!e.isIntersecting) return;
if (!s.started) {
s.started = true;
var wait = e.target.dataset.delay;
setTimeout(function () { cycle(e.target); }, wait === undefined ? DEFAULTS.delay : +wait || 0);
} else if (s.waiting) {
s.waiting = false;
cycle(e.target);
}
});
}, { threshold: 0.2 });
document.querySelectorAll('[data-fly-away]').forEach(function (el) {
if (el._fyw) return;
el._fyw = { n: 0 };
if (!reduced()) io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><div class="card" data-fly-away>Boarding pass sent</div>
<!-- or leaning left, harder: -->
<div class="card" data-fly-away data-tilt="-24">Boarding pass sent</div>