forked from rickharris/StickyScroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.stickyscroll.js
163 lines (135 loc) · 4.43 KB
/
jquery.stickyscroll.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
/**
* StickyScroll
* written by Rick Harris - @iamrickharris
*
* Requires jQuery 1.4+
*
* Make elements stick to the top of your page as you scroll
*
* See README for details
*
*/
(function($) {
$.fn.stickyScroll = function(options) {
var methods = {
init : function(options) {
var settings;
if (options.mode !== 'auto' && options.mode !== 'manual') {
if (options.container) {
options.mode = 'auto';
}
if (options.bottomBoundary) {
options.mode = 'manual';
}
}
settings = $.extend({
mode: 'auto', // 'auto' or 'manual'
container: $('body'),
topBoundary: null,
bottomBoundary: null
}, options);
function bottomBoundary() {
return $(document).height() - settings.container.offset().top
- settings.container.attr('offsetHeight');
}
function topBoundary() {
return settings.container.offset().top
}
function elHeight(el) {
return $(el).attr('offsetHeight');
}
// make sure user input is a jQuery object
settings.container = $(settings.container);
if(!settings.container.length) {
if(console) {
console.log('StickyScroll: the element ' + options.container +
' does not exist, we\'re throwing in the towel');
}
return;
}
// calculate automatic bottomBoundary
if(settings.mode === 'auto') {
settings.topBoundary = topBoundary();
settings.bottomBoundary = bottomBoundary();
}
return this.each(function(index) {
var el = $(this),
win = $(window),
id = Date.now() + index,
height = elHeight(el);
el.data('sticky-id', id);
win.bind('scroll.stickyscroll-' + id, function() {
var top = $(document).scrollTop(),
bottom = $(document).height() - top - height;
if(bottom <= settings.bottomBoundary) {
el.offset({
top: $(document).height() - settings.bottomBoundary - height
})
.removeClass('sticky-active')
.removeClass('sticky-inactive')
.addClass('sticky-stopped');
}
else if(top > settings.topBoundary) {
el.offset({
top: $(window).scrollTop()
})
.removeClass('sticky-stopped')
.removeClass('sticky-inactive')
.addClass('sticky-active');
}
else if(top < settings.topBoundary) {
el.css({
position: '',
top: '',
bottom: ''
})
.removeClass('sticky-stopped')
.removeClass('sticky-active')
.addClass('sticky-inactive');
}
});
win.bind('resize.stickyscroll-' + id, function() {
if (settings.mode === 'auto') {
settings.topBoundary = topBoundary();
settings.bottomBoundary = bottomBoundary();
}
height = elHeight(el);
$(this).scroll();
})
el.addClass('sticky-processed');
// start it off
win.scroll();
});
},
reset : function() {
return this.each(function() {
var el = $(this),
id = el.data('sticky-id');
el.css({
position: '',
top: '',
bottom: ''
})
.removeClass('sticky-stopped')
.removeClass('sticky-active')
.removeClass('sticky-inactive')
.removeClass('sticky-processed');
$(window).unbind('.stickyscroll-' + id);
});
}
};
// if options is a valid method, execute it
if (methods[options]) {
return methods[options].apply(this,
Array.prototype.slice.call(arguments, 1));
}
// or, if options is a config object, or no options are passed, init
else if (typeof options === 'object' || !options) {
return methods.init.apply(this, arguments);
}
else if(console) {
console.log('Method' + options +
' does not exist on jQuery.stickyScroll');
}
};
})(jQuery);