Skip to content

Instantly share code, notes, and snippets.

@kirilkirkov
Created July 28, 2015 13:02
Show Gist options
  • Save kirilkirkov/b8f421db0bc769858dc6 to your computer and use it in GitHub Desktop.
Save kirilkirkov/b8f421db0bc769858dc6 to your computer and use it in GitHub Desktop.
Ajax loading elements with scoll. More comfortably from pagination in some situations!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>Hello, world!</h1>
<hr>
<?php foreach ($arr as $prize) { ?>
<p><b>id:</b> <?= $prize['id'] ?> </p>
<p><b>title:</b> <?= $prize['title'] ?> </p>
<?php } ?>
<div class="progress progress-striped active hidden"><div class="progress-bar" style="width: 100%">Loading ..</div></div>
</div>
<button type="button" class="btn btn-primary active show_more" onclick="loadData()">Show me more</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
$(document).ready(function () {
$(window).scroll(function () {
if ($(window).scrollTop() >= ($(document).height() - $(window).height() - 200)) {
loadData();
}
});
});
var start_var = 40;
var stop_load = false;
function loadData() {
if (stop_load == false) {
$("div.progress.progress-striped").show();
$.ajax({
type: "POST",
url: "ajaxLoad.php",
data: {start: start_var}
}).done(function (data) {
$(".container").last().append(data);
if (data.length == 0) {
stop_load = true;
$(".show_more").text('No more games!');
$(".show_more").addClass("disabled");
}
}).fail(function (err) {
$.post("saveLoadErr.php", {error: JSON.stringify(err)});
alert('We have problem with loading games \nPlease contact with us or wait to recover');
}).always(function () {
$("div.progress.progress-striped").hide();
});
start_var += 40;
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment