Split Open In
The element opens from a line down its centre outwards, like a pair of curtains drawn apart, while its contents settle back to size behind them.
Now showingDoors open at 7:30
How to use it
- Paste the snippet just before
</head>. - Add
data-split-open-into any element.
Options
Add any of these attributes to the same element.
data-axis | x opens from a vertical centre line out to the sides, y from a horizontal one up and down. Give both, x y, and each loop uses the next. Default x. |
data-duration | How long it takes to open. 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. |
It uses clip-path on the element while it opens, and removes it afterwards.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Split Open In: the element opens from its centre line outwards, like a
// pair of curtains drawn apart, while its contents ease back to size.
// Usage: <div data-split-open-in>…</div>
// Optional: data-axis ("x", default, a vertical centre line that opens
// to the sides; "y" a horizontal one that opens up and down; give both,
// "x y", and each loop uses the next), 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: 'x', duration: 900, delay: 0, hold: 1400 };
var PAD = 40; // room past the edges, so a box-shadow isn't cut off
function reduced() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
function inset(v, h) {
return 'inset(' + v + 'px ' + h + 'px ' + v + 'px ' + h + 'px)';
}
function enter(el, axis, done) {
var d = el.dataset;
var t = +d.duration || DEFAULTS.duration;
// Clip to a line at the centre, then widen the clip to the full box.
var start = axis === 'y'
? inset(el.offsetHeight / 2, -PAD)
: inset(-PAD, el.offsetWidth / 2);
// (from/to given together: the browser shortens a computed inset() to fewer values.)
el.style.clipPath = start;
anime.utils.set(el, { scale: 1.06, opacity: 1 });
anime.animate(el, {
clipPath: { from: start, to: inset(-PAD, -PAD), duration: t, ease: 'inOut(3)' },
scale: { to: 1, duration: t * 1.1, ease: 'out(3)' },
onComplete: function () {
el.style.clipPath = '';
done();
}
});
}
function cycle(el) {
var d = el.dataset;
var s = el._spo;
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._spo;
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-split-open-in]').forEach(function (el) {
if (el._spo) return;
el._spo = { 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-split-open-in>Now showing</div>
<!-- or opening up and down: -->
<div class="card" data-split-open-in data-axis="y">Now showing</div>