Advertisement
Guenni007

avia-snippet-sticky-header

Sep 21st, 2022
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($)
  2. {
  3.     "use strict";
  4.  
  5.     $( function()
  6.     {
  7.         // decreases header size when user scrolls down
  8.         avia_header_size();
  9.     });
  10.  
  11.  
  12.     function av_change_class($element, change_method, class_name)
  13.     {
  14.         if($element[0].classList)
  15.         {
  16.             if(change_method == "add")
  17.             {
  18.                 $element[0].classList.add(class_name);
  19.             }
  20.             else
  21.             {
  22.                 $element[0].classList.remove(class_name);
  23.             }
  24.         }
  25.         else
  26.         {
  27.             if(change_method == "add")
  28.             {
  29.                 $element.addClass(class_name);
  30.             }
  31.             else
  32.             {
  33.                 $element.removeClass(class_name);
  34.             }
  35.         }
  36.     }
  37.  
  38.  
  39.     function avia_header_size()
  40.     {
  41.         var win             = $(window),
  42.             header          = $('.html_header_top.html_header_sticky #header'),
  43.             unsticktop      = $('.av_header_unstick_top');
  44.  
  45.         if( ! header.length && ! unsticktop.length )
  46.         {
  47.             return;
  48.         }
  49.  
  50.         var logo            = $('#header_main .container .logo img, #header_main .container .logo svg, #header_main .container .logo a'),
  51.             elements        = $('#header_main .container:not(#header_main_alternate>.container), #header_main .main_menu ul:first-child > li > a:not(.avia_mega_div a, #header_main_alternate a), #header_main #menu-item-shop .cart_dropdown_link'),
  52.             el_height       = $(elements).first().height(),
  53.             isMobile        = $.avia_utilities.isMobile,
  54. //  new entries to calculate the correct line-height and set some styles   
  55.         twoliner            = $('.twoliner.menu-item-top-level'),      
  56.         twoliner_text       = $('.twoliner.menu-item-top-level > a > .avia-menu-text'),
  57.         twoliner_no_button  = $('.twoliner.menu-item-top-level:not(.av-menu-button) > a > .avia-menu-text'),
  58.         twoliner_line       = $('#top .av_header_transparency .twoliner.menu-item-top-level .avia-menu-fx'),
  59.             scroll_top      = $('#scroll-top-link'),
  60.             transparent     = header.is('.av_header_transparency'),
  61.             shrinking       = header.is('.av_header_shrinking'),
  62.             header_meta     = header.find('#header_meta'),
  63.             topbar_height   = header_meta.length ? header_meta.outerHeight() : 0,
  64.  
  65.             set_height = function()
  66.             {
  67.                 var st = win.scrollTop(),
  68.                     newH = 0,
  69.                     st_real = st;
  70.  
  71.                 if( unsticktop )
  72.                 {
  73.                     st -= topbar_height;
  74.                 }
  75.  
  76.                 if( st < 0 )
  77.                 {
  78.                     st = 0;
  79.                 }
  80.  
  81.                 if( shrinking && ! isMobile )
  82.                 {
  83.                     if( st < el_height/2 )
  84.                     {
  85.                         newH = el_height - st;
  86.                         if( st <= 0 )
  87.                         {
  88.                             newH = el_height;
  89.                         }
  90.  
  91.                         av_change_class( header, 'remove', 'header-scrolled' );
  92.                         //header.removeClass('header-scrolled');
  93.                     }
  94.                     else
  95.                     {
  96.                         newH = el_height/2;
  97.                         //header.addClass('header-scrolled');
  98.                         av_change_class( header, 'add', 'header-scrolled' );
  99.                     }
  100.  
  101.                     if( st - 30 < el_height )
  102.                     {
  103.                         av_change_class( header, 'remove', 'header-scrolled-full' );
  104.                     }
  105.                     else
  106.                     {
  107.                         av_change_class( header, 'add', 'header-scrolled-full' );
  108.                     }
  109.  
  110.  
  111.                     elements.css({'height': newH + 'px', 'lineHeight': newH + 'px'});
  112.                     logo.css({'maxHeight': newH + 'px'});
  113. //from here we have new settings for twoliners - go and set the class twoliner for those menu-items                
  114.                     twoliner_text.css({
  115.                         'display': 'table-caption',
  116.                         'padding': '0px 9px',
  117.                         'border-top-width': '2px',
  118.                         'white-space': 'break-spaces',
  119.                         'height': newH + 'px',
  120.                         'lineHeight': (newH/2) + 'px'
  121.                     });
  122.                     twoliner_no_button.css({
  123.                         'border-top-color': 'transparent',
  124.                         'border-top-style': 'solid'
  125.                     });
  126.                     twoliner_line.css({'bottom': '0'});
  127.                 }
  128.  
  129.                 if( unsticktop.length )
  130.                 {
  131.                     if( st <= 0 )
  132.                     {
  133.                         if( st_real <= 0 )
  134.                         {
  135.                             st_real = 0;
  136.                         }
  137.  
  138.                         unsticktop.css({"margin-top":"-"+st_real+"px"});
  139.                     }
  140.                     else
  141.                     {
  142.                         unsticktop.css({"margin-top":"-"+topbar_height+"px"});
  143.                     }
  144.                 }
  145.  
  146.                 if( transparent )
  147.                 {
  148.                     if( st > 50 )
  149.                     {
  150.                         //header.removeClass('av_header_transparency');
  151.                         av_change_class( header, 'remove', 'av_header_transparency' );
  152.                     }
  153.                     else
  154.                     {
  155.                         //header.addClass('av_header_transparency');
  156.                         av_change_class( header, 'add', 'av_header_transparency' );
  157.                     }
  158.                 }
  159.  
  160.  
  161.             };
  162.  
  163.         if( $('body').is( '.avia_deactivate_menu_resize' ) )
  164.         {
  165.             shrinking = false;
  166.         }
  167.  
  168.         if( ! transparent && ! shrinking && ! unsticktop.length )
  169.         {
  170.             return;
  171.         }
  172.  
  173.         win.on( 'debouncedresize',  function()
  174.         {
  175.             el_height = $( elements ).attr( 'style',"" ).first().height();
  176.             set_height();
  177.         });
  178.  
  179.         win.on( 'scroll',  function()
  180.         {
  181.             window.requestAnimationFrame( set_height );
  182.         });
  183.  
  184.         set_height();
  185.     }
  186.  
  187.  
  188. })(jQuery);
  189.  
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement