I would like to execute a command on scroll before a user reaches the bottom of the screen. My code below loads new dynamic content when a user scrolls midway down the screen but I would like the dynamic content to load outside of the viewport. Is there a way to do this? Will I need to adjust the .outerheight or .offset to load the dynamic content earlier? I've attached a graphic that shows what I am trying to accomplish.
var busy = false;
jQuery( document ).ajaxStart(function() {
busy = true;
});
jQuery( document ).ajaxStop(function() {
busy = false;
});
jQuery(window).bind('scroll', function() {
if(jQuery(window).scrollTop() >= jQuery('#dynamicContent').offset().top + jQuery('#dynamicContent').outerHeight() - window.innerHeight) {
if (!busy) {
jQuery('.load-more').click();
}
}
});
Here's a snippet of my ajax:
$.ajax({
url: '/content',
type: 'post',
data: { categoryData: categoryData},
dataType: 'json',
success: function(res) {
response = res.data;
},
error: function(err) {
error = err;
},
complete: function() {
self.hideLoader();
if (!error) {
$('#dynamicContent').html(response.html);
self.filterSet(response.enabled);
self.setHistory(href, order, filters);
}
}
});