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