When I run the example (in full page view) in Chrome (98) (or Safari 15.3) the first element directly vanishes when scrolling just a little bit, so kind of 'when leaving the non-padding or entering the padding-area'
The options define a rootMargin: '0px 0px 0px 0px' which I thought refer to the root elements border. That's how it behaves in Firefox(97). The inner element is only hidden when it reaches the top.
Is there a way to define the wrapper borders as boundary and not the 'inner padding-border'?
const wrapper = document.querySelector('#wrapper')
const firstElem = document.querySelector('#first-elem')
const options = {
root: wrapper,
rootMargin: '0px 0px 0px 0px',
threshold: 1
}
const observer = new IntersectionObserver(handleFade, options);
observer.observe(firstElem)
function handleFade(entries) {
entries.forEach(entry => {
let target = entry.target
if (entry.isIntersecting) {
target.classList.remove('fade-out')
} else {
target.classList.add('fade-out')
}
})
}
body {
padding: 0;
overflow: hidden;
}
#wrapper {
height: 100vh;
padding-top: 10rem;
overflow: auto;
border: 1px solid darkmagenta;
box-sizing: border-box;
}
.elem {
border: 3px solid teal;
padding: 0 2rem;
height: 20rem;
}
.fade-out {
visibility: hidden;
}
<div id="wrapper">
<div class="elem" id="first-elem">first watched element</div>
<div class="elem">element</div>
<div class="elem">element</div>
<div class="elem">element</div>
</div>
