I'm developing a web app here http://101drums.com/test and wish to fade in and out elements as they enter and leave the visible part of the scrolling parent element. I have been helped by the answer by myfunkyside in this question Fade In on Scroll Down, Fade Out on Scroll Up - based on element position in window and have adapted this code here https://jsfiddle.net/tciprop/u4vaodvL/. However, either the elements offset top is returning too big and/or the visible part of the scrolling div is returning to low so that the fade creeps into the visible area. Any ideas? Here is my Fiddle https://jsfiddle.net/tciprop/u4vaodvL/.
HTML
<div class="header"></div>
<div class="menu">
<div class="fade">Fade In 01</div>
<div class="fade">Fade In 02</div>
<div class="fade">Fade In 03</div>
<div class="fade">Fade In 04</div>
<div class="fade">Fade In 05</div>
<div class="fade">Fade In 06</div>
<div class="fade">Fade In 07</div>
<div class="fade">Fade In 08</div>
<div class="fade">Fade In 09</div>
<div class="fade">Fade In 10</div>
<div class="fade">Fade In 01</div>
<div class="fade">Fade In 02</div>
<div class="fade">Fade In 03</div>
<div class="fade">Fade In 04</div>
<div class="fade">Fade In 05</div>
<div class="fade">Fade In 06</div>
<div class="fade">Fade In 07</div>
<div class="fade">Fade In 08</div>
<div class="fade">Fade In 09</div>
<div class="fade">Fade In 10</div>
</div>
CSS
body {
overflow-x: hidden;
overflow-y: hidden;
}
.header {
height: 40px;
}
.fade {
position: relative;
margin: 10px 5px 10px 5px;
padding: 10px 5px 10px 5px;
background-color: lightgreen;
opacity: 1;
height: 50px;
}
.menu {
position: relative;
overflow-x: hidden;
overflow-y: scroll;
border: 2vw 3vh;
width: 90vw;
height: 80vh;
margin: auto;
}
JS
$(window).on("load",function() {
$(".menu").scroll(function() {
$(".fade").each(function() {
//find relative positions
var objectTop = $(this).offset().top;
var objectBottom = objectTop + $(this).outerHeight();
var windowTop = $(".menu").scrollTop();
var windowBottom = windowTop + $(".menu").innerHeight();
if (objectTop < windowTop) {//fade out on leaving top of scrollable area
if ($(this).css("opacity")==1) {
$(this).fadeTo(500,0);
}
} else if (objectBottom < windowBottom && objectTop > windowTop) {//fade in on entering top of scrollable area
if ($(this).css("opacity")==0) {
$(this).fadeTo(500,1);
}
} else if (objectBottom < windowBottom) {//fade in on entering bottom of scrollable area
if ($(this).css("opacity")==0) {
$(this).fadeTo(500,1);
}
} else {
if ($(this).css("opacity")==1) {//fade out on leaving bottom of scrollable area
$(this).fadeTo(500,0);
}
}
console.log(objectTop, objectBottom, windowTop, windowBottom);
});
}); $(".menu").scroll(); //invoke scroll-handler on page-load
});