Tilt In
The element tips upright from a slanted 3D angle, gliding in from a corner as it straightens up to face you. Pick the corner, or give several and a looping demo takes each in turn.
Boarding pass readyGate 22 · Seat 14A
How to use it
- Paste the snippet just before
</head>. - Add
data-tilt-into any element. - Choose a corner with
data-fromif it shouldn’t come from the top right.
Options
Add any of these attributes to the same element.
data-from | The corner it comes from: top-left, top-right, bottom-left or bottom-right. Give several and each loop uses the next. Default top-right. |
data-angle | How far it’s tipped at the start, in degrees. Default 35. |
data-distance | How far it glides as it straightens, in px. 0 to tip in place. Default 60. |
data-perspective | How close the viewer is, in px. Smaller is more dramatic. Default 700. |
data-duration | How long the entrance takes. Default 900 ms. |
data-delay | Wait before the first entrance. Default 0 ms. |
data-loop | Hold, fade away and tilt in again. |
data-hold | How long it stays before fading, when looping. Default 1400 ms. |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Tilt In: the element tips upright from a slanted 3D angle, gliding in
// from a corner as it straightens up to face you.
// Usage: <div data-tilt-in>…</div>
// Optional: data-from (the corner it comes from: "top-left",
// "top-right", default, "bottom-left" or "bottom-right"; give several and
// each loop uses the next), data-angle (how far it's tipped at the start,
// in degrees, default 35), data-distance (px it glides, default 60),
// data-perspective (px, default 700), data-duration (ms, default 900),
// data-delay (ms before the first entrance), data-loop (hold, fade away
// and tilt in again), data-hold (ms to stay when looping, default 1400).
var DEFAULTS = { from: 'top-right', angle: 35, distance: 60, perspective: 700, duration: 900, delay: 0, hold: 1400 };
function reduced() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
function enter(el, from, done) {
var d = el.dataset;
var t = +d.duration || DEFAULTS.duration;
var a = d.angle === undefined ? DEFAULTS.angle : +d.angle || 0;
var dist = d.distance === undefined ? DEFAULTS.distance : +d.distance || 0;
var sx = /left/.test(from) ? -1 : 1;
var sy = /bottom/.test(from) ? 1 : -1;
// Lean the near corner towards you and the far side away.
anime.utils.set(el, {
perspective: (+d.perspective || DEFAULTS.perspective) + 'px',
x: sx * dist,
y: sy * dist,
rotateX: sy * -a,
rotateY: sx * a,
rotate: sx * a * 0.25,
opacity: 0
});
anime.animate(el, {
x: 0,
y: 0,
rotateX: 0,
rotateY: 0,
rotate: 0,
opacity: { to: 1, duration: t * 0.5, ease: 'out(2)' },
duration: t,
ease: 'out(3)',
onComplete: done
});
}
function cycle(el) {
var d = el.dataset;
var s = el._tli;
var corners = (d.from || DEFAULTS.from).split(/[\s,]+/);
enter(el, corners[s.n++ % corners.length], function () {
if (d.loop === undefined || d.loop === 'false') return;
setTimeout(function () {
anime.animate(el, {
opacity: 0,
duration: 400,
ease: 'out(2)',
onComplete: function () {
// Wait until it's back on screen before the next entrance.
setTimeout(function () {
if (s.visible) cycle(el);
else s.waiting = true;
}, 500);
}
});
}, +d.hold || DEFAULTS.hold);
});
}
function init() {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
var s = e.target._tli;
s.visible = e.isIntersecting;
if (!e.isIntersecting) return;
if (!s.started) {
s.started = true;
setTimeout(function () { cycle(e.target); }, +e.target.dataset.delay || DEFAULTS.delay);
} else if (s.waiting) {
s.waiting = false;
cycle(e.target);
}
});
}, { threshold: 0.2 });
document.querySelectorAll('[data-tilt-in]').forEach(function (el) {
if (el._tli) return;
el._tli = { n: 0 };
if (reduced()) return;
anime.utils.set(el, { opacity: 0 });
io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><div class="card" data-tilt-in>Boarding pass ready</div>
<!-- or from the bottom left, tipping in place: -->
<div class="card" data-tilt-in data-from="bottom-left" data-distance="0">Boarding pass ready</div>