ischenkodv (owner)

Revisions

gist: 97794 Download_button fork
public
Description:
Script for endless scrolling using jQuery
Public Clone URL: git://gist.github.com/97794.git
endless.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// script for endless scrolling using jQuery
// demo: http://www.jstoolbox.com/demo/endless/
var engine = {
 
posts : [],
target : null,
busy : false,
count : 5,
 
render : function(obj){
var xhtml = '<div class="post" id=post_'+obj.id+'>';
if (obj.title) {
xhtml += '<h2>'+obj.title+'</h2>';
}
if (obj.posted_at) {
xhtml += '<div class="posted_at">Posted on: '+obj.posted_at+'</div>';
}
if (obj.comments_count) {
xhtml += '<div class="comments_count">Comments: ' + obj.comments_count + '</div>';
}
xhtml += '<div class="content">' + obj.content + '</div>';
xhtml += '</div>';
 
return xhtml;
},
 
init : function(posts, target){
 
if (!target)
return;
 
this.target = $(target);
 
this.append(posts);
 
var that = this;
$(window).scroll(function(){
if ($(document).height() - $(window).height() <= $(window).scrollTop() + 50) {
that.scrollPosition = $(window).scrollTop();
that.get();
}
});
},
 
append : function(posts){
posts = (posts instanceof Array) ? posts : [];
this.posts = this.posts.concat(posts);
 
for (var i=0, len = posts.length; i<len; i++) {
this.target.append(this.render(posts[i]));
}
 
if (this.scrollPosition !== undefined && this.scrollPosition !== null) {
$(window).scrollTop(this.scrollPosition);
}
},
 
get : function() {
 
if (!this.target || this.busy) return;
 
if (this.posts && this.posts.length) {
var lastId = this.posts[this.posts.length-1].id;
} else {
var lastId = 0;
}
 
this.setBusy(true);
var that = this;
 
$.getJSON('getposts.php', {count:this.count, last:lastId},
function(data){
if (data.length > 0) {
that.append(data);
}
that.setBusy(false);
}
);
},
 
showLoading : function(bState){
var loading = $('#loading');
 
if (bState) {
$(this.target).append(loading);
loading.show('slow');
} else {
$('#loading').hide();
}
}
 
setBusy : function(bState){
this.showLoading(this.busy = bState);
}
};
 
// usage
$(document).ready(function(){
engine.init(null, $("#blog"));
engine.get();
});