Advertisement
Guenni007

avia-snippet-sticky-header

Feb 23rd, 2023
496
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.             scroll_top      = $('#scroll-top-link'),
  55.             transparent     = header.is('.av_header_transparency'),
  56.             shrinking       = header.is('.av_header_shrinking'),
  57.             header_meta     = header.find('#header_meta'),
  58.             topbar_height   = header_meta.length ? header_meta.outerHeight() : 0,
  59.  
  60.             set_height = function()
  61.             {
  62.                 var st = win.scrollTop(),
  63.                     newH = 0,
  64.                     st_real = st;
  65.  
  66.                 if( unsticktop )
  67.                 {
  68.                     st -= topbar_height;
  69.                 }
  70.  
  71.                 if( st < 0 )
  72.                 {
  73.                     st = 0;
  74.                 }
  75.  
  76.                 if( shrinking && ! isMobile )
  77.                 {
  78.                     if( st < el_height/4 )
  79.                     {
  80.                         newH = el_height - st;
  81.                         if( st <= 0 )
  82.                         {
  83.                             newH = el_height;
  84.                         }
  85.  
  86.                         av_change_class( header, 'remove', 'header-scrolled' );
  87.                         //header.removeClass('header-scrolled');
  88.                     }
  89.                     else
  90.                     {
  91.                         newH = el_height/1.333;
  92.                         //header.addClass('header-scrolled');
  93.                         av_change_class( header, 'add', 'header-scrolled' );
  94.                     }
  95.  
  96.                     if( st - 30 < el_height )
  97.                     {
  98.                         av_change_class( header, 'remove', 'header-scrolled-full' );
  99.                     }
  100.                     else
  101.                     {
  102.                         av_change_class( header, 'add', 'header-scrolled-full' );
  103.                     }
  104.  
  105.  
  106.                     elements.css({'height': newH + 'px', 'lineHeight': newH + 'px'});
  107.                     logo.css({'maxHeight': newH + 'px'});
  108.                 }
  109.  
  110.                 if( unsticktop.length )
  111.                 {
  112.                     if( st <= 0 )
  113.                     {
  114.                         if( st_real <= 0 )
  115.                         {
  116.                             st_real = 0;
  117.                         }
  118.  
  119.                         unsticktop.css({"margin-top":"-"+st_real+"px"});
  120.                     }
  121.                     else
  122.                     {
  123.                         unsticktop.css({"margin-top":"-"+topbar_height+"px"});
  124.                     }
  125.                 }
  126.  
  127.                 if( transparent )
  128.                 {
  129.                     if( st > 50 )
  130.                     {
  131.                         //header.removeClass('av_header_transparency');
  132.                         av_change_class( header, 'remove', 'av_header_transparency' );
  133.                     }
  134.                     else
  135.                     {
  136.                         //header.addClass('av_header_transparency');
  137.                         av_change_class( header, 'add', 'av_header_transparency' );
  138.                     }
  139.                 }
  140.  
  141.  
  142.             };
  143.  
  144.         if( $('body').is( '.avia_deactivate_menu_resize' ) )
  145.         {
  146.             shrinking = false;
  147.         }
  148.  
  149.         if( ! transparent && ! shrinking && ! unsticktop.length )
  150.         {
  151.             return;
  152.         }
  153.  
  154.         win.on( 'debouncedresize',  function()
  155.         {
  156.             el_height = $( elements ).attr( 'style',"" ).first().height();
  157.             set_height();
  158.         });
  159.  
  160.         win.on( 'scroll',  function()
  161.         {
  162.             window.requestAnimationFrame( set_height );
  163.         });
  164.  
  165.         set_height();
  166.     }
  167.  
  168.  
  169. })(jQuery);
  170.  
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement