-
Notifications
You must be signed in to change notification settings - Fork 28
/
waterfall-light.js
208 lines (184 loc) · 4.91 KB
/
waterfall-light.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* Copyright 2015, Zap Lin
* All rights reserved.
*
* This source code is licensed under the Apache license found in the
* LICENSE file in the root directory of this source tree.
*/
(function($){
// waterfall option of every elements
var g_option = {
// default setting
_init_:{
top : false, // all of column height
w : false, // current container-width
col : false, // grid number
gap : 10,
gridWidth : [0,400,600,800,1200],
refresh: 500,
timer : false,
scrollbottom : false
}
},
// container id
hash = 0;
// waterfall methods (can be called)
var methods = {
init : function() {
// first call, set default
var id = getHashId(this);
if(!g_option[id]){
g_option[id]=$.extend({},g_option._init_);
}
// overwrite setting
if(arguments[0]){
g_option[id]=$.extend(g_option[id],arguments[0]);
}
// if set scroll to bottom, overwrite setting`
if(g_option[id].scrollbottom){
g_option[id].scrollbottom = $.extend({
ele : this.parent(),
endele: $('<div>').css({width:'100%',textAlign:'center',position:'absolute'}),
endtxt: 'No More Data',
gap: 300
},g_option[id].scrollbottom);
}
// Waterfall is a absolute-position base layout
// the container must be relative or absolute position
// This setting needs to be strengthened
this.css('position', 'relative');
// start
detect(this);
},
sort : function() {
sorting(this);
},
stop : function() {
var id = getHashId(this);
if(g_option[id].timer){
clearInterval(g_option[id].timer);
g_option[id].timer = false;
}
},
end : function() {
var id = getHashId(this);
if(g_option[id].scrollbottom){
g_option[id].scrollbottom.ele.css('top',g_option[id].top[getPolesCol(id,true)]+"px");
this.append(g_option[id].scrollbottom.endele);
}
if(g_option[id].timer){
clearInterval(g_option[id].timer);
g_option[id].timer = false;
}
}
};
/*
get min or max col
@param id : hash id
@param boo: true is max ,else min
*/
function getPolesCol(id,boo){
var top = g_option[id].top, col = 0, v =top[col];
for(var i=0;i<top.length;i++) {
if(boo){
if(top[i]>v){
v = top[i];
col = i;
}
}
else{
if(top[i]<v){
v = top[i];
col = i;
}
}
}
return col;
}
function sorting(t){
var id = getHashId(t);
var gw = g_option[id].gridWidth,
w = g_option[id].w,
gap =g_option[id].gap,
scrollbottom = g_option[id].scrollbottom;
g_option[id].col = 1;
g_option[id].top=[];
for(var i=gw.length-1;i>=0;i--){
if(w>gw[i]){
g_option[id].col = i+1; break;
}
}
var cwidth =(w-((g_option[id].col-1)*gap))/g_option[id].col,
left=[];
for(var j=0;j<g_option[id].col;j++){
left.push(j*cwidth+j*gap);
g_option[id].top.push(0);
}
t.children().css({
position:'absolute',
left: (w/2-cwidth/2)+'px',
top: t.scrollTop(),
transition: 'left ' + g_option[id].refresh + 'ms ease-in-out,' +
'top ' + g_option[id].refresh + 'ms ease-in-out,' +
'opacity ' + g_option[id].refresh + 'ms ease-in-out'
}).each(function() {
var ic = getPolesCol(id,false);
$(this).css({ width: cwidth+'px', left: left[ic]+'px', top : g_option[id].top[ic]+'px',opacity:'1' });
g_option[id].top[ic]+=$(this)[0].offsetHeight+gap;
});
//set the waterfall box height
t.css("height",g_option[id].top[getPolesCol(id,true)] + "px");
if(scrollbottom)
if(scrollbottom.endele)
scrollbottom.endele
.addClass('endele')
.text(scrollbottom.endtxt)
.css('top', g_option[id].top[getPolesCol(id,true)]+"px");
}
// detect screen width change , resort cards
function detect(t){
var id = getHashId(t);
if(!g_option[id].timer){
g_option[id].timer = setInterval(function(){
var bw = t[0].offsetWidth;
if(g_option[id].w!==bw) {
g_option[id].w=bw;
sorting(t);
}
if(g_option[id].scrollbottom){
if(g_option[id].scrollbottom.callback && isbottom(g_option[id].scrollbottom.ele,g_option[id].scrollbottom.gap)){
g_option[id].scrollbottom.callback(t);
}
}
},g_option[id].refresh);
}
sorting(t);
}
// return is scroll to bottom
function isbottom(ele,gap){
var wh = $(window).height();
var es = (ele.get(0) == $('body').get(0))?Math.max(ele.scrollTop(), window.pageYOffset):ele.scrollTop();
return (wh+es>(ele.prop("scrollHeight")-gap));
}
// get element unique id
function getHashId(t){
if(!t.attr('wf-id')){
hash+=0.1;
t.attr('wf-id',hash);
}
return t.attr('wf-id');
}
$.fn.waterfall = function() {
var res;
if(!arguments[0] || typeof arguments[0] === 'object'){
res = methods.init.apply(this,arguments);
}
else if(methods[arguments[0]]){
res = methods[ arguments[0] ].apply( this, Array.prototype.slice.call( arguments[0], 1 ));
}
else {
$.error( 'Method ' + arguments[0] + ' does not exist on jQuery.waterfall' );
}
return res || this;
};
})(jQuery);