Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Ajax loader on scroll

Featured Replies

I'm using the following code to load in new wordpress posts on click of a category or "Show more"

This is working fine, but I want to replace the 'bloc-load' click action with it automatically loading when scrolling to the bottom of the page.

 
var page = 1; 
var loading = true; 
var root="http://www.example.com/";
var cat = "home"; 

$("body").delegate("#bloc-load","click", function() {
	console.log("loading more...");
	loading = true;  
	page++;  
	load_posts(16,page,cat);  
});

$("body").delegate("#bloc-menu a","click", function () {
	cat = $(this).text().toLowerCase();
	var url = document.URL;
	if(window.location.hash) {
		url = url.substr(0,url.indexOf("#"));
	}
	console.log(url);
	if(url!=root){
		window.location=root+"#"+cat;
	} else {
	window.location.hash=cat;
	console.log("loading "+cat+"...");
	loading = true;  
	page=1;  
	load_posts(16,page,cat);  
	}
	return false;
});
	

function load_posts(n,p,c){  
	console.log("loading posts - "+n+", "+p+", "+c); 
	var $window = $(window);  
	var $content = $("body.blog #home-arts"); 
	if(n==16){
	console.log("reset content...");
	}
	$.ajax({  
		type       : "GET",  
		data       : {numPosts : n, pageNumber: p, cat: c},  
		dataType   : "html",  
		url        : "http://www.example.com/wp-content/themes/example/ajaxlooper.php",  
		beforeSend : function(){  
			if(page != 1){  
				$content.append('<div id="bloc-loader">Loading...</div>');  
			}  
		},  
		success    : function(data){  
			$data = $(data);  
			//console.log($data);
			if($data.length){
				console.log("appending...");  
				$data.hide();  
				if(page == 1){
					$content.html("");
				}
				$content.append($data);  
				$data.fadeIn(500, function(){  
					$("#bloc-loader").remove();  
					loading = false;  
				});  
			} else {  
				$("#bloc-loader").remove();  
			}  
		},  
		error     : function(jqXHR, textStatus, errorThrown) {  
			$("#bloc-loader").remove();  
			alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);  
		}  
  	});  
}

$(function () {
	if(document.URL!=root) {
		cat=window.location.hash;
	}
	console.log("loading...");
	load_posts(16,page,cat);  
});



I did think this would work:

$window.scroll(function() {  
	var content_offset = $content.offset();  
	console.log(content_offset.top);  
	if(!loading && ($window.scrollTop() +  
		$window.height()) > ($content.scrollTop() +  
		$content.height() + content_offset.top)) {  
			console.log("loading more...");
			loading = true;  
			page++;  
			load_posts(16,page,cat);
	}  
});  

But nope....Any ideas?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.