PluginProbe ʕ •ᴥ•ʔ
My Sticky Bar – Floating Notification Bar & Sticky Header (formerly myStickymenu) / 2.0.4
My Sticky Bar – Floating Notification Bar & Sticky Header (formerly myStickymenu) v2.0.4
2.9.1 2.9.0 2.8.9 2.8.8 trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 2.0 2.0.1 2.0.3 2.0.4 2.0.5 2.0.6 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7
mystickymenu / js / mystickymenu.js
mystickymenu / js Last commit date
iris-script.js 11 years ago mystickymenu.js 8 years ago mystickymenu.min.js 8 years ago
mystickymenu.js
540 lines
1 /*!
2 * myStickymenu by m.r.d.a
3 * v2.0.4
4 */
5
6 (function( $ ) {
7 "use strict";
8
9 $(document).ready(function($){
10
11 // get Sticky Class setting if class name existts
12 if ($(option.mystickyClass) [0]){
13 // Do nothing
14 }
15 else {
16 // Do something if class does not exist and stop
17 console.log("myStickymenu: Entered Sticky Class does not exist, change it in Dashboard / Settings / myStickymenu / Sticky Class. ");
18 return;
19 }
20
21
22 // Get class name
23 var mystickyClass = document.querySelector(option.mystickyClass);
24
25 // get disable at small screen size setting
26 var disableWidth = parseInt(option.disableWidth);
27
28 // get disable at large screen size setting
29 var disableLargeWidth = parseInt(option.disableLargeWidth);
30
31 // get body width
32 //var bodyWidth = parseInt(document.body.clientWidth);
33
34 // get transition effect (slide or fade)
35 var mystickyTransition = option.mystickyTransition;
36
37 // get activaton height setting
38 var activationHeight = parseInt(option.activationHeight);
39
40 // if is admin bar showing, needed for auto calc of activation height when admin bar is showing
41 var adminBar = option.adminBar;
42
43 // disable on scroll down
44 var mysticky_disable_down = option.mysticky_disable_down;
45
46
47
48
49 var viewportWidth;
50
51
52 function calcViewportWidth(e){
53
54 // Calculate actual viewport width
55 var e = window, a = 'inner';
56
57 if (!('innerWidth' in window )) {
58 a = 'client';
59 e = document.documentElement || document.body;
60 }
61 viewportWidth = e[ a+'Width' ];
62
63
64 }
65
66
67 calcViewportWidth();
68
69
70
71
72
73
74
75
76 var parentmysticky = mystickyClass.parentNode;
77
78 var wrappermysticky = document.createElement('div');
79 var position = 0;
80 for(var i = 0; i < parentmysticky.childNodes.length; i++) {
81 if(parentmysticky.childNodes[i] == mystickyClass) {
82 position = i;
83 break;
84 }
85 }
86
87 wrappermysticky.id = 'mysticky-wrap';
88 wrappermysticky.appendChild(mystickyClass);
89 parentmysticky.insertBefore(wrappermysticky, parentmysticky.childNodes[position]);
90
91 var parentnav = mystickyClass.parentNode;
92 var wrappernav = document.createElement('div');
93 wrappernav.id = 'mysticky-nav';
94 parentnav.replaceChild(wrappernav, mystickyClass);
95 wrappernav.appendChild(mystickyClass);
96
97
98
99
100 // get activation height from settings
101 if ( activationHeight == "0" ) {
102 var autoActivate = true;
103 }
104
105
106
107
108
109 var mydivHeight;
110
111
112 function initialDivHeight(){
113
114
115
116 // get initial element height of selected sticky class
117 mydivHeight = (mystickyClass.offsetHeight);
118
119
120
121 // when initial element have margin bottom - awaken example using #masthead class
122
123 if (parseInt($(mystickyClass).css("marginBottom")) > 0) {
124
125 // element have margin bottom, apply it to initial wrap
126 //$(mystickyClass).css("marginBottom").replace('px', '')
127 wrappermysticky.style.marginBottom = ($(mystickyClass).css("marginBottom"));
128 }
129
130
131 if (mydivHeight == "0") {
132 // something is wrong, wrapper cant be zero, if so content will jump while scroll. Awaken theme (for example) with .awaken-navigation-container class selected will use this part. Calculate height based on element children height
133
134 $(mystickyClass).children().filter(':visible').each(function(){
135 mydivHeight = $(this).outerHeight(true);
136
137 });
138
139 }
140
141 if (viewportWidth >= disableWidth) {
142 //wrappermysticky.style.height = mydivHeight + 'px';
143 }
144 }
145
146 initialDivHeight();
147
148
149
150
151 var myfixedHeight;
152
153 function fixedDivHeight(){
154 //if ( autoActivate == true ) {
155
156 // calculate element height while fixed
157
158 mystickyClass.classList.add('myfixed')
159
160 myfixedHeight = $(".myfixed").outerHeight();
161
162 if (myfixedHeight == "0") {
163 // something is wrong, wrapper cant be zero, try to calculate again with div children.
164 $(".myfixed").children().filter(':visible').each(function(){
165 myfixedHeight = $(this).outerHeight(true);
166 });
167 }
168
169 mystickyClass.classList.remove('myfixed');
170
171 //}
172
173 }
174
175 fixedDivHeight();
176
177
178
179
180 var adminBarHeight = 0;
181
182 function calcAdminBarHeight(){
183
184
185
186
187 if ((adminBar == "true" ) && (viewportWidth > 600)) {
188
189 if ($("#wpadminbar")[0]){
190
191 adminBarHeight = $('#wpadminbar').height();
192
193 } else {
194
195 adminBarHeight = 0;
196
197 }
198
199
200
201 } else {
202
203 adminBarHeight = 0;
204 }
205
206
207 //wrappernav.style.top = adminBarHeight + "px";
208
209 if (mystickyTransition == "on") {
210
211 wrappernav.style.top = "-" + myfixedHeight + "px";
212 //wrappernav.style.top = "-" + myfixedHeight + "px";
213
214 } else {
215 wrappernav.style.top = adminBarHeight + "px";
216
217 }
218
219 }
220
221
222
223 calcAdminBarHeight();
224
225
226
227
228
229
230 var mydivWidth;
231
232
233 function initialDivWidth(){
234
235 var rect = $(mystickyClass)[0].getBoundingClientRect();
236 mydivWidth = rect.width;
237
238 //var mydivWidth = ((mystickyClass.offsetWidth) + 'px');
239 //mystickyClass.style.width = mydivWidth + "px";
240
241
242 }
243
244 initialDivWidth();
245
246
247
248
249 var deactivationHeight = activationHeight;
250
251 function calcActivationHeight() {
252
253 // If activate height (Make visible on Scroll) is set to 0, automatic calculation will be used.
254 if ( autoActivate == true ) {
255
256 // Automatic calculation of activation and deactivation height (Make visible on Scroll is set to 0).
257 if (mystickyTransition == "on") {
258 // Slide effect is selected
259 //activationHeight = $(mystickyClass).offset().top + mystickyClass.offsetHeight - adminBarHeight;
260 activationHeight = $(mystickyClass).offset().top + mydivHeight - adminBarHeight;
261 deactivationHeight = $(mystickyClass).offset().top + mydivHeight - adminBarHeight;
262 //deactivationHeight = $(mystickyClass).offset().top - adminBarHeight;
263
264 if (mysticky_disable_down == "on") {
265 deactivationHeight = $(mystickyClass).offset().top - adminBarHeight;
266
267 }
268
269 }
270
271 if (mystickyTransition == "fade") {
272
273 if (mysticky_disable_down == "false") {
274 // Fade effect is selected
275 activationHeight = $(mystickyClass).offset().top - adminBarHeight;
276
277 deactivationHeight = $(mystickyClass).offset().top - adminBarHeight;
278
279 }
280
281 if (mysticky_disable_down == "on") {
282
283 // Fade effect is selected
284 activationHeight = $(mystickyClass).offset().top - adminBarHeight + mydivHeight;
285 deactivationHeight = $(mystickyClass).offset().top - adminBarHeight;
286
287 }
288
289 }
290
291 }
292
293 }
294
295 calcActivationHeight();
296
297
298
299
300
301
302 function headerDeactivateOnHeight() {
303
304
305 if ( autoActivate == true ) {
306
307 if ( mydivHeight > myfixedHeight ){
308 // Auto activate is true, Make visible on Scroll is set to 0, menu is probably header
309
310
311
312 if (mystickyTransition == "on") {
313 // slide effect is selected
314 deactivationHeight = activationHeight;
315
316 if (mysticky_disable_down == "on") {
317 deactivationHeight = activationHeight - myfixedHeight;
318
319 }
320
321
322 } else {
323 activationHeight = mydivHeight;
324 deactivationHeight = mydivHeight;
325
326 }
327
328 }
329
330 }
331
332 }
333
334 headerDeactivateOnHeight();
335
336
337
338 var hasScrollY = 'scrollY' in window;
339 var lastScrollTop = 0;
340
341
342 function onScroll(e) {
343
344
345 //initialDivHeight();
346
347 // if body width is larger than disable at small screen size setting
348
349
350
351
352 if (viewportWidth >= disableWidth) {
353
354
355
356 if ( disableLargeWidth == 0 || viewportWidth <= disableLargeWidth ) {
357
358
359
360 //if (mysticky_disable_down == "on") {
361
362 var y = hasScrollY ? window.scrollY : document.documentElement.scrollTop;
363
364
365
366 //var yScrollPosition = $(this).scrollTop();
367
368
369 // add up or down class to the element depending on scroll direction
370 if (0 <= y ) {
371
372 //var st = $(this).scrollTop();
373
374
375 if (y >= lastScrollTop){
376
377 // downscroll code
378
379
380
381
382 // add myfixed and wrapfixed class to selected fixed element while scroll down
383 y >= activationHeight ? mystickyClass.classList.add('myfixed') : "";
384 y >= activationHeight ? wrappernav.classList.add('wrapfixed') : "";
385
386 y >= activationHeight ? wrappermysticky.style.height = mydivHeight + 'px' : "";
387 y >= activationHeight ? mystickyClass.style.width = mydivWidth + "px" : "";
388
389
390 if (mystickyTransition == "on") {
391
392 if (mysticky_disable_down == "false") {
393 //y < activationHeight + (myfixedHeight + 250) - adminBarHeight ? wrappernav.style.top = "-" + myfixedHeight + "px" : '';
394 //wrappernav.style.top = "-" + myfixedHeight + "px"
395 y >= activationHeight + myfixedHeight - adminBarHeight ? wrappernav.style.top = adminBarHeight + "px" : wrappernav.style.top = "-" + myfixedHeight + "px";
396
397 }
398
399 if ( mydivHeight > myfixedHeight ){
400 // if it's header (guess)
401
402 if (mysticky_disable_down == "false") {
403
404 y < activationHeight + myfixedHeight ? wrappernav.style.top = "-" + mydivHeight + "px" : '';
405 y >= activationHeight + myfixedHeight ? wrappernav.style.top = adminBarHeight + "px" : '';
406
407 }
408
409 }
410
411 }
412
413 wrappernav.classList.add('down');
414 wrappernav.classList.remove('up');
415
416
417 if (mysticky_disable_down == "on") {
418 wrappernav.style.top = "-" + (mydivHeight + adminBarHeight ) + "px";
419 }
420
421
422
423
424 } else {
425 // upscroll code
426
427
428 var x = hasScrollY ? window.scrollY : document.documentElement.scrollTop;
429 //x > deactivationHeight ? '' : mystickyClass.classList.remove('myfixed') ;
430 //x > deactivationHeight ? '' : wrappernav.classList.remove('wrapfixed');
431
432 x > deactivationHeight ? "" : wrappermysticky.style.height = "";
433 x > deactivationHeight ? "" : mystickyClass.style.width = "";
434
435 if (mystickyTransition == "on") {
436
437
438
439 x > deactivationHeight ? '' : mystickyClass.classList.remove('myfixed') ;
440 x > deactivationHeight ? '' : wrappernav.classList.remove('wrapfixed');
441
442 if (mysticky_disable_down == "false") {
443
444 x < deactivationHeight + myfixedHeight + 200 - adminBarHeight ? wrappernav.style.top = "-" + myfixedHeight + "px" : '';
445
446 }
447
448 } else {
449
450 x > deactivationHeight ? "" : mystickyClass.classList.remove('myfixed') ;
451 x > deactivationHeight ? "" : wrappernav.classList.remove('wrapfixed');
452
453
454 }
455
456
457 wrappernav.classList.remove('down');
458 wrappernav.classList.add('up');
459
460 if (mysticky_disable_down == "on") {
461 wrappernav.style.top = adminBarHeight + "px";
462 }
463
464 }
465
466 lastScrollTop = y;
467
468 } else {
469 //if (mysticky_disable_down == "on") {
470 wrappernav.classList.remove('up');
471 //}
472 }
473
474
475 } // if disableWidth is greater than zero
476
477
478 } // if disableLargeWidth is 0 or greater than zero
479
480
481
482
483 }
484
485 document.addEventListener('scroll', onScroll);
486
487
488
489
490 var width = $(window).width()
491
492 function OnResizeDocument () {
493
494
495 // don't recalculate on height change, only width
496 if($(window).width() != width ){
497
498
499 wrappernav.classList.remove('up');
500 wrappernav.classList.remove('down');
501
502 if ($(".wrapfixed")[0]){
503 // If class wrapfixed exists
504 // Remove myfixed and wrapfixed clases so we can calculate
505 mystickyClass.classList.remove('myfixed');
506 wrappernav.classList.remove('wrapfixed');
507
508
509
510 } else {
511 // Else class wrapfixed does not exists
512 initialDivHeight();
513
514 // Remove width
515 mystickyClass.style.removeProperty("width");
516 initialDivWidth();
517
518 }
519 calcViewportWidth();
520 calcAdminBarHeight();
521 fixedDivHeight();
522 calcActivationHeight();
523 headerDeactivateOnHeight();
524
525 }
526
527 }
528
529 window.addEventListener('resize', OnResizeDocument);
530
531 // need to test this, it should fire script on mobile orientation change, since onresize is somehow faulty in this case
532 window.addEventListener('orientationchange', OnResizeDocument);
533
534
535
536
537 });
538
539
540 })(jQuery);