-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.stickwith.js
42 lines (38 loc) · 1.66 KB
/
jquery.stickwith.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
/* Stick With takes absolutely positioned elements and changes them to
* fixed position elements before they are about to scroll off the top
* of the page. When the user scrolls back up they become absolutely
* positioned again.
* Options: top - the minimum number of pixels the element should
* stay away from the top of the viewport. */
(function ( $ ) {
$.fn.stickWith = function ( options ) {
var settings = $.extend({
top: 0, // css top property. used when element's position is fixed
}, options);
return this.each(function () {
var $this = $( this );
var $window = $( window );
// number of pixels down the page we should change to fixed position
var swapPos = $this.offset().top - settings.top;
var fixed = false;
var fixedCSS = {position: 'fixed',
top: settings.top,
left: $this.offset().left };
var absoluteCSS = {position: 'absolute',
top: $this.css('top'),
left: $this.css('left'),
right: $this.css('right'),
bottom: $this.css('bottom') };
$window.scroll(function () { //called often, must be efficient
var shouldBeFixed = $window.scrollTop() > swapPos;
if (shouldBeFixed && !fixed) {
fixed = true;
$this.css(fixedCSS);
} else if (!shouldBeFixed && fixed) {
fixed = false;
$this.css(absoluteCSS);
}
})
})
};
})( jQuery );