I have looked at multiple formulas for getting the window offset where when scrolled to the top the offset is 0
and when scrolled to the bottom the offset is the offsetHeight
. None have worked. Some give 0
when at the top but not the right height when at the bottom and vise versa.
This is the closest I have gotten:
document.addEventListener('scroll', setBackgroundColor)
function setBackgroundColor() {
let offset = (window.innerHeight + window.scrollY) / document.body.offsetHeight
console.log(offset, window.innerHeight + window.scrollY, document.body.offsetHeight)
}
html,
body {
margin: 0;
padding: 0;
height: 500vh;
}
The closest I have gotten to zero is 0.2
, but the height works fine. What would the formula be for getting 0
and scrollHeight
?