mystickymenu.js
602 lines
| 1 | /*! |
| 2 | * myStickymenu by m.r.d.a |
| 3 | * v2 |
| 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 | // below 600 px adminbar is not fixed anymore |
| 210 | adminBarHeight = $('#wpadminbar').height(); |
| 211 | |
| 212 | |
| 213 | |
| 214 | } else { |
| 215 | |
| 216 | adminBarHeight = 0; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | //wrappernav.style.top = adminBarHeight + "px"; |
| 221 | |
| 222 | if (mystickyTransition == "on") { |
| 223 | |
| 224 | wrappernav.style.top = "-" + myfixedHeight + "px"; |
| 225 | //wrappernav.style.top = "-" + myfixedHeight + "px"; |
| 226 | |
| 227 | } else { |
| 228 | wrappernav.style.top = adminBarHeight + "px"; |
| 229 | |
| 230 | } |
| 231 | |
| 232 | } |
| 233 | |
| 234 | |
| 235 | |
| 236 | calcAdminBarHeight(); |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | var mydivWidth; |
| 249 | |
| 250 | |
| 251 | function initialDivWidth(){ |
| 252 | |
| 253 | var rect = $(mystickyClass)[0].getBoundingClientRect(); |
| 254 | mydivWidth = rect.width; |
| 255 | |
| 256 | //var mydivWidth = ((mystickyClass.offsetWidth) + 'px'); |
| 257 | //mystickyClass.style.width = mydivWidth + "px"; |
| 258 | |
| 259 | |
| 260 | } |
| 261 | |
| 262 | initialDivWidth(); |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | var deactivationHeight = activationHeight; |
| 280 | |
| 281 | function CalcActivationHeight() { |
| 282 | |
| 283 | // If activate height (Make visible on Scroll) is set to 0, automatic calculation will be used. |
| 284 | if ( autoActivate == true ) { |
| 285 | |
| 286 | // Automatic calculation of activation and deactivation height (Make visible on Scroll is set to 0). |
| 287 | if (mystickyTransition == "on") { |
| 288 | // Slide effect is selected |
| 289 | //activationHeight = $(mystickyClass).offset().top + mystickyClass.offsetHeight - adminBarHeight; |
| 290 | activationHeight = $(mystickyClass).offset().top + mydivHeight - adminBarHeight; |
| 291 | deactivationHeight = $(mystickyClass).offset().top + mydivHeight - adminBarHeight; |
| 292 | //deactivationHeight = $(mystickyClass).offset().top - adminBarHeight; |
| 293 | |
| 294 | if (mysticky_disable_down == "on") { |
| 295 | deactivationHeight = $(mystickyClass).offset().top - adminBarHeight; |
| 296 | |
| 297 | } |
| 298 | |
| 299 | } |
| 300 | |
| 301 | if (mystickyTransition == "fade") { |
| 302 | |
| 303 | if (mysticky_disable_down == "false") { |
| 304 | // Fade effect is selected |
| 305 | activationHeight = $(mystickyClass).offset().top - adminBarHeight; |
| 306 | |
| 307 | deactivationHeight = $(mystickyClass).offset().top - adminBarHeight; |
| 308 | |
| 309 | } |
| 310 | |
| 311 | if (mysticky_disable_down == "on") { |
| 312 | |
| 313 | // Fade effect is selected |
| 314 | activationHeight = $(mystickyClass).offset().top - adminBarHeight + mydivHeight; |
| 315 | deactivationHeight = $(mystickyClass).offset().top - adminBarHeight; |
| 316 | |
| 317 | } |
| 318 | |
| 319 | } |
| 320 | |
| 321 | } |
| 322 | |
| 323 | } |
| 324 | |
| 325 | CalcActivationHeight(); |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | function HeaderDeactivateOnHeight() { |
| 333 | |
| 334 | |
| 335 | if ( autoActivate == true ) { |
| 336 | |
| 337 | if ( mydivHeight > myfixedHeight ){ |
| 338 | // Auto activate is true, Make visible on Scroll is set to 0, menu is probably header |
| 339 | |
| 340 | |
| 341 | |
| 342 | if (mystickyTransition == "on") { |
| 343 | // slide effect is selected |
| 344 | deactivationHeight = activationHeight; |
| 345 | |
| 346 | if (mysticky_disable_down == "on") { |
| 347 | deactivationHeight = activationHeight - myfixedHeight; |
| 348 | |
| 349 | } |
| 350 | |
| 351 | |
| 352 | } else { |
| 353 | activationHeight = mydivHeight; |
| 354 | deactivationHeight = mydivHeight; |
| 355 | |
| 356 | } |
| 357 | |
| 358 | } |
| 359 | |
| 360 | } |
| 361 | |
| 362 | } |
| 363 | |
| 364 | HeaderDeactivateOnHeight(); |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | var hasScrollY = 'scrollY' in window; |
| 372 | var lastScrollTop = 0; |
| 373 | |
| 374 | |
| 375 | function onScroll(e) { |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | //initialDivHeight(); |
| 381 | |
| 382 | // if body width is larger than disable at small screen size setting |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | if (viewportWidth >= disableWidth) { |
| 388 | |
| 389 | |
| 390 | |
| 391 | //if (mysticky_disable_down == "on") { |
| 392 | |
| 393 | var y = hasScrollY ? window.scrollY : document.documentElement.scrollTop; |
| 394 | |
| 395 | |
| 396 | |
| 397 | //var yScrollPosition = $(this).scrollTop(); |
| 398 | |
| 399 | |
| 400 | // add up or down class to the element depending on scroll direction |
| 401 | if (0 <= y ) { |
| 402 | |
| 403 | //var st = $(this).scrollTop(); |
| 404 | |
| 405 | |
| 406 | if (y >= lastScrollTop){ |
| 407 | |
| 408 | // downscroll code |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | // add myfixed and wrapfixed class to selected fixed element while scroll down |
| 414 | y >= activationHeight ? mystickyClass.classList.add('myfixed') : ""; |
| 415 | y >= activationHeight ? wrappernav.classList.add('wrapfixed') : ""; |
| 416 | |
| 417 | y >= activationHeight ? wrappermysticky.style.height = mydivHeight + 'px' : ""; |
| 418 | y >= activationHeight ? mystickyClass.style.width = mydivWidth + "px" : ""; |
| 419 | |
| 420 | |
| 421 | if (mystickyTransition == "on") { |
| 422 | |
| 423 | if (mysticky_disable_down == "false") { |
| 424 | //y < activationHeight + (myfixedHeight + 250) - adminBarHeight ? wrappernav.style.top = "-" + myfixedHeight + "px" : ''; |
| 425 | //wrappernav.style.top = "-" + myfixedHeight + "px" |
| 426 | y >= activationHeight + myfixedHeight - adminBarHeight ? wrappernav.style.top = adminBarHeight + "px" : wrappernav.style.top = "-" + myfixedHeight + "px"; |
| 427 | |
| 428 | } |
| 429 | |
| 430 | if ( mydivHeight > myfixedHeight ){ |
| 431 | // if it's header (guess) |
| 432 | |
| 433 | if (mysticky_disable_down == "false") { |
| 434 | |
| 435 | y < activationHeight + myfixedHeight ? wrappernav.style.top = "-" + mydivHeight + "px" : ''; |
| 436 | y >= activationHeight + myfixedHeight ? wrappernav.style.top = adminBarHeight + "px" : ''; |
| 437 | |
| 438 | } |
| 439 | |
| 440 | } |
| 441 | |
| 442 | } |
| 443 | |
| 444 | wrappernav.classList.add('down'); |
| 445 | wrappernav.classList.remove('up'); |
| 446 | |
| 447 | |
| 448 | if (mysticky_disable_down == "on") { |
| 449 | wrappernav.style.top = "-" + (mydivHeight + adminBarHeight ) + "px"; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | } else { |
| 456 | // upscroll code |
| 457 | |
| 458 | |
| 459 | var x = hasScrollY ? window.scrollY : document.documentElement.scrollTop; |
| 460 | //x > deactivationHeight ? '' : mystickyClass.classList.remove('myfixed') ; |
| 461 | //x > deactivationHeight ? '' : wrappernav.classList.remove('wrapfixed'); |
| 462 | |
| 463 | x > deactivationHeight ? "" : wrappermysticky.style.height = ""; |
| 464 | x > deactivationHeight ? "" : mystickyClass.style.width = ""; |
| 465 | |
| 466 | if (mystickyTransition == "on") { |
| 467 | |
| 468 | |
| 469 | |
| 470 | x > deactivationHeight ? '' : mystickyClass.classList.remove('myfixed') ; |
| 471 | x > deactivationHeight ? '' : wrappernav.classList.remove('wrapfixed'); |
| 472 | |
| 473 | if (mysticky_disable_down == "false") { |
| 474 | |
| 475 | x < deactivationHeight + myfixedHeight + 200 - adminBarHeight ? wrappernav.style.top = "-" + myfixedHeight + "px" : ''; |
| 476 | |
| 477 | } |
| 478 | |
| 479 | } else { |
| 480 | |
| 481 | x > deactivationHeight ? "" : mystickyClass.classList.remove('myfixed') ; |
| 482 | x > deactivationHeight ? "" : wrappernav.classList.remove('wrapfixed'); |
| 483 | |
| 484 | |
| 485 | } |
| 486 | |
| 487 | |
| 488 | wrappernav.classList.remove('down'); |
| 489 | wrappernav.classList.add('up'); |
| 490 | |
| 491 | if (mysticky_disable_down == "on") { |
| 492 | wrappernav.style.top = adminBarHeight + "px"; |
| 493 | } |
| 494 | |
| 495 | } |
| 496 | |
| 497 | lastScrollTop = y; |
| 498 | |
| 499 | } else { |
| 500 | //if (mysticky_disable_down == "on") { |
| 501 | wrappernav.classList.remove('up'); |
| 502 | //} |
| 503 | } |
| 504 | |
| 505 | |
| 506 | } |
| 507 | |
| 508 | } |
| 509 | |
| 510 | document.addEventListener('scroll', onScroll); |
| 511 | |
| 512 | |
| 513 | var width = $(window).width() |
| 514 | |
| 515 | function OnResizeDocument () { |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | if($(window).width() != width ){ |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | wrappernav.classList.remove('up'); |
| 528 | wrappernav.classList.remove('down'); |
| 529 | |
| 530 | if ($(".wrapfixed")[0]){ |
| 531 | // If class wrapfixed exists |
| 532 | // Remove myfixed and wrapfixed clases so we can calculate |
| 533 | mystickyClass.classList.remove('myfixed'); |
| 534 | wrappernav.classList.remove('wrapfixed'); |
| 535 | |
| 536 | |
| 537 | |
| 538 | } else { |
| 539 | // Else class wrapfixed does not exists |
| 540 | initialDivHeight(); |
| 541 | |
| 542 | // Remove width |
| 543 | mystickyClass.style.removeProperty("width"); |
| 544 | initialDivWidth(); |
| 545 | |
| 546 | |
| 547 | // Calculate new width - need to put this in function like height |
| 548 | //var mydivWidth = ((mystickyClass.offsetWidth) + 'px'); |
| 549 | //mystickyClass.style.width = mydivWidth; |
| 550 | |
| 551 | |
| 552 | |
| 553 | // var rect = $(mystickyClass)[0].getBoundingClientRect(); |
| 554 | // var mydivWidth = rect.width + "px"; |
| 555 | |
| 556 | //var mydivWidth = ((mystickyClass.offsetWidth) + 'px'); |
| 557 | // mystickyClass.style.width = mydivWidth; |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | } |
| 568 | calcViewportWidth(); |
| 569 | calcAdminBarHeight(); |
| 570 | fixedDivHeight(); |
| 571 | |
| 572 | CalcActivationHeight(); |
| 573 | HeaderDeactivateOnHeight(); |
| 574 | |
| 575 | } |
| 576 | |
| 577 | } |
| 578 | |
| 579 | window.addEventListener('resize', OnResizeDocument); |
| 580 | |
| 581 | //var lazyLayout = OnResizeDocument().debounce(calculateLayout, 300); |
| 582 | //$(window).resize(lazyLayout); |
| 583 | |
| 584 | //window.addEventListener('resize', lazyLayout); |
| 585 | |
| 586 | // need to test this, it should fire script on mobile orientation change, since onresize is somehow faulty in this case |
| 587 | window.addEventListener('orientationchange', OnResizeDocument); |
| 588 | |
| 589 | //} |
| 590 | |
| 591 | |
| 592 | } |
| 593 | |
| 594 | else { |
| 595 | console.log("myStickymenu: Entered Sticky Class does not exist, change it in Dashboard / Settings / myStickymenu / Sticky Class. "); |
| 596 | // Do something if class does not exist |
| 597 | } |
| 598 | |
| 599 | }); |
| 600 | |
| 601 | |
| 602 | })(jQuery); |