-
Notifications
You must be signed in to change notification settings - Fork 3
/
StickyScroller.js
323 lines (280 loc) · 9.82 KB
/
StickyScroller.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*****************************************************************************/
// Class Scroller
// Purpose: Create a fixed scroller
// Parameters:
// obj: The object that will be scrolling
// start: What distance from the top (in px) the effect starts
// end: What distance from the top (in px) the effect ends
// interval: What scroll distance triggers the callback
// range: How many pixels after the
// margin: Margin from the top of the browser
// Dependencies:
// GetSet class. Included in Vert Library @ http://vertstudios.com/vertlib.js
//****************************************************************************/
function StickyScroller(obj, options)
{
//Keep track of how many scrollers we have
if ( typeof StickyScroller.counter === 'undefined' )
{
StickyScroller.counter = 0;
}
else
{
StickyScroller.counter++;
}
//Store function scope
var $this = this;
//Store initial top and left
var offset = $(obj).offset();
var top = offset.top;
var left = offset.left;
var cssLeft = $(obj).css('left');
var cssTop = $(obj).css('top');
var parentHeight = $(obj).parent().height();
var scroll = 0;
//------------------------------------------------------------
// Set default property values
//------------------------------------------------------------
var defaults = {
start: "parent",
end: "parent",
interval: null,
margin: 100,
range: $(obj).height()
}, settings = jQuery.extend(defaults,options);
obj = $(obj);
if(settings.start === "parent"){
settings.start = $(obj).parent().offset().top - settings.margin;
}
if(settings.end === "parent"){
settings.end = $(obj).parent().offset().top
+ parentHeight
- $(obj).height()
- settings.margin;
}
this.recalculate = function(){
offset = $(obj).offset();
top = offset.top;
left = offset.left;
$(obj).css({
position : 'absolute',
top: 0,
left: 0
});
$("#scrollcontainer"+ StickyScroller.counter).css({
position : 'absolute',
top: $(window).scrollTop()+settings.margin,
left: cssLeft
});
settings.end += (settings.end - settings.margin)
console.log("End: " + settings.end)
};
settings.index = 0;
settings.oldIndex = 0;
//Accessors for settings
GetSet.getters({scope: $this, obj: settings});
GetSet.setters({scope: $this, obj: settings});
//------------------------------------------------------------//
// Callback Functions //
//------------------------------------------------------------//
var Callback = {};
Callback.newIndex = function(){}; //When the index changes
Callback.limbo = function(){}; //When scroller not in range
Callback.scroll = function(){}; //On window scroll
//Get setters for Callback functions
GetSet.setters({scope: this, prefix: "on", obj: Callback});
//=========================================================//
//Public distanceFrom
//Purpose: Determines the distance in pixels between
// the scroller and an index
//Parameters:
// index: The index whose distance from scroller will be calculated
//Postcondition: Returns an integer
//=========================================================//
this.distanceFrom = function(index)
{
//Check for both references: "Top" of the range and "bottom"
var top = index*settings.interval + settings.start
var bottom = index*settings.interval + settings.range + settings.start
var distanceFromTop = Math.abs(scroll-top);
var distanceFromBottom = Math.abs(scroll-bottom);
//Return the smallest distance
if(distanceFromTop < distanceFromBottom)
{
return distanceFromTop;
}
else
{
return distanceFromBottom;
}
};
//=========================================================//
//Public closestIndex
//Purpose: Determines the closest index
//Postcondition: Returns the closest index as an integer
//=========================================================//
this.closestIndex = function()
{
//If index is 0, automatically return 1
if(settings.index === 0)
{
return 1;
}
//Distance from next/previous index
var dPrev = this.distanceFrom(settings.index-1);
var dNext = this.distanceFrom(settings.index+1);
//Return the index associated with the smallest distance
if(dPrev <= dNext)
{
return settings.index-1;
}
else
{
return settings.index+1;
}
};
//=========================================================//
//Private getIndex
//Purpose: returns index
//=========================================================//
var getIndex = function()
{
//Make sure movement would be in the bounds
if(scroll > settings.start && scroll < settings.end)
{
//Possible new index
tempIndex = Math.floor((scroll-settings.start)/settings.interval);
//Make sure the index is different before reassigning
//or executing the callback
if(tempIndex !== settings.index)
{
//Store old index
settings.oldIndex = settings.index;
//Assign new index
settings.index = tempIndex;
}
}
//If scroll goes beyond end mark, set distance at end mark
else if(scroll >= settings.end)
{
settings.oldIndex = settings.index;
settings.index =
Math.floor((settings.end-settings.start)/settings.interval);
}
//If scroll goes beyond beginning mark, set distance at start
else
{
settings.oldIndex = settings.index;
settings.index = 0;
}
};
//=========================================================//
//Public firstIndex
//Purpose: Returns first index
//Postcondition: Returns an integer
//=========================================================//
this.firstIndex = function()
{
return 0;
};
//=========================================================//
//Public lastIndex
//Purpose: Returns last index
//Postcondition: Returns an integer
//=========================================================//
this.lastIndex = function()
{
return Math.floor((settings.end-settings.start+settings.margin)
/settings.interval);
};
//=========================================================//
//Public inRange
//Purpose: Determines if the scroller is in interval range
//Postcondition: Returns boolean
//=========================================================//
this.inRange = function()
{
var upperbound = settings.index * settings.interval + settings.start;
var lowerbound = settings.index * settings.interval
+ settings.start + settings.range;
var inRange = (scroll >= upperbound ) && (scroll <= lowerbound);
return inRange;
};
//------------------------------------------------------------//
// On Browser Scroll //
//------------------------------------------------------------//
var wrap = $('<div id="scrollcontainer' + StickyScroller.counter + '">')
.css({
width: obj.width(),
height: obj.height(),
position: "absolute",
top: cssTop,
left: cssLeft
});
obj.wrap(wrap);
$(obj).css({
top: 0,
left: 0
});
$(window).scroll(function()
{
scroll = $(window).scrollTop();
//Get the current index
if(settings.interval){
getIndex();
}
//If scroll less than beginning, set back to beginning
if(scroll < settings.start)
{
$(obj).css({
position : 'absolute',
top: 0,
left: 0});
$("#scrollcontainer"+ StickyScroller.counter).css({
position : 'absolute',
top: cssTop,
left: cssLeft
});
}
//If scroll greater than ending position, set to end
else if(scroll > settings.end)
{
$(obj).css({
position : 'absolute',
top: 0,
left: 0
});
$("#scrollcontainer"+ StickyScroller.counter).css({
position : 'absolute',
top: parentHeight - $(obj).height(),
left: cssLeft
});
}
//Make sure we stay in the specified boundaries
else
{
//Put back to fixed
$(obj).css({
position : 'fixed',
top: settings.margin,
left: left
});
}
//If in the specified range and a new index, do the callback
if(settings.oldIndex !== settings.index && settings.interval)
{
Callback.newIndex(settings.index);
}
//Do the "limbo" call back, which is a callback that executes when
//the scroller is not in the range, but still between start and end
if(!$this.inRange()
&& scroll > settings.start
&& scroll <settings.end
&& settings.interval)
{
Callback.limbo(settings.index);
}
//Do the scroll callback regardless of what happens
Callback.scroll(settings.index);
});
}