This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
keepinview.js
253 lines (195 loc) · 8.37 KB
/
keepinview.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
/*! ###########################################################################
Source: https://github.com/dutchcelt/Keep-in-View
Copyright (C) 2011 - 2013, Lunatech Labs B.V., C. Egor Kloos. All rights reserved.
GNU General Public License, version 3 (GPL-3.0)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.opensource.org/licenses/gpl-3.0.html
########################################################################### */
// Uses AMD or browser globals to create a jQuery plugin.
(function( factory ){
if( typeof define === 'function' && define.amd ){
// AMD. Register as an anonymous module.
define( ['jquery'], factory );
} else {
// Browser globals
factory( jQuery );
}
}( function( $ ){
$.fn.keepInView = function( settings ){
return this.each( function( index, stickyElem ){
var $elem;
var $parent;
var defaults = {
// Position will be fixed regardless of scroll position when set to true
fixed: false,
// Vertical offset that applies to both top and bottom;
edgeOffset: 0,
// Override z-index if you can't or don't want to set this with CSS
zindex: $( stickyElem ).css( 'zIndex' ),
// Override all scripted positions with your own custom CSS classname
// The set classname will be triggered when element scrolls out of view
// The Script will add a suffix of '-top' or '-bottom'
customClass: false,
// Only trigger this script on scrolling out at the 'top', 'bottom' the default is 'both'.
trigger: 'both',
// Scrollable box
scrollable: false,
// Set the height and width (user can override these if necessary)
h: $( stickyElem ).height(),
w: $( stickyElem ).width(),
// If a pageload scrolls to a hash you can use this to offset anchors if the 'sticky' element is covering the anchored content
// Beware that if the anchor itself contains content that it will also move up the page.
// This feature is best used with the clone feature below.
offsetAnchor: false, // boolean.
// Clone the sticky element and prepend to its parent.
// Beware that the original item is not removed from the page so make sure that the cloned element will cover it.
// The cloned item can be styled via the classname "KIV-cloned"
cloned: false // boolean.
};
var options = $.extend( {}, defaults, settings );
if( options.cloned ){
$parent = $( stickyElem ).parents().eq( 0 );
$elem = $( stickyElem ).clone().prependTo( $parent ).hide().addClass( "KIV-cloned" );
$( stickyElem ).addClass( "KIV-original" )
} else {
$elem = $( stickyElem );
}
var offset = $( stickyElem ).offset(),
position = $( stickyElem ).css( 'position' ),
leftPosition = $( stickyElem ).css( 'left' ),
marginOffset = ( leftPosition === "auto" ) ? parseInt( $( stickyElem ).css( 'marginLeft' ), 10 ) : 0,
cssObject = (function(){
return {
position: 'fixed',
left: leftPosition - marginOffset + 'px',
width: (options.scrollable) ? options.w - 15 : options.w,
height: (options.scrollable) ? ($( window ).height() - offset.top) + "px" : options.h,
zIndex: options.zindex
}
})(),
prepCSS = function( cssSettings ){
$elem.css( $.extend( {}, cssObject, cssSettings ) );
},
fixCSS = function( t ){
$elem.css( { top: t + 'px' } );
if( options.offsetAnchor ){
$( stickyElem ).css( { visibility: "hidden" } );
$elem.slideDown( "normal" );
}
}
if( options.offsetAnchor ){
// It is possible that there a lot of anchors!
// Using an array instead of a loop by 'shifting' the array
// This speeds up the iterations and setTimeout prevents the browser from locking up.
// put all the dom elements collected by jQuery in to an array
var $anchors = $( "a[name]" );
var anchorArray = $.makeArray( $anchors );
var arrayShifter = function(){
var start = +new Date();
do {
var anchor = anchorArray.shift();
// Invoke lazyLoad method with the current item
if( anchorArray[0] !== void 0 ){
$( anchor ).css( { position: "relative", display: "block", top: "-" + $elem.outerHeight() + "px" } );
}
} while( anchorArray[0] !== void 0 && (+new Date() - start < 50) ); // increase to 100ms if needed.
if( anchorArray[0] !== void 0 ){
setTimeout( arrayShifter, 0 );
}
};
arrayShifter();
}
var setElem = function( opts ){
// Making sure that $elem doesn't fire if it is taller than the window (like a sidebar)
// To prevent elastic scrolling fireing set the body in css to 'overflow: hidden'.
// Then wrap your content in a div with 'overflow: auto'.
if( $elem.height() > $( window ).height() && !options.scrollable ){
return false;
}
if( options.clearStyle ){
$elem.removeAttr( "style" );
}
var scrolledOutAt = "";
var windowHeight = $( window ).height();
var outerHeight = $elem.outerHeight();
if( windowHeight < parseInt( offset.top + outerHeight - Math.abs( $( window ).scrollTop() ) + options.edgeOffset, 10 ) && !options.fixed ){
scrolledOutAt = "bottom";
}
if( ($( window ).scrollTop()) > offset.top - options.edgeOffset && !options.fixed ){
scrolledOutAt = "top";
}
if( !options.customClass ){
if( options.scrollable ){
prepCSS( {height: (windowHeight - offset.top) + "px", overflow: "auto"} );
} else {
prepCSS();
}
if( scrolledOutAt === "bottom" && (options.trigger === 'both' || options.trigger === 'bottom') ){
if( options.scrollable ){
prepCSS( {height: windowHeight + "px", top: (windowHeight - outerHeight - options.edgeOffset) + "px", overflow: "auto" } );
} else {
fixCSS( (windowHeight - outerHeight - options.edgeOffset) );
}
} else if( scrolledOutAt === "top" && (options.trigger === 'both' || options.trigger === 'top') ){
if( options.scrollable ){
prepCSS( { height: windowHeight + "px", top: options.edgeOffset + "px", overflow: "auto" } );
} else {
fixCSS( options.edgeOffset );
}
} else if( options.fixed ){
$elem.css( {top: options.edgeOffset, left: offset.left, height: "auto"} );
} else {
if( options.scrollable ){
$elem.css( {position: position, top: offset.top + "px", height: (windowHeight - offset.top + $( window ).scrollTop()) + "px"} );
} else {
if( options.offsetAnchor ){
$( stickyElem ).css( { visibility: "visible" } );
$elem.hide();
} else {
$elem.removeAttr( 'style' );
}
}
}
} else if( options.customClass ){
if( options.trigger === 'both' ){
if( scrolledOutAt === "bottom" || scrolledOutAt === "top" ){
$elem.addClass( options.customClass + "-" + scrolledOutAt );
} else if( !scrolledOutAt ){
$elem.removeClass( options.customClass + "-top" ).removeClass( options.customClass + "-bottom" );
}
} else if( scrolledOutAt === options.trigger ){
$elem.addClass( options.customClass + "-" + options.trigger );
} else if( !scrolledOutAt ){
$elem.removeClass( options.customClass + "-" + options.trigger );
}
}
}
var staySticky = function(){
options.w = $elem.width();
options.h = $elem.height();
offset = $elem.offset();
options.clearStyle = true;
requestAnimationFrame( setElem );
}
var setElemRequest = function(){
options.clearStyle = false;
requestAnimationFrame( setElem );
}
var killSticky = function(){
$elem.removeAttr( 'style' ).off( ".sticky" );
$( window ).off( '.sticky', staySticky ).off( '.sticky', setElemRequest );
}
$elem.on( 'update.sticky', staySticky );
$elem.on( 'unstick.sticky', killSticky );
$( window ).on( 'resize.sticky', $elem, staySticky ).on( 'scroll.sticky', $elem, setElemRequest ).trigger( 'scroll' );
} );
};
} ));