Skip to content

Commit

Permalink
feat: scroll the selected element to the center of the viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
ARandomOWL committed Feb 17, 2024
1 parent 134003b commit b71dc16
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,8 @@ const scrollToElement = (searchEngine, element) => {
}
const elementBounds = element.getBoundingClientRect();
const scrollY = window.scrollY;
if (elementBounds.top < topMargin) {
// scroll element to top
element.scrollIntoView(true);
window.scrollBy(0, -topMargin);
} else if (elementBounds.bottom + bottomMargin > window.innerHeight) {
// scroll element to bottom
element.scrollIntoView(false);
window.scrollBy(0, bottomMargin);
}
window.scrollTo(0,
elementBounds.top + window.pageYOffset - (window.innerHeight / 2));
return Math.abs(window.scrollY - scrollY) > 0.01;
};

Expand Down

0 comments on commit b71dc16

Please sign in to comment.