I'm using a sticky header which changes to position:fixed, as soon as you scroll up. For detecting the upwards scroll, I compare the current scrollTop with the old scrolltop – this works perfectly fine!
Now I'm implementing content-visibility for my footer to save some rendering time on the page load.
$(document).ready(function(){
var lastScrollTop = window.pageOffsetY || 0;
var isFixed = false;
$(window).on('scroll', _.throttle(function(){
var scrollTop = $(window).scrollTop();
if(scrollTop > 200 && scrollTop < lastScrollTop && !isFixed){
console.log('stick header');
$('.header').addClass('header-fixed');
isFixed = true;
} else if((scrollTop > lastScrollTop && isFixed) || scrollTop == 0){
console.log('unstick header');
$('.header').removeClass('header-fixed');
isFixed = false;
}
lastScrollTop = scrollTop;
}, 500));
});
.content {
height: 200vh;
background-color: grey;
}
.content-visibility {
content-visibility: auto;
}
.content-inner {
height: 100px;
background-color: green;
}
img.content-inner {
height: auto;
}
.content.content-visibility {
height: auto;
background-color: #ddd;
}
.footer {
background-color: #eee;
}
.header {
height: 200px;
background-color: white;
}
.header-fixed {
position: fixed;
top: 0;
width: 100vw;
left: 0;
z-index: 10;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
<div class="container">
<div class="header">
header
</div>
<div class="content">
content 1
</div>
<div class="content-visibility">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/301x310">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/302x303">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/302x340">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/303x340">
<div style="height:200px"></div>
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/304x340">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/305x340">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/306x340">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/307x340">
</div>
<div class="content">
content 2
</div>
<div class="footer content-visibility">
footer
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/301x300">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/302x300">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/302x300">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/303x300">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/304x300">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/305x300">
<div style="height:200px"></div>
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/306x300">
<img loading="lazy" class="content-inner" src="https://via.placeholder.com/307x300">
</div>
</div>
if you scroll down fast, you can see the log statement "stick header", this is wrong and should only happen on scroll up. The problem now is, that at the moment the browser is rendering content-visibility elements, the javascript detects an upwards scroll, because the document height changes (I guess).
Is there any way to prevent this?