On my website, I want to implement a feature where more divs
load using a smooth fadeIn()
animation when the user reaches the bottom of the page.
Initially, I had this code in mind:
$(document).ready(function() {
$(window).on('scroll resize', function (e) {
/*desktop > 768px*/
if ($(window).scrollTop() + $(window).height() > $(document).height() - 1 && oncedone1 == false && $(window).width() > 768) {
$(".breakpoint").fadeIn(2500).css({
"display": "grid"
});
$("#home-page-best-anime, .home-page-best-anime, #home-page-season-anime, .home-page-season-anime, #home-page-genre-anime, #home-page-action-anime, .home-page-action-anime").hide();
if ($(window).scrollTop() + $(window).height() > $(document).height() - 1 && oncedone2 == false) {
$("#home-page-best-anime, .home-page-best-anime, #home-page-season-anime, .home-page-season-anime").fadeIn(2500).css({
"display": "grid"
});
if ($(window).scrollTop() + $(window).height() > $(document).height() - 1 && oncedone3 == false) {
$("#home-page-genre-anime, #home-page-action-anime, .home-page-action-anime").fadeIn(2500).css({
"display": "grid"
});
oncedone1 = true;
oncedone2 = true;
oncedone3 = true;
//oncedone4 = true;
}
}
}
/*mobile / < 768px*/
//if ($(window).scrollTop() + $(window).height() > $(document).height() - 100 && oncedone1v1 == false /*&& $(window).width() < 768*/) {
//$(".breakpoint").fadeIn(2500).css({ "display": "grid" });
//$("#home-page-best-anime, .home-page-best-anime, #home-page-season-anime, .home-page-season-anime, #home-page-genre-anime, #home-page-action-anime, .home-page-action-anime").hide();
//if ($(window).scrollTop() + $(window).height() > $(document).height() - 100 && oncedone2v1 == false) {
// $("#home-page-best-anime, .home-page-best-anime").fadeIn(2500).css({ "display": "grid" });
//if ($(window).scrollTop() + $(window).height() > $(document).height() - 100 && oncedone3v1 == false) {
// $("#home-page-season-anime, .home-page-season-anime").fadeIn(2500).css({ "display": "grid" });
//if ($(window).scrollTop() + $(window).height() > $(document).height() - 100 && oncedone4v1 == false) {
// $("#home-page-genre-anime, #home-page-action-anime, .home-page-action-anime").fadeIn(2500).css({ "display": "grid" });
//oncedone1v1 = true;
//oncedone2v1 = true;
//oncedone3v1 = true;
//oncedone4v1 = true;
//}
//}
// }
//}
});
However, this approach caused some bugs as it would fadeIn the entire page instead of just the targeted div
. So, I decided to utilize the .each function to handle fading in each div
individually when the user scrolls to the bottom of the page:
$(window).on('scroll resize', function () {
$('.bpshow').each(function (i) {
if ($(window).scrollTop() + $(window).height() == $(document).height() && $(window).width() > 768) {
$(this).fadeIn(2500).css({ "display": "grid" });
}
});
});
You can view the website I am working on here: AniBUY All Anime