Flash
The element blinks off and on twice, like a light catching your eye. The blinks are quick fades rather than a hard cut, so it stays easy on the eyes.
LiveStreaming now
How to use it
- Paste the snippet just before
</head>. - Add
data-flashto any element. It flashes once when it scrolls into view. - Add
data-loopto flash again every so often.
Options
Add any of these attributes to the same element.
data-times | How many blinks. Default 2. |
data-min | How faint it gets at the bottom of a blink, from 0 (gone) to 1. Default 0. |
data-duration | How long all the blinks take. Default 900 ms. |
data-delay | Wait before the first flash. Default 0 ms. |
data-loop | Flash again every so often while it’s on screen. |
data-every | Rest between flashes, when looping. Default 1800 ms. |
data-hover | Also play it when the pointer moves over the element or it’s tapped. |
Call animacious.flash(el) to flash it from your own code. Keep flashing to a few blinks: fast, endless flashing can be uncomfortable, and the script leaves it still for visitors who prefer reduced motion.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Flash: the element blinks off and on, twice by default, like a light
// catching your eye. Quick fades rather than a hard cut, so it stays easy
// on the eyes.
// Usage: <span data-flash>…</span>
// Optional: data-times (how many blinks, default 2), data-min (opacity at
// the bottom of a blink, 0 to 1, default 0), data-duration (ms for all the
// blinks, default 900), data-delay (ms before the first flash), data-loop
// (flash again every so often while in view), data-every (ms of rest
// between loops, default 1800), data-hover (also flash on hover or tap).
// Script use: animacious.flash(el) flashes it from code.
var DEFAULTS = { times: 2, min: 0, duration: 900, delay: 0, every: 1800 };
function on(v) { return v !== undefined && v !== 'false'; }
function reduced() { return window.matchMedia('(prefers-reduced-motion: reduce)').matches; }
function play(el, done) {
var s = el._fls;
if (s.busy) { if (done) s.after = done; return; }
if (reduced()) { if (done) done(); return; }
s.busy = true;
var d = el.dataset;
function finish() {
s.busy = false;
var after = s.after;
s.after = null;
if (done) done();
if (after) after();
}
var times = Math.max(1, Math.round(+d.times || DEFAULTS.times));
var min = d.min === undefined ? DEFAULTS.min : Math.min(1, Math.max(0, +d.min));
var frames = { '0%': { opacity: 1 } };
for (var i = 0; i < times; i++) {
// Each blink: fade down fast, hold a moment, come back up.
var start = (i / times) * 100, span = 100 / times;
frames[(start + span * 0.3).toFixed(2) + '%'] = { opacity: min, ease: 'in(2)' };
frames[(start + span * 0.5).toFixed(2) + '%'] = { opacity: min, ease: 'linear' };
frames[(start + span * 0.8).toFixed(2) + '%'] = { opacity: 1, ease: 'out(2)' };
}
frames['100%'] = { opacity: 1 };
anime.animate(el, {
keyframes: frames,
duration: +d.duration || DEFAULTS.duration,
onComplete: finish
});
}
function cycle(el) {
var s = el._fls;
play(el, function () {
if (!on(el.dataset.loop)) return;
clearTimeout(s.timer);
s.timer = setTimeout(function () {
if (s.visible) cycle(el);
else s.waiting = true;
}, +el.dataset.every || DEFAULTS.every);
});
}
function init() {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
var s = e.target._fls;
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-flash]').forEach(function (el) {
if (el._fls) return;
el._fls = {};
if (on(el.dataset.hover)) {
el.addEventListener('pointerenter', function () { play(el); });
el.addEventListener('pointerdown', function () { play(el); });
}
if (!reduced()) io.observe(el);
});
}
window.animacious = window.animacious || {};
window.animacious.flash = function (el) { if (el._fls) play(el); };
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><span class="badge" data-flash>Live</span>
<!-- or three softer blinks, repeating: -->
<span class="badge" data-flash data-times="3" data-min="0.2" data-loop>Live</span>