interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / public / js / turing-look.js

74 lines · 2938 bytes Raw Blame History
1/*
2 * The Turing look, motion half: the same two effects /turing/ runs.
3 * 1. A blue-to-green reading bar that fills as the page scrolls.
4 * 2. Sections and cards glide up and fade in once as they come into view,
5 * with cards in a row arriving one after another.
6 * Without this script (or with reduced motion asked for) every element is
7 * simply shown: the hiding class is only added here.
8 */
9(function () {
10 var root = document.documentElement;
11 var calm = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
12
13 function start() {
14 var bar = document.createElement("div");
15 bar.className = "tl-progress";
16 bar.setAttribute("aria-hidden", "true");
17 document.body.appendChild(bar);
18 function onScroll() {
19 var room = root.scrollHeight - root.clientHeight;
20 bar.style.width = (room > 0 ? (root.scrollTop / room) * 100 : 0).toFixed(2) + "%";
21 }
22 window.addEventListener("scroll", onScroll, { passive: true });
23 window.addEventListener("resize", onScroll);
24 onScroll();
25
26 if (calm || !("IntersectionObserver" in window)) return;
27
28 // Blocks fade in whole; cards inside a block arrive one after another.
29 var cards = [
30 ".hub-card", ".narr-card", ".rs-card", ".blog-card", ".home-q", ".entry",
31 ".card", ".research-wrap .artifact", ".receipt", ".pdf-callout",
32 ".hub-callout", ".theory-fig", ".loop-stage"
33 ].join(",");
34 var blocks = [
35 ".section-head", ".hub-section", ".narr-section", ".rs-section", ".loop",
36 ".home-questions", ".research-wrap .section", ".aien-footer-row"
37 ].join(",");
38 var fold = window.innerHeight * 0.9;
39 var seen = [];
40 function add(el) {
41 // Leave the first screen alone: it is already in view on arrival.
42 if (el.getBoundingClientRect().top < fold) return;
43 for (var i = 0; i < seen.length; i++) if (seen[i].contains(el) || el.contains(seen[i])) return;
44 seen.push(el);
45 }
46 document.querySelectorAll(cards).forEach(add);
47 document.querySelectorAll(blocks).forEach(function (el) {
48 if (!el.querySelector(cards)) add(el);
49 });
50 if (!seen.length) return;
51
52 var io = new IntersectionObserver(function (entries) {
53 var order = 0;
54 entries.forEach(function (e) {
55 if (!e.isIntersecting) return;
56 e.target.style.transitionDelay = Math.min(order++ * 80, 320) + "ms";
57 e.target.classList.add("tl-in");
58 e.target.addEventListener("transitionend", function clear() {
59 e.target.style.transitionDelay = "";
60 e.target.removeEventListener("transitionend", clear);
61 });
62 io.unobserve(e.target);
63 });
64 }, { threshold: 0.12, rootMargin: "0px 0px -6% 0px" });
65
66 seen.forEach(function (el) {
67 el.classList.add("tl-reveal");
68 io.observe(el);
69 });
70 }
71
72 if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", start);
73 else start();
74})();