I'm trying to create a script that will match div ids that scroll into view with the corresponding children in a gallery. I don't want to have to create individual scrollTop codes for each div id—so needing some kind of variable for the index of each div id? I've struggled to find anything close online.
This is the simple scrollTop code I am using as an example. When the "in-view" class is applied, the corresponding gallery items change their opacity.
<script>
$(function(){
$(document).scroll(function(){
if($(this).scrollTop() >= $("#item-1").offset().top - 250) {
$(".sticky-gallery .image-item:nth-child(1)").addClass("in-view");
} else {
$(".sticky-gallery .image-item:nth-child(1)").removeClass("in-view");
}
});
});
</script>
I'm hoping there's a way to create something that can flexibly work for #item-1 with nth-child(1), #item-2 with nth-child(2), etc...
Thank you for any guidance.