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