I think the this
keyword you use in var wScroll = $(this).scrollTop();
is cause the problem. If you want the scrollTop of any element, write the selector of element (I think in here it is window). Also if you want to make it work for the first time, then you should write another algorithm that scrolled variable is not false there for very first time.
But I look at snippet below:
A brief description:
We have a function isVisible that detect if an element is visible in just window section (not above it and neither below). Also each scroll, limit the search area with selecting vidCard
s that are not have is-showing
class (by using this approach, detection will be faster and the animation will be smooth and not take a long time to show!). I assumed that is-showing
have opacity effect.
I hope it helps :)
function isVisible(el) {
var $el = $(el),
above = $el.offset().top - $(window).scrollTop() + $el.outerHeight() < 0,
below = $el.offset().top > $(window).outerHeight() + $(window).scrollTop();
return !above && !below;
}
function loadCards() {
$('.vidCard:not(.is-showing)').each(function(i) {
var card = $(this);
if (isVisible(card) && !card.hasClass('is-showing')) {
setTimeout(function() {
card.addClass('is-showing');
}, 150 * (i + 1));
}
});
}
$(window).on('scroll', function() {
loadCards();
});
loadCards();
.vidCard {
width: 100px;
height: 100px;
background-color: #eee;
margin-bottom: 20px;
opacity: 0;
}
.is-showing {
opacity: 1;
-webkit-transition: all ease 0.6s;
-moz-transition: all ease 0.6s;
-ms-transition: all ease 0.6s;
-o-transition: all ease 0.6s;
transition: all ease 0.6s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>
<div class="vidCard"></div>