forked from mojotech/stickymojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stickyMojo.js
136 lines (120 loc) · 3.73 KB
/
stickyMojo.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
(function($) {
$.fn.extend({
stickyMojo: function(options) {
var settings = $.extend({
'footerID': '',
'contentID': '',
'orientation': $(this).css('float')
}, options);
var sticky = {
'el': $(this),
'stickyLeft': $(settings.contentID).outerWidth() + $(settings.contentID).offset.left,
'stickyTop2': $(this).offset().top,
'stickyHeight': $(this).outerHeight(true),
'contentHeight': $(settings.contentID).outerHeight(true),
'win': $(window),
'breakPoint': $(this).outerWidth(true) + $(settings.contentID).outerWidth(true),
'marg': parseInt($(this).css('margin-top'), 10)
};
var errors = checkSettings();
cacheElements();
return this.each(function() {
buildSticky();
});
function buildSticky() {
if (!errors.length) {
sticky.el.css('left', sticky.stickyLeft);
sticky.win.bind({
'scroll': stick,
'resize': function() {
sticky.el.css('left', sticky.stickyLeft);
stick();
}
});
} else {
if (console && console.warn) {
console.warn(errors);
} else {
alert(errors);
}
}
}
// Caches the footer and content elements into jquery objects
function cacheElements() {
settings.footerID = $(settings.footerID);
settings.contentID = $(settings.contentID);
}
// Calcualtes the limits top and bottom limits for the sidebar
function calculateLimits() {
return {
limit: settings.footerID.offset().top - sticky.stickyHeight,
windowTop: sticky.win.scrollTop(),
stickyTop: sticky.stickyTop2 - sticky.marg
}
}
// Sets sidebar to fixed position
function setFixedSidebar() {
sticky.el.css({
position: 'fixed',
top: 0
});
}
// Determines the sidebar orientation and sets margins accordingly
function checkOrientation() {
if (settings.orientation === "left") {
settings.contentID.css('margin-left', sticky.el.outerWidth(true));
} else {
sticky.el.css('margin-left', settings.contentID.outerWidth(true));
}
}
// sets sidebar to a static positioned element
function setStaticSidebar() {
sticky.el.css({
'position': 'static',
'margin-left': '0px'
});
settings.contentID.css('margin-left', '0px');
}
// initiated to stop the sidebar from intersecting the footer
function setLimitedSidebar(diff) {
sticky.el.css({
top: diff
});
}
//determines whether sidebar should stick and applies appropriate settings to make it stick
function stick() {
var tops = calculateLimits();
var hitBreakPoint = tops.stickyTop < tops.windowTop && (sticky.win.width() >= sticky.breakPoint);
if (hitBreakPoint) {
setFixedSidebar();
checkOrientation();
} else {
setStaticSidebar();
}
if (tops.limit < tops.windowTop) {
var diff = tops.limit - tops.windowTop;
setLimitedSidebar(diff);
}
}
// verifies that all settings are correct
function checkSettings() {
var errors = [];
for (var key in settings) {
if (!settings[key]) {
errors.push(settings[key]);
}
}
ieVersion() && errors.push("NO IE 7");
return errors;
}
function ieVersion() {
if(document.querySelector) {
return false;
}
else {
return true;
}
}
}
});
})(jQuery);