jquery.more.js
3.59 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(function($) {
var target = null;
var template = null;
var lock = false;
var is_stop = true;
var cur_last = 0;
var variables = {
'last': 0
}
var settings = {
'amount': '10',
'address': 'comments.php',
'format': 'json',
'template': '.single_item',
'trigger': '.get_more',
'scroll': 'false',
'offset': '100',
'spinner_code': ''
}
var methods = {
init: function(options) {
return this.each(function() {
if (options) {
$.extend(settings, options);
}
template = $(this).children(settings.template).wrap('<div/>').parent();
template.css('display', 'none')
$(this).append('<div class="more_loader_spinner">' + settings.spinner_code + '</div>')
template.remove();
target = $(this);
if (settings.scroll == 'false') {
$(this).find(settings.trigger).bind('click.more', methods.get_data);
$(this).more('get_data');
} else {
if ($(this).height() <= $(this).attr('scrollHeight')) {
target.more('get_data', settings.amount * 2);
}
$(this).bind('scroll.more', methods.check_scroll);
}
})
},
check_scroll: function() {
if ((target.scrollTop() + target.height() + parseInt(settings.offset)) >= target.attr('scrollHeight') && lock == false) {
target.more('get_data');
}
},
debug: function() {
var debug_string = '';
$.each(variables, function(k, v) {
debug_string += k + ' : ' + v + '\n';
})
alert(debug_string);
},
remove: function() {
target.children(settings.trigger).unbind('.more');
target.unbind('.more')
target.children(settings.trigger).remove();
},
add_elements: function(data) {
//alert('adding elements')
var root = target
// alert(root.attr('id'))
var counter = 0;
if (data) {
$(data).each(function() {
counter++
var t = template
$.each(this, function(key, value) {
if (t.find('.' + key)) t.find('.' + key).html(value);
})
//t.attr('id', 'more_element_'+ (variables.last++))
if (settings.scroll == 'true') {
// root.append(t.html())
root.children('.more_loader_spinner').before(t.html())
} else {
//alert('...')
root.children(settings.trigger).before(t.html())
}
root.children(settings.template + ':last').attr('id', 'more_element_' + ((variables.last++) + 1));
})
} else methods.remove()
target.children('.more_loader_spinner').css('display', 'none');
if (counter < settings.amount) methods.remove()
},
get_data: function() {
//alert('getting data')
var ile;
lock = true;
target.children(".more_loader_spinner").css('display', 'block');
$(settings.trigger).css('display', 'none');
if (typeof(arguments[0]) == 'number') ile = arguments[0];
else {
ile = settings.amount;
}
if(variables.last >= cur_last){
$.post(settings.address, {
last: variables.last,
amount: ile
}, function(data) {
$(settings.trigger).css('display', 'block')
methods.add_elements(data)
lock = false;
}, settings.format)
cur_last = cur_last+10;
}
}
};
$.fn.more = function(method) {
if (methods[method]) {
//load_flag = true;
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method == 'object' || !method) {
//load_flag = true;
return methods.init.apply(this, arguments);
} else $.error('Method ' + method + ' does not exist!');
}
$(window).scroll(function() {
if (is_stop) {
if ($(window).scrollTop() == $(document).height() - $(window).height() && is_stop == true) {
is_stop == false;
$('.get_more').click();
is_stop == true;
}
}
});
})(jQuery)