Skip to content

Commit 56fc804

Browse files
committed
Fix scroll bounce-back caused by scrollIntoView hijacking momentum
Replace scrollIntoView with manual scrollBy on the family nav container to avoid interrupting macOS/iOS momentum scrolling. Remove conflicting native loading="lazy" from img tags since we manage lazy loading via IntersectionObserver. Add min-height fallback on image containers. Made-with: Cursor
1 parent 6befce3 commit 56fc804

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

css/styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ main {
422422
position: relative;
423423
width: 100%;
424424
aspect-ratio: 4 / 3;
425+
min-height: 180px;
425426
background: #e8e0d8;
426427
overflow: hidden;
427428
}

js/app.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
${BUTTERFLY_SVG}
100100
<span class="loading-label">Loading photo</span>
101101
</div>
102-
<img alt="${sp.commonName} (${sp.scientificName})" data-species="${sp.scientificName}" loading="lazy">
102+
<img alt="${sp.commonName} (${sp.scientificName})" data-species="${sp.scientificName}">
103103
${badges.length ? `<div class="card-badges">${badges.join("")}</div>` : ""}
104104
</div>
105105
<div class="card-body">
@@ -280,6 +280,7 @@
280280
function setupScrollSpy() {
281281
const familyLinks = document.querySelectorAll(".family-link");
282282
const sections = document.querySelectorAll(".family-section");
283+
const navScroller = document.getElementById("family-nav");
283284

284285
const observer = new IntersectionObserver((entries) => {
285286
entries.forEach(entry => {
@@ -288,7 +289,13 @@
288289
const activeLink = document.querySelector(`.family-link[data-family="${entry.target.id}"]`);
289290
if (activeLink) {
290291
activeLink.classList.add("active");
291-
activeLink.scrollIntoView({ block: "nearest", inline: "center", behavior: "smooth" });
292+
// Scroll the nav pill into view without touching the page scroll.
293+
// scrollIntoView can hijack momentum scrolling on macOS/iOS,
294+
// so we manually adjust the horizontal scroll of the nav container.
295+
const navRect = navScroller.getBoundingClientRect();
296+
const linkRect = activeLink.getBoundingClientRect();
297+
const offset = linkRect.left - navRect.left - navRect.width / 2 + linkRect.width / 2;
298+
navScroller.scrollBy({ left: offset, behavior: "smooth" });
292299
}
293300
}
294301
});

0 commit comments

Comments
 (0)