Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$('#loading').fadeOut();
var i = 0;
$(elementsLoaded).fadeInWithDelay();
if ($('#content').children().size() > 100){ // if more than 100 results already loaded, then stop pagination (only for testing)
if ($('#content').children().size() > 30){ // if more than 100 results already loaded, then stop pagination (only for testing)
$('#nomoreresults').fadeIn();
$('#content').stopScrollPagination();
}
Expand Down Expand Up @@ -99,6 +99,7 @@ <h1>Example</h1>
</ul>
<div class="loading" id="loading">Wait a moment... it's loading!</div>
<div class="loading" id="nomoreresults">Sorry, no more results for your pagination demo.</div>
<div class="loading" id="errloading">Something error, data load faile.</div>
</div>
</body>
</html>
22 changes: 6 additions & 16 deletions scripts/jquery.js

Large diffs are not rendered by default.

45 changes: 33 additions & 12 deletions scripts/scrollpagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


$.fn.scrollPagination = function(options) {

var opts = $.extend($.fn.scrollPagination.defaults, options);
var target = opts.scrollTarget;
if (target == null){
Expand All @@ -34,26 +33,44 @@
};

$.fn.scrollPagination.loadContent = function(obj, opts){
if ($(obj).prop('scrollPaginationOnLoading')){
return;
}

var target = opts.scrollTarget;
var mayLoadContent = $(target).scrollTop()+opts.heightOffset >= $(document).height() - $(target).height();
if (mayLoadContent){
$(obj).prop('scrollPaginationOnLoading', true);

if (opts.beforeLoad != null){
opts.beforeLoad();
}
$(obj).children().attr('rel', 'loaded');
$.ajax({
type: 'POST',
type: opts.method,
url: opts.contentPage,
data: opts.contentData,
success: function(data){
$(obj).append(data);
var objectsRendered = $(obj).children('[rel!=loaded]');

if (opts.afterLoad != null){
opts.afterLoad(objectsRendered);
}
$(obj).prop('scrollPaginationOnLoading', false);

if (opts.dataType == 'html'){
$(obj).append(data);
var objectsRendered = $(obj).children('[rel!=loaded]');
if (opts.afterLoad != null){
opts.afterLoad(objectsRendered);
}
} else if (opts.dataType == 'json') {
if (opts.afterLoad != null){
opts.afterLoad(data);
}
}
},
dataType: 'html'
error: function(){
if(opts.errorLoad != null){
opts.errorLoad();
}
},
dataType: opts.dataType
});
}

Expand All @@ -62,7 +79,8 @@
$.fn.scrollPagination.init = function(obj, opts){
var target = opts.scrollTarget;
$(obj).attr('scrollPagination', 'enabled');

$(obj).prop('scrollPaginationOnLoading', false);

$(target).scroll(function(event){
if ($(obj).attr('scrollPagination') == 'enabled'){
$.fn.scrollPagination.loadContent(obj, opts);
Expand All @@ -80,8 +98,11 @@
'contentPage' : null,
'contentData' : {},
'beforeLoad': null,
'afterLoad': null ,
'afterLoad': null,
'errorLoad': null,
'scrollTarget': null,
'heightOffset': 0
'heightOffset': 0,
'method': 'GET',
'dataType': 'html'
};
})( jQuery );