Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Step 5 - Adjust scroll position in case of layout shift
  • Loading branch information
shaileshpandit committed Feb 10, 2021
1 parent ce0d430 commit df95990
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions script.js
Expand Up @@ -18,6 +18,9 @@ loadUi();
function loadUi() {
getSections().then(sections => {
populateGrid(document.getElementById("grid"), sections);

// simulating directly jumping to random scroll position
window.scrollTo({ top: 10000 });
});
}

Expand Down Expand Up @@ -90,6 +93,10 @@ function populateSection(sectionDiv, segments) {
sectionToAdjustDiv.style.top = `${sectionStates[sectionToAdjustId].top}px`;
});

// adjust scroll if user is scrolling upwords and we loaded some section above current scroll position
if (window.scrollY > sectionStates[sectionId].top) {
window.scrollBy(0, heightDelta);
}
}

// generates Segment html and height
Expand Down Expand Up @@ -128,14 +135,18 @@ function handleSectionIntersection(entries, observer) {

if (entry.isIntersecting) {
getSegments(sectionDiv.id).then(segments => {
if (sectionStates[sectionDiv.id].lastUpdateTime !== entry.time) {
console.log("new updates received on section, discarding update for", sectionDiv.id, entry.time);
return;
}
populateSection(sectionDiv, segments);
window.requestAnimationFrame(() => {
if (sectionStates[sectionDiv.id].lastUpdateTime === entry.time) {
populateSection(sectionDiv, segments);
}
});
});
} else {
detachSection(sectionDiv, entry.time);
window.requestAnimationFrame(() => {
if (sectionStates[sectionDiv.id].lastUpdateTime === entry.time) {
detachSection(sectionDiv, entry.time)
}
});
}
});
}

0 comments on commit df95990

Please sign in to comment.