-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponsive-sidebar.js
More file actions
300 lines (299 loc) · 8.66 KB
/
responsive-sidebar.js
File metadata and controls
300 lines (299 loc) · 8.66 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
(function(global) {
var objects = 0; // counts number of created objects to create unique @keyframes rules for CSS with the properly width
function Sidebar(sidebar, buttons, options) {
objects = objects + 1; // adds 1 for every created object
var id = objects; // unique id for this object
// Assign variables
var sidebar = document.querySelectorAll(sidebar)[0];
var buttons = document.querySelectorAll(buttons);
var buttons = buttons;
var isOpen = false; // Declares state of sidebar
var time = 1000;
var displayStyle = "block";
var width = sidebar.offsetWidth + "px";
var overlayStyles = {};
var animationTimingFunction = "ease-in";
if (options !== undefined) {
if (options.time !== undefined) {
time = options.time;
}
if (options.width !== undefined) {
width = options.width;
}
if (options.overlayStyles !== undefined) {
overlayStyles = options.overlayStyles;
}
if (options.displayStyle !== undefined) {
displayStyle = options.displayStyle;
}
if (options.animationTimingFunction !== undefined) {
animationTimingFunction = options.animationTimingFunction;
}
}
// Creates overlay
var bodyHeight =
document.getElementsByTagName("body")[0].offsetHeight + "px";
var bodyWidth = document.getElementsByTagName("body")[0].offsetWidth + "px";
var overlayParent = document.createElement("div");
overlayParent.setAttribute("class", "overlay");
addStyles(overlayParent, overlayStyles); // Sets customized styles
// Sets absolute necessary styles to make sure everything works well
addStyles(overlayParent, {
display: "none",
position: "absolute",
top: "0px",
left: "0px",
height: bodyHeight,
width: bodyWidth
});
// Check if animations are supported in the calling browser to provide a fallback if not
var animation = false,
animationstring = "animation",
browserPrefix = "",
domPrefixes = "Webkit Moz O ms Khtml".split(" "),
pfx = "";
if (sidebar.style.animationName !== undefined) {
animation = true;
}
if (animation === false) {
for (var i = 0; i < domPrefixes.length; i++) {
if (sidebar.style[domPrefixes[i] + "AnimationName"] !== undefined) {
pfx = domPrefixes[i];
animationstring = pfx + "Animation";
browserPrefix = "-" + pfx.toLowerCase() + "-";
animation = true;
break;
}
}
}
// Adds @Keyframes rules to styles
var openSidebarKeyframes =
"@" +
browserPrefix +
"keyframes openSidebar" +
id +
" { " +
"from {" +
browserPrefix +
"transform:translateX( 0px ) }" +
"to {" +
browserPrefix +
"transform:translateX( " +
width +
" ) }" +
"}";
var closeSidebarKeyframes =
"@" +
browserPrefix +
"keyframes closeSidebar" +
id +
" { " +
"from {" +
browserPrefix +
"transform:translateX( 0px ) }" +
"to {" +
browserPrefix +
"transform:translateX( " +
width +
" ) }" +
"}";
var opacity = 0;
if (overlayStyles.opacity !== undefined) {
opacity = overlayStyles.opacity;
overlayParent.style[browserPrefix + "filter"] =
"progid:DXImageTransform.Microsoft.Alpha(Opacity=" +
opacity * 100 +
")"; // Support for IE 8
}
var fadeInKeyframes =
"@" +
browserPrefix +
"keyframes fadeInOverlay" +
id +
" { " +
"from {" +
browserPrefix +
"opacity: 0 }" +
"to {" +
browserPrefix +
"opacity: " +
opacity +
"}" +
"}";
var fadeOutKeyframes =
"@" +
browserPrefix +
"keyframes fadeOutOverlay" +
id +
" { " +
"from {" +
browserPrefix +
"opacity: 0 }" +
"to {" +
browserPrefix +
"opacity: " +
opacity +
" }" +
"}";
if (animation) {
var s = document.createElement("style");
s.innerHTML =
openSidebarKeyframes +
" " +
closeSidebarKeyframes +
" " +
fadeInKeyframes +
" " +
fadeOutKeyframes;
document.getElementsByTagName("head")[0].appendChild(s);
}
// Adds overlay
var body = document.getElementsByTagName("body")[0];
var overlay = body.appendChild(overlayParent);
// Method to close the sidebar
var closeSidebar = function() {
if (isOpen) {
// if sidebar is already closed then don't close it again
isOpen = false;
if (animation) {
// if animations are supported run @Keyframes
overlay.style[animationstring] =
"fadeOutOverlay" +
id +
" " +
time +
"ms " +
animationTimingFunction +
" 1 reverse both";
body.style[animationstring] =
"closeSidebar" +
id +
" " +
time +
"ms " +
animationTimingFunction +
" 1 reverse both";
setTimeout(function() {
overlay.style.display = "none";
addStyles(sidebar, {
marginLeft: "",
position: "",
top: "",
left: "",
width: "",
height: "",
display: ""
});
}, time);
} else {
overlay.style.display = "none";
sidebar.style.marginLeft = "";
body.style.marginLeft = "";
addStyles(sidebar, {
marginLeft: "",
position: "",
top: "",
left: "",
width: "",
height: "",
display: ""
});
}
}
};
var openSidebar = function() {
if (!isOpen) {
isOpen = true;
if (animation) {
overlay.style[animationstring] =
"fadeInOverlay" +
id +
" " +
time +
"ms " +
animationTimingFunction +
" 1 forwards";
sidebar.style.marginLeft = "-" + width;
body.style[animationstring] =
"openSidebar" +
id +
" " +
time +
"ms " +
animationTimingFunction +
" 1 forwards";
overlay.style.display = "block";
} else {
sidebar.style.marginLeft = "0px";
body.style.marginLeft = width;
overlay.style.marginLeft = width;
}
addStyles(sidebar, {
position: "absolute",
top: "0px",
left: "0px",
width: width,
height: bodyHeight
});
sidebar.style.display = displayStyle;
overlay.style.display = "block";
}
};
// Assign click listener to overlay that closes the sidebar
addEventListener(overlay, "click", closeSidebar);
var disposeListeners = function() {
removeEventListener(overlay, "click", closeSidebar);
forEach(buttons, function(item, i) {
removeEventListener(item, "click", toggleSidebar);
});
};
var toggleSidebar = function() {
if (isOpen) {
closeSidebar();
} else {
openSidebar();
}
};
// Sets listener that closes and opens sidebar for every button
forEach(buttons, function(item, i) {
addEventListener(item, "click", toggleSidebar);
});
// Add class to identify sidebars
addClass(sidebar, "my-sidebar");
this.close = closeSidebar;
this.open = openSidebar;
this.dispose = disposeListeners;
}
// addClass function
function addClass(element, className) {
if (element.classList) element.classList.add(className);
else element.className += " " + className;
}
// event listener methods that adds support for old browsers
function addEventListener(el, eventName, handler) {
if (el.addEventListener) {
el.addEventListener(eventName, handler);
} else if (el.attachEvent) {
el.attachEvent("on" + eventName, function() {
handler.call(el);
});
}
}
function removeEventListener(el, eventName, handler) {
if (el.removeEventListener) el.removeEventListener(eventName, handler);
else if (el.detachEvent) el.detachEvent("on" + eventName, handler);
}
// forEach loop function to support old browsers
function forEach(array, fn) {
for (i = 0; i < array.length; i++) fn(array[i], i);
}
// function to add styles fast and easy with object literal
function addStyles(element, styles) {
for (var i in styles) {
if (styles.hasOwnProperty(i)) {
element.style[i] = styles[i];
}
}
}
global.Sidebar = Sidebar;
})(window);