Slit In
The element opens out of a thin slit in 3D. It starts far back and edge-on, rushes forward while still a sliver, then swings open to face you.
Door unlockedFront door, just now
How to use it
- Paste the snippet just before
</head>. - Add
data-slit-into any element. - Add
data-axis="x"for a horizontal slit that opens up and down.
Options
Add any of these attributes to the same element.
data-axis | y is a vertical slit that opens sideways, x a horizontal one that opens up and down. Give both, y x, and each loop uses the next. Default y. |
data-depth | How far back it starts, in px. Default 600. |
data-perspective | How close the viewer is, in px. Smaller is more dramatic. Default 800. |
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 open 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 () {
// Slit In: the element opens out of a thin slit in 3D. It starts far
// back and edge-on, rushes forward still almost edge-on, then swings
// open to face you.
// Usage: <div data-slit-in>…</div>
// Optional: data-axis ("y", default, a vertical slit that opens
// sideways; "x" a horizontal slit that opens up and down; give both,
// "y x", and each loop uses the next), data-depth (how far back it
// starts, in px, default 600), data-perspective (px, default 800),
// data-duration (ms, default 900), data-delay (ms before the first
// entrance), data-loop (hold, fade away and open again), data-hold (ms to
// stay when looping, default 1400).
var DEFAULTS = { axis: 'y', depth: 600, perspective: 800, duration: 900, delay: 0, hold: 1400 };
function reduced() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
function enter(el, axis, done) {
var d = el.dataset;
var t = +d.duration || DEFAULTS.duration;
var depth = +d.depth || DEFAULTS.depth;
var prop = axis === 'x' ? 'rotateX' : 'rotateY';
var other = axis === 'x' ? 'rotateY' : 'rotateX';
var start = { opacity: 0, perspective: (+d.perspective || DEFAULTS.perspective) + 'px', z: -depth };
start[prop] = 90;
start[other] = 0;
anime.utils.set(el, start);
var props = {
opacity: { to: 1, duration: t * 0.45, ease: 'linear' },
// Rush forward while still a slit, then swing open the rest of the way.
z: [
{ to: -depth * 0.25, duration: t * 0.5, ease: 'out(2)' },
{ to: 0, duration: t * 0.5, ease: 'out(3)' }
],
onComplete: done
};
props[prop] = [
{ to: 84, duration: t * 0.5, ease: 'out(2)' },
{ to: 0, duration: t * 0.5, ease: 'out(3)' }
];
anime.animate(el, props);
}
function cycle(el) {
var d = el.dataset;
var s = el._slt;
var axes = (d.axis || DEFAULTS.axis).split(/[\s,]+/);
enter(el, axes[s.n++ % axes.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._slt;
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-slit-in]').forEach(function (el) {
if (el._slt) return;
el._slt = { 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-slit-in>Door unlocked</div>
<!-- or a horizontal slit: -->
<div class="card" data-slit-in data-axis="x">Door unlocked</div>