| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
function reducedMotion() { |
| 5 |
return window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches; |
| 6 |
} |
| 7 |
|
| 8 |
function reveal(units) { |
| 9 |
// CSS stagger is driven by --bkst-i custom property; just add class |
| 10 |
units.forEach(function (unit) { |
| 11 |
unit.classList.add('bkst-revealed'); |
| 12 |
}); |
| 13 |
} |
| 14 |
|
| 15 |
function hide(units) { |
| 16 |
units.forEach(function (unit) { |
| 17 |
unit.classList.remove('bkst-revealed'); |
| 18 |
}); |
| 19 |
} |
| 20 |
|
| 21 |
function getUnits(section) { |
| 22 |
var split = section.dataset.split || 'words'; |
| 23 |
var sel = split === 'chars' ? '.bkst-char' : split === 'lines' ? '.bkst-line' : '.bkst-word'; |
| 24 |
return Array.prototype.slice.call(section.querySelectorAll(sel)); |
| 25 |
} |
| 26 |
|
| 27 |
function initSection(section) { |
| 28 |
var threshold = parseFloat(section.dataset.threshold) || 0.15; |
| 29 |
var repeat = section.dataset.repeat === 'true'; |
| 30 |
|
| 31 |
if (reducedMotion()) { |
| 32 |
// Show all immediately |
| 33 |
var allUnits = Array.prototype.slice.call(section.querySelectorAll('.bkst-word,.bkst-char,.bkst-line')); |
| 34 |
reveal(allUnits); |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
var units = getUnits(section); |
| 39 |
var stagger = parseFloat(section.dataset.stagger) || 80; |
| 40 |
|
| 41 |
// Set CSS custom property --bkst-i on each unit for stagger timing |
| 42 |
units.forEach(function (unit, i) { |
| 43 |
unit.style.setProperty('--bkst-i', i); |
| 44 |
}); |
| 45 |
|
| 46 |
var observer = new IntersectionObserver(function (entries) { |
| 47 |
entries.forEach(function (entry) { |
| 48 |
if (entry.isIntersecting) { |
| 49 |
reveal(units); |
| 50 |
if (!repeat) observer.unobserve(section); |
| 51 |
} else if (repeat) { |
| 52 |
hide(units); |
| 53 |
} |
| 54 |
}); |
| 55 |
}, { threshold: threshold }); |
| 56 |
|
| 57 |
observer.observe(section); |
| 58 |
} |
| 59 |
|
| 60 |
function init() { |
| 61 |
document.querySelectorAll('.bkst-section[data-animation]').forEach(initSection); |
| 62 |
} |
| 63 |
|
| 64 |
if (typeof IntersectionObserver === 'undefined') { |
| 65 |
// Fallback for very old browsers |
| 66 |
document.querySelectorAll('.bkst-word,.bkst-char,.bkst-line').forEach(function (el) { |
| 67 |
el.classList.add('bkst-revealed'); |
| 68 |
}); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if (document.readyState === 'loading') { |
| 73 |
document.addEventListener('DOMContentLoaded', init); |
| 74 |
} else { |
| 75 |
init(); |
| 76 |
} |
| 77 |
|
| 78 |
}()); |
| 79 |
|