-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.sticky.js
129 lines (96 loc) · 2.85 KB
/
jquery.sticky.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
(function ($) {
"use strict";
/* Sticky Definition */
var Sticky = function(element, options) {
this.options = options;
this.$element = $(element);
this.$parent = this.$element.parent();
this.$placeholder = $("<div class='sticky-placeholder'/>");
this.init();
};
Sticky.prototype = {
init: function() {
// Create a placeholder to hold the shape of the element.
// Note: Using `outerHeight()` only works as long as there aren't
// any margins on the element and the elements immediate siblings
// or we get have with collapsing margins.
this.$placeholder
.css('height', this.$element.outerHeight())
.insertAfter(this.$element).hide();
this.staticTop = this.$element.position().top;
this.stickyTop = this.staticTop;
this.isSticking = false;
this.$parent.on('scroll.sticky', this.draw.bind(this));
},
destroy: function() {
this.$parent.off('scroll.sticky');
this.positionStatic();
this.$element.removeData('sticky');
},
positionStatic: function() {
if(!this.isSticking)
return;
this.isSticking = false;
this.$placeholder.hide();
this.$element
.css('position', 'static')
.removeClass('is-sticking');
},
positionSticky: function() {
if(this.isSticking)
return;
this.isSticking = true;
this.$placeholder.show();
this.$element
.css('position', 'absolute')
.addClass('is-sticking');
},
stickTo: function(left, top) {
this.$element
.css({
"top": "0px",
"-webkit-transform": "translateY(" + top + "px)"
});
this.positionSticky();
},
draw: function() {
var scrollTop = this.$parent.scrollTop();
if(this.options.autohide && scrollTop >= this.staticTop) {
// The stickyTop is the fixed position of $element. This should
// be between the scrollTop of the $parent and the scrollTop of
// the parent less the height of the element.
this.stickyTop = Math.min(Math.max(this.stickyTop, scrollTop-this.$element.outerHeight()), scrollTop);
if(this.stickyTop == this.staticTop)
this.positionStatic()
else
this.stickTo(0, this.stickyTop);
}
else if(scrollTop > this.staticTop)
this.stickTo(0, scrollTop);
else
this.positionStatic();
},
show: function() {
},
hide: function() {
}
};
/* jQuery PLUGIN */
$.fn.sticky = function (option) {
return this.each(function () {
var $this = $(this),
data = $this.data('sticky'),
options = $.extend({}, $.fn.sticky.defaults, typeof option == 'object' && option);
if(!data)
$this.data('sticky', (data = new Sticky(this, options)));
if(typeof option == 'string')
data[option]();
});
};
$.fn.sticky.defaults = {
autohide: false,
// Roadmap: Stick to 'top', 'bottom', 'left', 'right', 'vertically', or 'horizontally'
// to create sticky headers, footers and sidebars.
to: 'top'
};
}(jQuery));