PluginProbe ʕ •ᴥ•ʔ
My Sticky Bar – Floating Notification Bar & Sticky Header (formerly myStickymenu) / 2.0.3
My Sticky Bar – Floating Notification Bar & Sticky Header (formerly myStickymenu) v2.0.3
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
610 lines
1 /*!
2 * myStickymenu by m.r.d.a
3 * v2.0.1
4 */
5
6 (function( $ ) {
7 "use strict";
8
9 jQuery(document).ready(function($){
10
11
12
13 // get Sticky Class setting if class name existts
14 if ($(option.mystickyClass)[0]){
15
16 // Do something if class exists
17 var mystickyClass = document.querySelector(option.mystickyClass);
18
19 // get disable at small screen size setting
20 var disableWidth = parseInt(option.disableWidth);
21
22 // get body width
23 //var bodyWidth = parseInt(document.body.clientWidth);
24
25 // get transition effect (slide or fade)
26 var mystickyTransition = option.mystickyTransition;
27
28 // get activaton height setting
29 var activationHeight = parseInt(option.activationHeight);
30
31 // if is admin bar showing, needed for auto calc of activation height when admin bar is showing
32 var adminBar = option.adminBar;
33
34 // disable on scroll down
35 var mysticky_disable_down = option.mysticky_disable_down;
36
37
38
39
40
41
42
43
44
45 var viewportWidth;
46
47
48 function calcViewportWidth(e){
49
50 // Calculate actual viewport width
51 var e = window, a = 'inner';
52
53 if (!('innerWidth' in window )) {
54 a = 'client';
55 e = document.documentElement || document.body;
56 }
57 viewportWidth = e[ a+'Width' ];
58
59
60 }
61
62
63 calcViewportWidth();
64
65
66
67
68
69
70
71
72 var parentmysticky = mystickyClass.parentNode;
73
74 var wrappermysticky = document.createElement('div');
75 var position = 0;
76 for(var i = 0; i < parentmysticky.childNodes.length; i++) {
77 if(parentmysticky.childNodes[i] == mystickyClass) {
78 position = i;
79 break;
80 }
81 }
82
83 wrappermysticky.id = 'mysticky-wrap';
84 wrappermysticky.appendChild(mystickyClass);
85 parentmysticky.insertBefore(wrappermysticky, parentmysticky.childNodes[position]);
86
87 var parentnav = mystickyClass.parentNode;
88 var wrappernav = document.createElement('div');
89 wrappernav.id = 'mysticky-nav';
90 parentnav.replaceChild(wrappernav, mystickyClass);
91 wrappernav.appendChild(mystickyClass);
92
93
94
95
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
110
111
112
113
114
115
116
117
118
119
120
121
122 var mydivHeight;
123
124
125 function initialDivHeight(){
126
127
128
129
130 // get initial element height of selected sticky class
131 mydivHeight = (mystickyClass.offsetHeight);
132
133
134
135 // when initial element have margin bottom - awaken example using #masthead class
136
137 if (parseInt($(mystickyClass).css("marginBottom")) > 0) {
138
139 // element have margin bottom, apply it to initial wrap
140 //$(mystickyClass).css("marginBottom").replace('px', '')
141 wrappermysticky.style.marginBottom = ($(mystickyClass).css("marginBottom"));
142 }
143
144
145 if (mydivHeight == "0") {
146 // 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
147
148 $(mystickyClass).children().filter(':visible').each(function(){
149 mydivHeight = $(this).outerHeight(true);
150
151 });
152
153 }
154
155 if (viewportWidth >= disableWidth) {
156 //wrappermysticky.style.height = mydivHeight + 'px';
157 }
158 }
159
160 initialDivHeight();
161
162
163
164
165 var myfixedHeight;
166
167 function fixedDivHeight(){
168 //if ( autoActivate == true ) {
169
170 // calculate element height while fixed
171
172 mystickyClass.classList.add('myfixed')
173
174 myfixedHeight = $(".myfixed").outerHeight();
175
176 if (myfixedHeight == "0") {
177 // something is wrong, wrapper cant be zero, try to calculate again with div children.
178 $(".myfixed").children().filter(':visible').each(function(){
179 myfixedHeight = $(this).outerHeight(true);
180 });
181 }
182
183 mystickyClass.classList.remove('myfixed');
184
185 //}
186
187 }
188
189 fixedDivHeight();
190
191
192
193
194
195
196
197
198
199
200 var adminBarHeight = 0;
201
202
203 function calcAdminBarHeight(){
204
205
206
207
208 if ((adminBar == "true" ) && (viewportWidth > 600)) {
209
210 if ($("#wpadminbar")[0]){
211
212 adminBarHeight = $('#wpadminbar').height();
213
214 } else {
215
216 adminBarHeight = 0;
217
218 }
219
220
221
222 } else {
223
224 adminBarHeight = 0;
225 }
226
227
228 //wrappernav.style.top = adminBarHeight + "px";
229
230 if (mystickyTransition == "on") {
231
232 wrappernav.style.top = "-" + myfixedHeight + "px";
233 //wrappernav.style.top = "-" + myfixedHeight + "px";
234
235 } else {
236 wrappernav.style.top = adminBarHeight + "px";
237
238 }
239
240 }
241
242
243
244 calcAdminBarHeight();
245
246
247
248
249
250
251
252
253
254
255
256 var mydivWidth;
257
258
259 function initialDivWidth(){
260
261 var rect = $(mystickyClass)[0].getBoundingClientRect();
262 mydivWidth = rect.width;
263
264 //var mydivWidth = ((mystickyClass.offsetWidth) + 'px');
265 //mystickyClass.style.width = mydivWidth + "px";
266
267
268 }
269
270 initialDivWidth();
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287 var deactivationHeight = activationHeight;
288
289 function CalcActivationHeight() {
290
291 // If activate height (Make visible on Scroll) is set to 0, automatic calculation will be used.
292 if ( autoActivate == true ) {
293
294 // Automatic calculation of activation and deactivation height (Make visible on Scroll is set to 0).
295 if (mystickyTransition == "on") {
296 // Slide effect is selected
297 //activationHeight = $(mystickyClass).offset().top + mystickyClass.offsetHeight - adminBarHeight;
298 activationHeight = $(mystickyClass).offset().top + mydivHeight - adminBarHeight;
299 deactivationHeight = $(mystickyClass).offset().top + mydivHeight - adminBarHeight;
300 //deactivationHeight = $(mystickyClass).offset().top - adminBarHeight;
301
302 if (mysticky_disable_down == "on") {
303 deactivationHeight = $(mystickyClass).offset().top - adminBarHeight;
304
305 }
306
307 }
308
309 if (mystickyTransition == "fade") {
310
311 if (mysticky_disable_down == "false") {
312 // Fade effect is selected
313 activationHeight = $(mystickyClass).offset().top - adminBarHeight;
314
315 deactivationHeight = $(mystickyClass).offset().top - adminBarHeight;
316
317 }
318
319 if (mysticky_disable_down == "on") {
320
321 // Fade effect is selected
322 activationHeight = $(mystickyClass).offset().top - adminBarHeight + mydivHeight;
323 deactivationHeight = $(mystickyClass).offset().top - adminBarHeight;
324
325 }
326
327 }
328
329 }
330
331 }
332
333 CalcActivationHeight();
334
335
336
337
338
339
340 function HeaderDeactivateOnHeight() {
341
342
343 if ( autoActivate == true ) {
344
345 if ( mydivHeight > myfixedHeight ){
346 // Auto activate is true, Make visible on Scroll is set to 0, menu is probably header
347
348
349
350 if (mystickyTransition == "on") {
351 // slide effect is selected
352 deactivationHeight = activationHeight;
353
354 if (mysticky_disable_down == "on") {
355 deactivationHeight = activationHeight - myfixedHeight;
356
357 }
358
359
360 } else {
361 activationHeight = mydivHeight;
362 deactivationHeight = mydivHeight;
363
364 }
365
366 }
367
368 }
369
370 }
371
372 HeaderDeactivateOnHeight();
373
374
375
376
377
378
379 var hasScrollY = 'scrollY' in window;
380 var lastScrollTop = 0;
381
382
383 function onScroll(e) {
384
385
386
387
388 //initialDivHeight();
389
390 // if body width is larger than disable at small screen size setting
391
392
393
394
395 if (viewportWidth >= disableWidth) {
396
397
398
399 //if (mysticky_disable_down == "on") {
400
401 var y = hasScrollY ? window.scrollY : document.documentElement.scrollTop;
402
403
404
405 //var yScrollPosition = $(this).scrollTop();
406
407
408 // add up or down class to the element depending on scroll direction
409 if (0 <= y ) {
410
411 //var st = $(this).scrollTop();
412
413
414 if (y >= lastScrollTop){
415
416 // downscroll code
417
418
419
420
421 // add myfixed and wrapfixed class to selected fixed element while scroll down
422 y >= activationHeight ? mystickyClass.classList.add('myfixed') : "";
423 y >= activationHeight ? wrappernav.classList.add('wrapfixed') : "";
424
425 y >= activationHeight ? wrappermysticky.style.height = mydivHeight + 'px' : "";
426 y >= activationHeight ? mystickyClass.style.width = mydivWidth + "px" : "";
427
428
429 if (mystickyTransition == "on") {
430
431 if (mysticky_disable_down == "false") {
432 //y < activationHeight + (myfixedHeight + 250) - adminBarHeight ? wrappernav.style.top = "-" + myfixedHeight + "px" : '';
433 //wrappernav.style.top = "-" + myfixedHeight + "px"
434 y >= activationHeight + myfixedHeight - adminBarHeight ? wrappernav.style.top = adminBarHeight + "px" : wrappernav.style.top = "-" + myfixedHeight + "px";
435
436 }
437
438 if ( mydivHeight > myfixedHeight ){
439 // if it's header (guess)
440
441 if (mysticky_disable_down == "false") {
442
443 y < activationHeight + myfixedHeight ? wrappernav.style.top = "-" + mydivHeight + "px" : '';
444 y >= activationHeight + myfixedHeight ? wrappernav.style.top = adminBarHeight + "px" : '';
445
446 }
447
448 }
449
450 }
451
452 wrappernav.classList.add('down');
453 wrappernav.classList.remove('up');
454
455
456 if (mysticky_disable_down == "on") {
457 wrappernav.style.top = "-" + (mydivHeight + adminBarHeight ) + "px";
458 }
459
460
461
462
463 } else {
464 // upscroll code
465
466
467 var x = hasScrollY ? window.scrollY : document.documentElement.scrollTop;
468 //x > deactivationHeight ? '' : mystickyClass.classList.remove('myfixed') ;
469 //x > deactivationHeight ? '' : wrappernav.classList.remove('wrapfixed');
470
471 x > deactivationHeight ? "" : wrappermysticky.style.height = "";
472 x > deactivationHeight ? "" : mystickyClass.style.width = "";
473
474 if (mystickyTransition == "on") {
475
476
477
478 x > deactivationHeight ? '' : mystickyClass.classList.remove('myfixed') ;
479 x > deactivationHeight ? '' : wrappernav.classList.remove('wrapfixed');
480
481 if (mysticky_disable_down == "false") {
482
483 x < deactivationHeight + myfixedHeight + 200 - adminBarHeight ? wrappernav.style.top = "-" + myfixedHeight + "px" : '';
484
485 }
486
487 } else {
488
489 x > deactivationHeight ? "" : mystickyClass.classList.remove('myfixed') ;
490 x > deactivationHeight ? "" : wrappernav.classList.remove('wrapfixed');
491
492
493 }
494
495
496 wrappernav.classList.remove('down');
497 wrappernav.classList.add('up');
498
499 if (mysticky_disable_down == "on") {
500 wrappernav.style.top = adminBarHeight + "px";
501 }
502
503 }
504
505 lastScrollTop = y;
506
507 } else {
508 //if (mysticky_disable_down == "on") {
509 wrappernav.classList.remove('up');
510 //}
511 }
512
513
514 }
515
516 }
517
518 document.addEventListener('scroll', onScroll);
519
520
521 var width = $(window).width()
522
523 function OnResizeDocument () {
524
525
526
527
528 if($(window).width() != width ){
529
530
531
532
533
534
535 wrappernav.classList.remove('up');
536 wrappernav.classList.remove('down');
537
538 if ($(".wrapfixed")[0]){
539 // If class wrapfixed exists
540 // Remove myfixed and wrapfixed clases so we can calculate
541 mystickyClass.classList.remove('myfixed');
542 wrappernav.classList.remove('wrapfixed');
543
544
545
546 } else {
547 // Else class wrapfixed does not exists
548 initialDivHeight();
549
550 // Remove width
551 mystickyClass.style.removeProperty("width");
552 initialDivWidth();
553
554
555 // Calculate new width - need to put this in function like height
556 //var mydivWidth = ((mystickyClass.offsetWidth) + 'px');
557 //mystickyClass.style.width = mydivWidth;
558
559
560
561 // var rect = $(mystickyClass)[0].getBoundingClientRect();
562 // var mydivWidth = rect.width + "px";
563
564 //var mydivWidth = ((mystickyClass.offsetWidth) + 'px');
565 // mystickyClass.style.width = mydivWidth;
566
567
568
569
570
571
572
573
574
575 }
576 calcViewportWidth();
577 calcAdminBarHeight();
578 fixedDivHeight();
579
580 CalcActivationHeight();
581 HeaderDeactivateOnHeight();
582
583 }
584
585 }
586
587 window.addEventListener('resize', OnResizeDocument);
588
589 //var lazyLayout = OnResizeDocument().debounce(calculateLayout, 300);
590 //$(window).resize(lazyLayout);
591
592 //window.addEventListener('resize', lazyLayout);
593
594 // need to test this, it should fire script on mobile orientation change, since onresize is somehow faulty in this case
595 window.addEventListener('orientationchange', OnResizeDocument);
596
597 //}
598
599
600 }
601
602 else {
603 console.log("myStickymenu: Entered Sticky Class does not exist, change it in Dashboard / Settings / myStickymenu / Sticky Class. ");
604 // Do something if class does not exist
605 }
606
607 });
608
609
610 })(jQuery);