Advertisement
Guenni007

avia-snippet-sticky-header

Aug 17th, 2020
2,904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($)
  2. {  
  3.     "use strict";
  4.    
  5.     $(document).ready(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 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).filter(':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.             top_logo        = $('#top.page-id-125 #header_meta .toplogo img'),
  60.             // new element that is included to the script
  61.             set_height      = function()
  62.             {  
  63.                 var st = win.scrollTop(),
  64.                     newH = 0,
  65.                     st_real = st;
  66.            
  67.                 if( unsticktop )
  68.                 {
  69.                     st -= topbar_height;
  70.                 }
  71.                
  72.                 if( st < 0 )
  73.                 {
  74.                     st = 0;
  75.                 }
  76.                
  77.                 if( shrinking )
  78.                 {
  79.                     if( st < el_height/1.285 )
  80.                     {
  81.                         newH = el_height - st;
  82.                         if( st <= 0 )
  83.                         {
  84.                             newH = el_height;
  85.                         }
  86.  
  87.                         av_change_class( header, 'remove', 'header-scrolled' );
  88.                         //header.removeClass('header-scrolled');
  89.                     }
  90.                     else
  91.                     {
  92.                         newH = el_height/4.545;
  93.                         //header.addClass('header-scrolled');
  94.                         av_change_class( header, 'add', 'header-scrolled' );
  95.                     }
  96.  
  97.                     if( st - 30 < el_height )
  98.                     {
  99.                         av_change_class( header, 'remove', 'header-scrolled-full' );
  100.                     }
  101.                     else
  102.                     {
  103.                         av_change_class( header, 'add', 'header-scrolled-full' );
  104.                     }
  105.                    
  106.                    
  107.                     elements.css({'height': newH + 'px', 'lineHeight': newH + 'px'});
  108.                     logo.css({'maxHeight': newH + 'px',});
  109.                     top_logo.css({'maxHeight': 1.7*newH + 'px',});
  110.  
  111.                 }
  112.                
  113.                 if( unsticktop.length )
  114.                 {
  115.                     if( st <= 0 )
  116.                     {
  117.                         if( st_real <= 0 )
  118.                         {
  119.                             st_real = 0;
  120.                         }
  121.  
  122.                         unsticktop.css({"margin-top":"-"+st_real+"px"});
  123.                     }
  124.                     else
  125.                     {
  126.                         unsticktop.css({"margin-top":"-"+topbar_height+"px"});
  127.                     }
  128.                 }
  129.                
  130.                 if( transparent )
  131.                 {  
  132.                     if( st > 50 )
  133.                     {  
  134.                         //header.removeClass('av_header_transparency');
  135.                         av_change_class( header, 'remove', 'av_header_transparency' );
  136.                     }
  137.                     else
  138.                     {
  139.                         //header.addClass('av_header_transparency');
  140.                         av_change_class( header, 'add', 'av_header_transparency' );
  141.                     }
  142.                 }
  143.  
  144.                
  145.             };
  146.  
  147.         if( $('body').is( '.avia_deactivate_menu_resize' ) )
  148.         {
  149.             shrinking = false;
  150.         }
  151.  
  152.         if( ! transparent && ! shrinking && ! unsticktop.length )
  153.         {
  154.             return;
  155.         }
  156.  
  157.         win.on( 'debouncedresize',  function(){
  158.                                 el_height = $( elements ).attr( 'style',"" ).filter( ':first' ).height();
  159.                                 set_height();
  160.                             });
  161.  
  162.         win.on( 'scroll',  function(){
  163.                                 window.requestAnimationFrame( set_height );
  164.                             });
  165.  
  166.         set_height();
  167.     }
  168.  
  169.  
  170. })(jQuery);
  171.  
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement