As someone relatively new to jquery, I recently asked a similar question but am encountering difficulties with the final logic in this script.
The issue is that I have a script that adds a class of .prod__viewed
whenever a div scrolls into the viewport. I also have a counter that displays the number of viewed items versus the total number of divs. My goal is to remember all the divs with the class .prod__viewed
upon page reload using sessionStorage. However, currently, the script is adding the class to every div and I can't seem to figure out why.
$.fn.prodInViewport = function() {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};
$(window).on('resize load scroll', function() {
$('.prod__item').each(function() {
if ($(this).prodInViewport()) {
$(this).addClass('prod__viewed');
sessionStorage.setItem('viewed', 'prod__viewed');
}
});
var numItems = $('.prod__item').length;
var totalNumItems = $('.prod__viewed').length;
var prodCountCheck = $('.prod__count__container');
var positionY = $(this).scrollTop();
var pageHeight = $(this).height();
var scrollHeight = $('body').height();
var scrollPercent = (positionY / (scrollHeight - pageHeight)) * 100;
var prodCountElement = $('.prod__count__container').outerHeight();
if (prodCountCheck.length == 1) {
$('.prod__count__content')
.html('<span>You've seen <strong>' + totalNumItems + '</strong> of <strong>' + numItems + '</strong> prods</span>');
$('.prod__load__bar').css({
'top': prodCountElement,
'width': scrollPercent + "%"
});
}
});
if (sessionStorage.getItem("viewed")) {
$(".prod__item").addClass(sessionStorage.getItem("viewed"));
}
<section class="prod__count__wrapper">
<div class="prod__count__container">
<div class="prod__count__content"></div><!-- /.prod__count__content -->
</div><!-- /.prod__count__container -->
<div class="prod__load__bar"></div><!-- /.prod__load__bar -->
</section><!-- /.prod__count__wrapper -->
<section class="prod__wrapper">
<div class="prod__container">
<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->
<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->
<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->
<div class="prod__item">
<div class="prod__item--img&qu"t;>
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->
<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->
<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->
</div>
</section>
If you have any insights on where I might be going wrong, your assistance would be greatly appreciated! Thanks for your help!