forked from AndrewHenderson/jSticky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.jsticky.js
117 lines (98 loc) · 3.38 KB
/
jquery.jsticky.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
/* jSticky Plugin
* =============
* Author: Andrew Henderson (@AndrewHenderson)
* Contributor: Mike Street (@mikestreety)
* Date: 9/7/2012
* Update: 09/20/2016
* Website: http://github.com/andrewhenderson/jsticky/
* Description: A jQuery plugin that keeps select DOM
* element(s) in view while scrolling the page.
*/
;(function($) {
$.fn.sticky = function(options) {
var defaults = {
topSpacing: 0, // No spacing by default
zIndex: '', // No default z-index
stopper: '.sticky-stopper', // Default stopper class, also accepts number value
stickyClass: false // Class applied to element when it's stuck
};
var settings = $.extend({}, defaults, options); // Accepts custom stopper id or class
// Checks if custom z-index was defined
function checkIndex() {
if (typeof settings.zIndex == 'number') {
return true;
} else {
return false;
}
}
var hasIndex = checkIndex(); // True or false
// Checks if a stopper exists in the DOM or number defined
function checkStopper() {
if (0 < $(settings.stopper).length || typeof settings.stopper === 'number') {
return true;
} else {
return false;
}
}
var hasStopper = checkStopper(); // True or false
return this.each(function() {
var $this = $(this);
var thisHeight = $this.outerHeight();
var thisWidth = $this.outerWidth();
var topSpacing = settings.topSpacing;
var zIndex = settings.zIndex;
var pushPoint = $this.offset().top - topSpacing; // Point at which the sticky element starts pushing
var placeholder = $('<div></div>').width(thisWidth).height(thisHeight).addClass('sticky-placeholder'); // Cache a clone sticky element
var stopper = settings.stopper;
var $window = $(window);
function stickyScroll() {
var windowTop = $window.scrollTop(); // Check window's scroll position
var stopPoint = stopper;
var parentWidth = $this.parent().width();
placeholder.width(parentWidth)
if ( hasStopper && typeof stopper === 'string' ) {
var stopperTop = $(stopper).offset().top;
stopPoint = (stopperTop - thisHeight) - topSpacing;
}
if (pushPoint < windowTop) {
// Create a placeholder for sticky element to occupy vertical real estate
if(settings.stickyClass)
$this.addClass(settings.stickyClass);
$this.after(placeholder).css({
position: 'fixed',
top: topSpacing,
width: parentWidth
});
if (hasIndex) {
$this.css({
zIndex: zIndex
});
}
if (hasStopper) {
if (stopPoint < windowTop) {
var diff = (stopPoint - windowTop) + topSpacing;
$this.css({
top: diff
});
}
}
} else {
if(settings.stickyClass)
$this.removeClass(settings.stickyClass);
$this.css({
position: 'static',
top: null,
left: null,
width: 'auto'
});
placeholder.remove();
}
}
if($window.innerHeight() > thisHeight) {
$window.bind('scroll', stickyScroll);
$window.bind('load', stickyScroll);
$window.bind('resize', stickyScroll);
}
});
};
})(jQuery);