-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.fixedscroll.js
More file actions
203 lines (180 loc) · 7.49 KB
/
jquery.fixedscroll.js
File metadata and controls
203 lines (180 loc) · 7.49 KB
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
/**
* jquery.fixedscroll.js
* Copyright (c) 2012 Josep del Rio (http://www.joseprio.com/)
* Licensed under the MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* @author Josep del Rio
*
* @projectDescription jQuery plugin to make areas of the page follow other content while scrolling..
*
* @version 0.1.0
*
* @requires jquery.js (tested with 1.8.2)
*
* @param follow string or jQuery container - string containing a jQuery selector, or jQuery container; will be used as the box to follow.
* default: box's parent
*/
( function( $ ) {
// Flag to ensure we register the scroll and resize events only once, and
// only when needed
var isCapturingEvents = false;
// Current boxes that follow
var boxes = [];
// Cached instances
var jWindow = $( window );
var jDocument = $( document );
function check() {
var viewportHeight = parseInt( jWindow.height() );
var pageScroll = parseInt( jDocument.scrollTop() );
var pageHeight = parseInt( jDocument.height() );
var parentTop = 0;
var parentHeight = 0;
var follow = null;
for (var c=0;c<boxes.length;c++) {
// If we're following the same object, no need to get the data again.
if (follow != boxes[c].follow) {
parentTop = boxes[c].follow.cachedTop;
parentHeight = parseInt(boxes[c].follow.innerHeight() );
follow = boxes[c].follow;
}
checkBox(boxes[c], viewportHeight, pageScroll, pageHeight, parentTop, parentHeight);
}
}
function checkBox(box, viewportHeight, pageScroll, pageHeight, parentTop, parentHeight)
{
var boxHeight = box.innerHeight();
var boxOffsetTop = parseInt( box.offset().top);
var currentTop = parseInt( box.css( 'top' ) ) || 0;
// In case it's currently pinned somewhere
if (box.isFixed) {
if (boxHeight > parentHeight) {
// If our box is larger than followed box, we just want to stick it to the top
box.css('top', "" + (parentTop - box.offsetDiff) + "px");
box.css('bottom', 'auto');
box.css('position', 'absolute');
box.isFixed = false;
} else if (pageScroll < parentTop) {
// If the scroll of the page is smaller than the position of the followed box, we just want to stick it to the top
box.css('top', "" + (parentTop - box.offsetDiff) + "px");
box.css('bottom', 'auto');
box.css('position', 'absolute');
box.isFixed = false;
} else if ((pageScroll + viewportHeight) > (parentTop + parentHeight)) {
// Our scroll will show the bottom of the followed box, so stick the follower to the bottom position
if ((boxHeight > viewportHeight) || (boxHeight > (parentTop + parentHeight - pageScroll))){
box.css('top', "" + (parentTop + parentHeight - boxHeight - box.offsetDiff) + "px");
box.css('bottom', 'auto');
box.css('position', 'absolute');
box.isFixed = false;
}
} else if (boxHeight > viewportHeight){
if ((box.previousScroll - pageScroll) > 0) {
// scrolling up
if (box.get(0).style.bottom != 'auto') {
// If it's currently stuck in the bottom
if ((box.previousScroll + viewportHeight - boxHeight) > pageScroll) {
// If the new target position wouldn't show the full box, stick it fixed to the bottom
box.css('top', 0);
box.css('bottom', 'auto');
} else {
// Revert to absolute positioning to show more of the follower box
box.css('top', "" + (box.previousScroll + viewportHeight - boxHeight) + "px");
box.css('bottom', 'auto');
box.css('position', 'absolute');
box.isFixed = false;
}
}
} else if ((box.previousScroll - pageScroll) < 0) {
// scrolling down
if (box.get(0).style.top != 'auto') {
// If it's currently stuck in the top
if ((box.previousScroll + boxHeight) < (pageScroll + viewportHeight)) {
// If the new position would go past the end of the box, stick it to the bottom
box.css('top', 'auto');
box.css('bottom', 0);
} else {
box.css('top', "" + (box.previousScroll - box.offsetDiff) + "px");
box.css('bottom', 'auto');
box.css('position', 'absolute');
box.isFixed = false;
}
}
}
}
} else { // Position is absolute
if ((pageScroll < parentTop)
|| (boxHeight >= parentHeight)) {
box.css('top', "" + (parentTop - box.offsetDiff) + "px");
box.css('bottom', 'auto');
} else if (((pageScroll + viewportHeight) > (parentTop + parentHeight))
&& (boxHeight > viewportHeight)
) {
box.css('top', "" + (parentTop + parentHeight - boxHeight- box.offsetDiff) + "px");
box.css('bottom', 'auto');
} else if ((pageScroll > parentTop)
&& ((boxHeight < viewportHeight) || (boxOffsetTop > pageScroll))
&& (boxHeight < parentHeight)
&& (boxHeight < (parentTop + parentHeight - pageScroll))
) {
box.css('top', 0);
box.css('bottom', 'auto');
box.css('position', 'fixed');
box.isFixed = true;
} else if (((pageScroll + viewportHeight) > (boxOffsetTop + boxHeight))
&& (boxHeight > viewportHeight)
&& (boxHeight < parentHeight)
) {
box.css('top', 'auto');
box.css('bottom', 0);
box.css('position', 'fixed');
box.isFixed = true;
}
}
box.previousScroll = pageScroll;
};
$.fixedScroll = function ( box, options )
{
// Convert box into a jQuery object
box = $( box );
// 'box' is the object to be animated
var position = box.css( 'position' );
// If no follow target was specified, and the immediate parent does not have an ID
if ( options.follow ) {
if (typeof options.follow == 'string') {
box.follow = $( options.follow );
} else {
box.follow = options.follow;
}
box.follow.cachedTop = parseInt( box.follow.offset().top );
} else {
box.follow = box.parent();
}
// Finds the default positioning of the box.
box.initialOffsetTop = parseInt( box.offset().top );
box.initialTop = parseInt( box.css("top") ) || 0;
box.offsetDiff = box.initialOffsetTop - box.initialTop;
if (!isCapturingEvents) {
// Animate the box when the page is scrolled
$( window ).scroll( function () { check(); } );
// Animate the box when the page is resized
$( window ).resize( function () { check(); } );
isCapturingEvents = true;
}
boxes.push(box);
// Perform initial check
var viewportHeight = parseInt( jWindow.height() );
var pageScroll = parseInt( jDocument.scrollTop() );
var pageHeight = parseInt( jDocument.height() );
var parentTop = box.follow.cachedTop;
var parentHeight = parseInt(box.follow.innerHeight() );
checkBox(box, viewportHeight, pageScroll, pageHeight, parentTop, parentHeight);
};
$.fn.fixedScroll = function ( options ) {
options = options || {};
options.follow = options.follow || null;
this.each( function() {
new $.fixedScroll( this, options );
});
return this;
};
})( jQuery );