Created
February 12, 2020 12:02
-
-
Save saadwaseem/13ad20faeeda5103c0cd72ce881e372d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Infinite Scrolling for FAQs page. This sample code can be modified to handle infinite scrolling on any page for any type | |
content Text, Media etc. | |
/* | |
* Faqs load on scroll. | |
*/ | |
var track_page = 1; //track user scroll as page number, right now page number is 1 | |
var loading = false; | |
$('.load-more-faqs').hide(); | |
if ($('body').hasClass('tax-faqs_category')) { | |
$(window).scroll(function () { | |
if ($(window).scrollTop() + $(window).height() >= $(window).height()) { //if user scrolled to bottom of the page | |
if (loading == false) { | |
track_page++; | |
load_contents(track_page); | |
} | |
} | |
}); | |
} | |
/* | |
* Faqs load contents. | |
*/ | |
function load_contents(track_page) { | |
if (loading == false) { | |
loading = true; //set loading flag on | |
var position = (track_page - 1) * 5; | |
$('.load-more-faqs').show(); //show loading animation | |
// Ajax request to get posts. | |
$.post(ajaxurl, { | |
action: 'ajax_callback', | |
offset: position, | |
}, | |
function (data) { | |
if (data.result == 0) { | |
// Notify user if nothing to load. | |
$('.faqs-container').append("No more FAQs!"); | |
$('.load-more-faqs').hide(); | |
loading = true; | |
return; | |
} else { | |
loading = false; //set loading flag off once the content is loaded | |
} | |
$('.load-more-faqs').hide(); //hide loading animation once data is received | |
$(".faqs-container").append(data.result); // append data | |
}).fail(function (xhr, ajaxOptions, thrownError) { //any errors? | |
// Notify user if nothing to load. | |
$('.faqs-container').html("No content found!"); | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment