Advertisement
Guenni007

avia-snippet-sticky-header

Aug 28th, 2024 (edited)
40
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.             shrink_factor   = header.data( 'av_shrink_factor' ),
  58.             shrink_val      = el_height/2.0,
  59.             el_shrinked     = el_height/2.0,
  60.             header_meta     = header.find('#header_meta'),
  61.             topbar_height   = header_meta.length ? header_meta.outerHeight() : 0,
  62. // some new variables          
  63.             corrector_distance = 40,
  64.             trigger_distance = $('.trigger').length ? $('.trigger').offset().top - $('.trigger').height() + el_height - corrector_distance  : 0 ,
  65.  
  66.            
  67.             set_height = function()
  68.             {
  69.                 var st = win.scrollTop() - trigger_distance,
  70.                     newH = 0,
  71.                     st_real = st;
  72.  
  73.                 if( unsticktop )
  74.                 {
  75.                     st -= topbar_height;
  76.                 }
  77.  
  78.                 if( st < 0 )
  79.                 {
  80.                     st = 0;
  81.                 }
  82.  
  83.                 if( shrinking && ! isMobile )
  84.                 {
  85.                     if( st < shrink_val )
  86.                     {
  87.                         newH = el_height - st;
  88.                         if( st <= 0 )
  89.                         {
  90.                             newH = el_height;
  91.                         }
  92.  
  93.                         av_change_class( header, 'remove', 'header-scrolled' );
  94.                         //header.removeClass('header-scrolled');
  95.                     }
  96.                     else
  97.                     {
  98.                         newH = el_shrinked;
  99.                         //header.addClass('header-scrolled');
  100.                         av_change_class( header, 'add', 'header-scrolled' );
  101.                     }
  102.  
  103.                     if( st - 30 < el_height )
  104.                     {
  105.                         av_change_class( header, 'remove', 'header-scrolled-full' );
  106.                     }
  107.                     else
  108.                     {
  109.                         av_change_class( header, 'add', 'header-scrolled-full' );
  110.                     }
  111.  
  112.                     elements.css({'height': newH + 'px', 'lineHeight': newH + 'px'});
  113.                     logo.css({'maxHeight': newH + 'px'});
  114.                 }
  115.  
  116.                 if( unsticktop.length )
  117.                 {
  118.                     if( st <= 0 )
  119.                     {
  120.                         if( st_real <= 0 )
  121.                         {
  122.                             st_real = 0;
  123.                         }
  124.  
  125.                         unsticktop.css({"margin-top":"-"+st_real+"px"});
  126.                     }
  127.                     else
  128.                     {
  129.                         unsticktop.css({"margin-top":"-"+topbar_height+"px"});
  130.                     }
  131.                 }
  132.  
  133.                 if( transparent )
  134.                 {
  135.                     if( st > 50 )
  136.                     {
  137.                         //header.removeClass('av_header_transparency');
  138.                         av_change_class( header, 'remove', 'av_header_transparency' );
  139.                     }
  140.                     else
  141.                     {
  142.                         //header.addClass('av_header_transparency');
  143.                         av_change_class( header, 'add', 'av_header_transparency' );
  144.                     }
  145.                 }
  146.             };
  147.  
  148.         if( typeof shrink_factor != 'undefined' )
  149.         {
  150.             const value = parseInt( shrink_factor );
  151.             if( ! isNaN( value ) )
  152.             {
  153.                 shrink_val = el_height * ( value / 100.0 );
  154.                 el_shrinked = el_height - shrink_val;
  155.             }
  156.         }
  157.  
  158.         if( $('body').is( '.avia_deactivate_menu_resize' ) )
  159.         {
  160.             shrinking = false;
  161.         }
  162.  
  163.         if( ! transparent && ! shrinking && ! unsticktop.length )
  164.         {
  165.             return;
  166.         }
  167.  
  168.         win.on( 'debouncedresize',  function()
  169.         {
  170.             el_height = $( elements ).attr( 'style',"" ).first().height();
  171.             set_height();
  172.             trigger_distance = $('.trigger').length ? $('.trigger').offset().top - $('.trigger').height() + el_height - corrector_distance  : 0 ;
  173.         });
  174.  
  175.         win.on( 'scroll',  function()
  176.         {
  177.             window.requestAnimationFrame( set_height );
  178.         });
  179.  
  180.         set_height();
  181.        
  182.     }
  183.    
  184. })(jQuery);
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement