/* ── #ANCHORS THAT LAND WHERE THEY SAY ──────────────────────────────────── * * Two links now point into the middle of NEUStory: "Leadership" in the footer, * and the "About Us" button inside the credentials dome, both aimed at * `/neustory#founders`. Arriving from another page, the browser does the right * thing on its own and this file changes nothing. * * What it fixes is the case with NO page load — pressing a hash link while * already on the page, or going back and forward between two hashes. The * footer is on every page, so "Leadership" pressed FROM /neustory is exactly * that case, and the pillar pages are long enough that landing at the wrong * end of eleven thousand pixels is indistinguishable from a dead link. * * `scrollIntoView` rather than `scrollTo(0, offset)`: the page's images size * themselves late, so an offset measured now can be wrong by the time it is * used, while "reveal this box" stays correct. * * (A note for whoever reads this next: `html { overflow-x: clip }` at the top * of site.css does NOT block scrolling, though it looks like it might. That * was measured — every scroll API returns 0 in a tab Chrome has backgrounded, * which is easy to mistake for the CSS being at fault.) */ (function () { /* An id is not a selector. `#2025` is a legal fragment and an illegal CSS selector, so querySelector would throw on it. */ function find(hash) { if (!hash || hash.length < 2) return null; var id = hash.slice(1); try { id = decodeURIComponent(id); } catch (e) { /* leave it */ } return document.getElementById(id) || null; } function land(hash, smooth) { var mark = find(hash); if (!mark) return false; mark.scrollIntoView({ behavior: smooth ? 'smooth' : 'auto', block: 'start' }); return true; } /* ── ARRIVING WITH A HASH ── * The browser has already jumped by now. This is the second look, for the * pages whose hero image gets its height late and moves everything below it * after the jump has happened. * * ONLY IF THE VISITOR HAS NOT TOUCHED THE PAGE. Someone who lands and * immediately starts scrolling must not be hauled back a third of a second * later — that is worse than the miss it is correcting. */ if (location.hash) { var want = location.hash; var theirs = false; var noticed = function () { theirs = true; }; addEventListener('wheel', noticed, { passive: true, once: true }); addEventListener('touchstart', noticed, { passive: true, once: true }); addEventListener('keydown', noticed, { once: true }); setTimeout(function () { if (theirs) return; var mark = find(want); // already within a hair of right? leave it alone if (mark && Math.abs(mark.getBoundingClientRect().top) < 4) return; land(want, false); }, 400); } /* ── A HASH LINK PRESSED ON THE PAGE IT POINTS AT ── */ document.addEventListener('click', function (e) { var a = e.target.closest && e.target.closest('a[href*="#"]'); if (!a || a.hasAttribute('data-noanchor')) return; if (a.target === '_blank' || e.metaKey || e.ctrlKey || e.shiftKey) return; var href = a.getAttribute('href') || ''; var cut = href.indexOf('#'); var path = href.slice(0, cut); var hash = href.slice(cut); if (hash.length < 2) return; // a bare "#" if (path && path !== location.pathname) return; // a real navigation if (land(hash, true)) { e.preventDefault(); history.replaceState(null, '', hash); } }); /* Back and forward between hashes, which never reload. */ addEventListener('hashchange', function () { land(location.hash, true); }); })();