-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpager.js
78 lines (68 loc) · 2.31 KB
/
pager.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
$.fn.pager = function(options) {
// Set wrapper to the element Pager is running on
var wrapper = '#' + this.attr('id');
// Setup default values if they aren't declared by the Pager initialization
var defaults = {
// The element used for page links
link: '.pagination a',
// Pull only the entries within the wrapper of the next page
pull: wrapper + ' > *',
// The ID of the loader element
loaderID: 'loader',
// Where the browser should scroll to after the page loads
scrollTo: wrapper,
// Speed of the fade animation
fadeSpeed: 100,
// Opacity of the wrapper when loading the next page of entries
fadeOpacity: .5
};
// Extend all default settings into usable options
var options = $.extend(defaults, options);
// Add the correct hash to the URL when a user interacts with the pagination
$(wrapper).on("click", options.link, function(e) {
e.preventDefault();
var linkPath = $(this).attr('href');
window.location = '#' + linkPath;
});
// If a user uses a hash in the URL, load it
if (location.hash) {
var hash = location.hash;
var hashURL = hash.replace( /^#/, '');
loadPagination(hashURL);
}
// Hashchange history
$(window).hashchange(function() {
var hash = location.hash;
var hashURL = hash.replace( /^#/, '');
if (hash) {
// If the URL has a hash, load the page content into the wrapper
loadPagination(hashURL);
} else {
// If the URL does not have a hash, load the original content into the wrapper
loadPagination(window.location.href);
}
});
// Load selected page
function loadPagination(loadURL) {
$(wrapper, function() {
// Fade out wrapper during load
$(wrapper).fadeTo(options.fadeSpeed, options.fadeOpacity);
// Insert loading element
$(wrapper).after('<i id="' + options.loaderID + '" style="display: none;"></i>');
// Fade in loading element
$('#' + options.loaderID).fadeIn(options.fadeSpeed);
}).load(loadURL + ' ' + options.pull, function() {
// After load, fade results in
$(wrapper).fadeTo(options.fadeSpeed, 1);
// Scroll up to top of content
var scroller = $(options.scrollTo).offset().top - 20
$('body, html').animate({
scrollTop: scroller
}, 500);
// Remove loading element
$('#' + options.loaderID).fadeOut(options.fadeSpeed, function() {
$('#' + options.loaderID).remove();
});
});
}
};