Is there a way to determine the scroll direction when an event is triggered?
I've considered using the boundingClientRect
in the returned object to store the last scroll position, but I'm concerned about potential performance issues.
Could the intersection event be utilized to detect the scroll direction (up / down)?
I have included a basic snippet below and would greatly appreciate any assistance.
Snippet:
var options = {
rootMargin: '0px',
threshold: 1.0
}
function callback(entries, observer) {
entries.forEach(entry => {
if (entry.isIntersecting) {
console.log('entry', entry);
}
});
};
var elementToObserve = document.querySelector('#element');
var observer = new IntersectionObserver(callback, options);
observer.observe(elementToObserve);
#element {
margin: 1500px auto;
width: 150px;
height: 150px;
background: #ccc;
color: white;
font-family: sans-serif;
font-weight: 100;
font-size: 25px;
text-align: center;
line-height: 150px;
}
<div id="element">Observed</div>
I am looking for a solution to implement on a fixed headers menu to show/hide it based on the scroll direction.