-
Notifications
You must be signed in to change notification settings - Fork 531
/
jquery-scrolltofixed.js
567 lines (464 loc) · 20.5 KB
/
jquery-scrolltofixed.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
* ScrollToFixed
* https://github.com/bigspotteddog/ScrollToFixed
*
* Copyright (c) 2011 Joseph Cava-Lynch
* MIT license
*/
(function($) {
$.isScrollToFixed = function(el) {
return !!$(el).data('ScrollToFixed');
};
$.ScrollToFixed = function(el, options) {
// To avoid scope issues, use 'base' instead of 'this' to reference this
// class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element.
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object.
base.$el.data('ScrollToFixed', base);
// A flag so we know if the scroll has been reset.
var isReset = false;
// The element that was given to us to fix if scrolled above the top of
// the page.
var target = base.$el;
var position;
var originalPosition;
var originalFloat;
var originalOffsetTop;
var originalZIndex;
// The offset top of the element when resetScroll was called. This is
// used to determine if we have scrolled past the top of the element.
var offsetTop = 0;
// The offset left of the element when resetScroll was called. This is
// used to move the element left or right relative to the horizontal
// scroll.
var offsetLeft = 0;
var originalOffsetLeft = -1;
// This last offset used to move the element horizontally. This is used
// to determine if we need to move the element because we would not want
// to do that for no reason.
var lastOffsetLeft = -1;
// This is the element used to fill the void left by the target element
// when it goes fixed; otherwise, everything below it moves up the page.
var spacer = null;
var spacerClass;
var className;
// Capture the original offsets for the target element. This needs to be
// called whenever the page size changes or when the page is first
// scrolled. For some reason, calling this before the page is first
// scrolled causes the element to become fixed too late.
function resetScroll() {
// Set the element to it original positioning.
target.trigger('preUnfixed.ScrollToFixed');
setUnfixed();
target.trigger('unfixed.ScrollToFixed');
// Reset the last offset used to determine if the page has moved
// horizontally.
lastOffsetLeft = -1;
// Capture the offset top of the target element.
offsetTop = target.offset().top;
// Capture the offset left of the target element.
offsetLeft = target.offset().left;
// If the offsets option is on, alter the left offset.
if (base.options.offsets) {
offsetLeft += (target.offset().left - target.position().left);
}
if (originalOffsetLeft == -1) {
originalOffsetLeft = offsetLeft;
}
position = target.css('position');
// Set that this has been called at least once.
isReset = true;
if (base.options.bottom != -1) {
target.trigger('preFixed.ScrollToFixed');
setFixed();
target.trigger('fixed.ScrollToFixed');
}
}
function getLimit() {
var limit = base.options.limit;
if (!limit) return 0;
if (typeof(limit) === 'function') {
return limit.apply(target);
}
return limit;
}
// Returns whether the target element is fixed or not.
function isFixed() {
return position === 'fixed';
}
// Returns whether the target element is absolute or not.
function isAbsolute() {
return position === 'absolute';
}
function isUnfixed() {
return !(isFixed() || isAbsolute());
}
// Sets the target element to fixed. Also, sets the spacer to fill the
// void left by the target element.
function setFixed() {
// Only fix the target element and the spacer if we need to.
if (!isFixed()) {
//get REAL dimensions (decimal fix)
//Ref. http://stackoverflow.com/questions/3603065/how-to-make-jquery-to-not-round-value-returned-by-width
var dimensions = target[0].getBoundingClientRect();
// Set the spacer to fill the height and width of the target
// element, then display it.
spacer.css({
'display' : target.css('display'),
'width' : dimensions.width,
'height' : dimensions.height,
'float' : target.css('float')
});
// Set the target element to fixed and set its width so it does
// not fill the rest of the page horizontally. Also, set its top
// to the margin top specified in the options.
cssOptions={
'z-index' : base.options.zIndex,
'position' : 'fixed',
'top' : base.options.bottom == -1?getMarginTop():'',
'bottom' : base.options.bottom == -1?'':base.options.bottom,
'margin-left' : '0px'
}
if (!base.options.dontSetWidth){ cssOptions['width']=target.css('width'); };
target.css(cssOptions);
target.addClass(base.options.baseClassName);
if (base.options.className) {
target.addClass(base.options.className);
}
position = 'fixed';
}
}
function setAbsolute() {
var top = getLimit();
var left = offsetLeft;
if (base.options.removeOffsets) {
left = '';
top = top - offsetTop;
}
cssOptions={
'position' : 'absolute',
'top' : top,
'left' : left,
'margin-left' : '0px',
'bottom' : ''
}
if (!base.options.dontSetWidth){ cssOptions['width']=target.css('width'); };
target.css(cssOptions);
position = 'absolute';
}
// Sets the target element back to unfixed. Also, hides the spacer.
function setUnfixed() {
// Only unfix the target element and the spacer if we need to.
if (!isUnfixed()) {
lastOffsetLeft = -1;
// Hide the spacer now that the target element will fill the
// space.
spacer.css('display', 'none');
// Remove the style attributes that were added to the target.
// This will reverse the target back to the its original style.
target.css({
'z-index' : originalZIndex,
'width' : '',
'position' : originalPosition,
'left' : '',
'top' : originalOffsetTop,
'margin-left' : ''
});
target.removeClass('scroll-to-fixed-fixed');
if (base.options.className) {
target.removeClass(base.options.className);
}
position = null;
}
}
// Moves the target element left or right relative to the horizontal
// scroll position.
function setLeft(x) {
// Only if the scroll is not what it was last time we did this.
if (x != lastOffsetLeft) {
// Move the target element horizontally relative to its original
// horizontal position.
target.css('left', offsetLeft - x);
// Hold the last horizontal position set.
lastOffsetLeft = x;
}
}
function getMarginTop() {
var marginTop = base.options.marginTop;
if (!marginTop) return 0;
if (typeof(marginTop) === 'function') {
return marginTop.apply(target);
}
return marginTop;
}
// Checks to see if we need to do something based on new scroll position
// of the page.
function checkScroll() {
if (!$.isScrollToFixed(target) || target.is(':hidden')) return;
var wasReset = isReset;
var wasUnfixed = isUnfixed();
// If resetScroll has not yet been called, call it. This only
// happens once.
if (!isReset) {
resetScroll();
} else if (isUnfixed()) {
// if the offset has changed since the last scroll,
// we need to get it again.
// Capture the offset top of the target element.
offsetTop = target.offset().top;
// Capture the offset left of the target element.
offsetLeft = target.offset().left;
}
// Grab the current horizontal scroll position.
var x = $(window).scrollLeft();
// Grab the current vertical scroll position.
var y = $(window).scrollTop();
// Get the limit, if there is one.
var limit = getLimit();
// If the vertical scroll position, plus the optional margin, would
// put the target element at the specified limit, set the target
// element to absolute.
if (base.options.minWidth && $(window).width() < base.options.minWidth) {
if (!isUnfixed() || !wasReset) {
postPosition();
target.trigger('preUnfixed.ScrollToFixed');
setUnfixed();
target.trigger('unfixed.ScrollToFixed');
}
} else if (base.options.maxWidth && $(window).width() > base.options.maxWidth) {
if (!isUnfixed() || !wasReset) {
postPosition();
target.trigger('preUnfixed.ScrollToFixed');
setUnfixed();
target.trigger('unfixed.ScrollToFixed');
}
} else if (base.options.bottom == -1) {
// If the vertical scroll position, plus the optional margin, would
// put the target element at the specified limit, set the target
// element to absolute.
if (limit > 0 && y >= limit - getMarginTop()) {
if (!wasUnfixed && (!isAbsolute() || !wasReset)) {
postPosition();
target.trigger('preAbsolute.ScrollToFixed');
setAbsolute();
target.trigger('unfixed.ScrollToFixed');
}
// If the vertical scroll position, plus the optional margin, would
// put the target element above the top of the page, set the target
// element to fixed.
} else if (y >= offsetTop - getMarginTop()) {
if (!isFixed() || !wasReset) {
postPosition();
target.trigger('preFixed.ScrollToFixed');
// Set the target element to fixed.
setFixed();
// Reset the last offset left because we just went fixed.
lastOffsetLeft = -1;
target.trigger('fixed.ScrollToFixed');
}
// If the page has been scrolled horizontally as well, move the
// target element accordingly.
setLeft(x);
} else {
// Set the target element to unfixed, placing it where it was
// before.
if (!isUnfixed() || !wasReset) {
postPosition();
target.trigger('preUnfixed.ScrollToFixed');
setUnfixed();
target.trigger('unfixed.ScrollToFixed');
}
}
} else {
if (limit > 0) {
if (y + $(window).height() - target.outerHeight(true) >= limit - (getMarginTop() || -getBottom())) {
if (isFixed()) {
postPosition();
target.trigger('preUnfixed.ScrollToFixed');
if (originalPosition === 'absolute') {
setAbsolute();
} else {
setUnfixed();
}
target.trigger('unfixed.ScrollToFixed');
}
} else {
if (!isFixed()) {
postPosition();
target.trigger('preFixed.ScrollToFixed');
setFixed();
}
setLeft(x);
target.trigger('fixed.ScrollToFixed');
}
} else {
setLeft(x);
}
}
}
function getBottom() {
if (!base.options.bottom) return 0;
return base.options.bottom;
}
function postPosition() {
var position = target.css('position');
if (position == 'absolute') {
target.trigger('postAbsolute.ScrollToFixed');
} else if (position == 'fixed') {
target.trigger('postFixed.ScrollToFixed');
} else {
target.trigger('postUnfixed.ScrollToFixed');
}
}
var windowResize = function(event) {
// Check if the element is visible before updating it's position, which
// improves behavior with responsive designs where this element is hidden.
if(target.is(':visible')) {
isReset = false;
checkScroll();
} else {
// Ensure the spacer is hidden
setUnfixed();
}
}
var windowScroll = function(event) {
(!!window.requestAnimationFrame) ? requestAnimationFrame(checkScroll) : checkScroll();
}
// From: http://kangax.github.com/cft/#IS_POSITION_FIXED_SUPPORTED
var isPositionFixedSupported = function() {
var container = document.body;
if (document.createElement && container && container.appendChild && container.removeChild) {
var el = document.createElement('div');
if (!el.getBoundingClientRect) return null;
el.innerHTML = 'x';
el.style.cssText = 'position:fixed;top:100px;';
container.appendChild(el);
var originalHeight = container.style.height,
originalScrollTop = container.scrollTop;
container.style.height = '3000px';
container.scrollTop = 500;
var elementTop = el.getBoundingClientRect().top;
container.style.height = originalHeight;
var isSupported = (elementTop === 100);
container.removeChild(el);
container.scrollTop = originalScrollTop;
return isSupported;
}
return null;
}
var preventDefault = function(e) {
e = e || window.event;
if (e.preventDefault) {
e.preventDefault();
}
e.returnValue = false;
}
// Initializes this plugin. Captures the options passed in, turns this
// off for devices that do not support fixed position, adds the spacer,
// and binds to the window scroll and resize events.
base.init = function() {
// Capture the options for this plugin.
base.options = $.extend({}, $.ScrollToFixed.defaultOptions, options);
originalZIndex = target.css('z-index')
// Turn off this functionality for devices that do not support it.
// if (!(base.options && base.options.dontCheckForPositionFixedSupport)) {
// var fixedSupported = isPositionFixedSupported();
// if (!fixedSupported) return;
// }
// Put the target element on top of everything that could be below
// it. This reduces flicker when the target element is transitioning
// to fixed.
base.$el.css('z-index', base.options.zIndex);
// Create a spacer element to fill the void left by the target
// element when it goes fixed.
spacer = $('<div />');
position = target.css('position');
originalPosition = target.css('position');
originalFloat = target.css('float');
originalOffsetTop = target.css('top');
// Place the spacer right after the target element.
if (isUnfixed()) base.$el.after(spacer);
// Reset the target element offsets when the window is resized, then
// check to see if we need to fix or unfix the target element.
$(window).bind('resize.ScrollToFixed', windowResize);
// When the window scrolls, check to see if we need to fix or unfix
// the target element.
$(window).bind('scroll.ScrollToFixed', windowScroll);
// For touch devices, call checkScroll directlly rather than
// rAF wrapped windowScroll to animate the element
if ('ontouchmove' in window) {
$(window).bind('touchmove.ScrollToFixed', checkScroll);
}
if (base.options.preFixed) {
target.bind('preFixed.ScrollToFixed', base.options.preFixed);
}
if (base.options.postFixed) {
target.bind('postFixed.ScrollToFixed', base.options.postFixed);
}
if (base.options.preUnfixed) {
target.bind('preUnfixed.ScrollToFixed', base.options.preUnfixed);
}
if (base.options.postUnfixed) {
target.bind('postUnfixed.ScrollToFixed', base.options.postUnfixed);
}
if (base.options.preAbsolute) {
target.bind('preAbsolute.ScrollToFixed', base.options.preAbsolute);
}
if (base.options.postAbsolute) {
target.bind('postAbsolute.ScrollToFixed', base.options.postAbsolute);
}
if (base.options.fixed) {
target.bind('fixed.ScrollToFixed', base.options.fixed);
}
if (base.options.unfixed) {
target.bind('unfixed.ScrollToFixed', base.options.unfixed);
}
if (base.options.spacerClass) {
spacer.addClass(base.options.spacerClass);
}
target.bind('resize.ScrollToFixed', function() {
spacer.height(target.height());
});
target.bind('scroll.ScrollToFixed', function() {
target.trigger('preUnfixed.ScrollToFixed');
setUnfixed();
target.trigger('unfixed.ScrollToFixed');
checkScroll();
});
target.bind('detach.ScrollToFixed', function(ev) {
preventDefault(ev);
target.trigger('preUnfixed.ScrollToFixed');
setUnfixed();
target.trigger('unfixed.ScrollToFixed');
$(window).unbind('resize.ScrollToFixed', windowResize);
$(window).unbind('scroll.ScrollToFixed', windowScroll);
target.unbind('.ScrollToFixed');
//remove spacer from dom
spacer.remove();
base.$el.removeData('ScrollToFixed');
});
// Reset everything.
windowResize();
};
// Initialize the plugin.
base.init();
};
// Sets the option defaults.
$.ScrollToFixed.defaultOptions = {
marginTop : 0,
limit : 0,
bottom : -1,
zIndex : 1000,
baseClassName: 'scroll-to-fixed-fixed'
};
// Returns enhanced elements that will fix to the top of the page when the
// page is scrolled.
$.fn.scrollToFixed = function(options) {
return this.each(function() {
(new $.ScrollToFixed(this, options));
});
};
})(jQuery);