compute-scroll-into-view
compute-scroll-into-view copied to clipboard
Check of if element is in viewport can fail under certain conditions
Hello!
If you have the following scenario then the check starting on line 346 to see if the element is in the viewport will fail:
- You supplied no boundary and are not within a scrollable element
- You gave the HTML element a height of 100%
- You have scrolled more than the current height of the page
Seems that bottom property from getClientBoundingRect
for the HTML element will be negative if conditions two and three hold. I think bottom = top + height in this condition, where top < -height since you scrolled more than the current height of the page. The check targetBottom <= bottom will then fail in this case because targetBottom will be positive.
I have no clue why this is the case, and I might be slightly off in my deduction but I guess to fix this you would need to have an if statement to check for if bottom is negative and if it is recalculate bottom using something like document.documentElement.clientHeight
.
Happy to commit a PR if you agree with the cause + fix @stipsan :)
Hey @Francesco-Lanciana! You're welcome to do a PR 😄 Good investigative work 🕵️