-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjquery.sticky-like-facebook.js
More file actions
208 lines (175 loc) · 7.88 KB
/
jquery.sticky-like-facebook.js
File metadata and controls
208 lines (175 loc) · 7.88 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
201
202
203
204
205
206
207
208
// Sticky like Facebook Plugin v0.0.1 for jQuery
// =============
// Author: Paul Weber
// Forked From: Sticky.js by Anthony Garand
// Improvements by German M. Bravo (Kronuz) and Ruud Kamphuis (ruudk)
// Improvements by Leonardo C. Daronco (daronco)
// Created: 03.09.2013
// Date: 2/12/2012
// Website: http://labs.anthonygarand.com/sticky
// Description: Makes a sidebar smaller than the main content always stay on the screen -
// like facebook does it!
(function ($) {
var defaults = {
topSpacing:0,
bottomSpacing:0,
className:'is-sticky',
wrapperClassName:'sticky-wrapper',
center:false,
getWidthFrom:'',
debug: true
},
$window = $(window),
$document = $(document),
sticked = [],
windowHeight = $window.height(),
stickyElementPlacer = function() {
var scrollTop = $window.scrollTop();
// position wrapper
s.stickyWrapper.css('height', ($document.height() - s.topSpacing - s.bottomSpacing - 100) + 'px');
s.stickyElement.css('width', s.originalWidth);
var wrapperTop = s.stickyWrapper.offset().top,
wrapperHeight = s.stickyWrapper.height(),
scrolling = (s.lastScrollTop < scrollTop) ? 'down' : 'up';
var attributes = {'position':'relative'};
var headerHeight = 90;
var bottomLineOfHeader = scrollTop + headerHeight;
s.lastScroll = null;
if (scrolling == 'up') {
if(s.stickyElement.css('position') === 'fixed' && s.stickyElement.offset().top > s.topSpacing && s.lastScroll == "up") {
attributes = {
'position':'fixed',
'top':"110px", bottom : 'auto'
}
} else {
// check if we are over upper border
if (bottomLineOfHeader < s.stickyElement.offset().top) {
if (bottomLineOfHeader > s.topSpacing) {
var newTop = (bottomLineOfHeader - s.topSpacing );
// check if we are going over lower border
if (newTop + s.stickyElement.height() < s.stickyWrapper.height()) {
attributes = {
'position':'fixed',
'top':"110px", bottom : 'auto'
}
}
} else {
attributes = $.extend(attributes, {
'top':0 + 'px',
'bottom':'auto'
});
}
} else {
// keep current position as relative
attributes = $.extend(attributes, {
'top': s.stickyElement.offset().top - s.stickyWrapper.offset().top,
'bottom':'auto'
});
}
}
} else if (scrolling == 'down') {
// check if we are going over lower border
// leave unchanged until it goes over bottom of screen.
var lowerElementBorder = s.stickyElement.offset().top + s.stickyElement.height();
var overLowerBorder = lowerElementBorder < wrapperHeight + wrapperTop;
if (lowerElementBorder < scrollPositionBottom && overLowerBorder) {
//console.log('distanceToBottom', $document.height() - scrollPositionBottom);
if($document.height() - scrollPositionBottom > s.bottomSpacing) {
attributes = {
'position' : 'fixed',
'top' : 'auto',
'bottom' : '10px'
}
} else {
// calculate new top from distance from browser window border
var absolutePosition = scrollPositionBottom - 10;
var relativePosition = absolutePosition - s.stickyElement.height() - s.stickyWrapper.offset().top;
attributes = $.extend(attributes, {
'top':relativePosition,
'bottom':'auto'
});
}
} else {
// keep current position as relative
attributes = $.extend(attributes, {
'top': s.stickyElement.offset().top - s.stickyWrapper.offset().top,
'bottom':'auto'
});
}
}
s.lastScroll = scrolling;
s.stickyElement.css(attributes);
s.lastScrollTop = scrollTop;
},
scroller = function () {
for (var i = 0; i < sticked.length; i++) {
var s = sticked[i];
this.stickyElementPlacer(s);
}
},
resizer = function () {
windowHeight = $window.height();
},
methods = {
stick:{
},
unstick:{
},
init:function (options) {
var o = $.extend(defaults, options);
return this.each(function () {
var stickyElement = $(this);
var stickyId = stickyElement.attr('id');
var wrapper = $('<div></div>')
.attr('id', stickyId + '-sticky-wrapper')
.addClass(o.wrapperClassName);
stickyElement.wrapAll(wrapper);
if (o.center) {
stickyElement.parent().css({width:stickyElement.outerWidth(), marginLeft:"auto", marginRight:"auto"});
}
if (stickyElement.css("float") == "right") {
stickyElement.css({"float":"none"}).parent().css({"float":"right"});
}
var stickyWrapper = stickyElement.parent();
stickyWrapper.css({
position:"absolute",
top:o.topSpacing,
height:$document.height(),
left:stickyElement.position().left,
right:$document.width() - (stickyElement.offset().left + stickyElement.width())
});
sticked.push({
topSpacing:o.topSpacing,
bottomSpacing:o.bottomSpacing,
stickyElement:stickyElement,
currentTop:null,
stickyWrapper:stickyWrapper,
className:o.className,
getWidthFrom:o.getWidthFrom,
originalWidth: stickyElement.width()
});
});
},
update:scroller
};
// should be more efficient than using $window.scroll(scroller) and $window.resize(resizer):
if (window.addEventListener) {
window.addEventListener('scroll', scroller, false);
window.addEventListener('resize', resizer, false);
} else if (window.attachEvent) {
window.attachEvent('onscroll', scroller);
window.attachEvent('onresize', resizer);
}
$.fn.stickyLikeFacebook = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.stickyLikeFacebook');
}
};
$(function () {
setTimeout(scroller, 0);
});
})(jQuery);