/** * Stick elements * * => How to use? * * jQuery( {SELECTOR} ).astExtSticky( { * dependent: [{selectors}], // Not required. Default: []. Stick element dependent selectors. * stick_upto_scroll: {value}, // Not required. Default: 0. Stick element after scroll upto the {value} in px. * gutter: {value}, // Not required. Default: 0. Stick element from top of the window in px\. * }); * * @package Astra Addon * @since 1.0.0 */ ;(function ( $, window, undefined ) { var pluginName = 'astExtSticky', document = window.document, windowWidth = jQuery( window ).outerWidth(), viewPortWidth = jQuery( window ).width(), header_builder_active = astraAddon.header_builder_active, defaults = { dependent : [], max_width : '', site_layout : '', break_point : 920, admin_bar_height_lg : 32, admin_bar_height_sm : 46, admin_bar_height_xs : 0, stick_upto_scroll : 0, gutter : 0, wrap : '
', // Padding support of tag. body_padding_support : true, // Padding support of tag. html_padding_support : true, // Added shrink option. shrink : { padding_top : '', padding_bottom : '', }, // Enable sticky on mobile sticky_on_device : 'desktop', header_style : 'none', hide_on_scroll : 'no', }, /* Manage hide on scroll down */ lastScrollTop = 0, delta = 5, navbarHeight = 0, loginPopup = document.querySelector('#ast-hb-account-login-wrap') !== null, should_stick = true, hideScrollInterval; /** * Init * * @since 1.0.0 */ function astExtSticky( element, options ) { this.element = element; this.options = $.extend( {}, defaults, options ); this._defaults = defaults; this._name = pluginName; /* Manage hide on scroll down */ if ( '1' == this.options.hide_on_scroll ) { this.navbarHeight = $(element).outerHeight(); } this.lastScrollTop = 0; this.delta = 5; this.should_stick = true; this.hideScrollInterval = ''; this.init(); } /** * Stick element * * @since 1.0.0 */ astExtSticky.prototype.stick_me = function( self, type ) { var selector = jQuery( self.element ), windowWidth = jQuery( window ).outerWidth(); stick_upto_scroll = parseInt( self.options.stick_upto_scroll ), max_width = parseInt( selector.parent().attr( 'data-stick-maxwidth' ) ), // parseInt( self.options.max_width ), gutter = parseInt( selector.parent().attr( 'data-stick-gutter' ) ), // parseInt( self.options.gutter ). aboveHeaderSelectorValue = gutter; if ( header_builder_active && astraAddon.header_main_shrink ) { // Decrese the top of primary / below as we decrease the min-height of all sticked headers by 20. if ( ( selector.hasClass( 'ast-stick-primary-below-wrapper' ) || ( selector.hasClass( 'ast-primary-header' ) ) ) && 1 == astraAddon.header_above_stick && gutter > 0 ) { gutter = gutter - 10; } var aboveHeaderSelector = document.querySelector('.ast-above-header-bar'); if ( 1 == astraAddon.header_above_stick && null !== aboveHeaderSelector ) { aboveHeaderSelectorValue = aboveHeaderSelector.getBoundingClientRect().height + parseInt( aboveHeaderSelector.parentNode.getAttribute( 'data-stick-gutter' ) ); } } /** * Check window width */ if ( (self.options.sticky_on_device === 'desktop' && jQuery('body').hasClass('ast-header-break-point')) || (self.options.sticky_on_device === 'mobile' && !jQuery('body').hasClass('ast-header-break-point')) ) { self.stickRelease(self); } else { // stick_upto_scroll with negative value enables a sticky by default so rounding up to zero. if ( stick_upto_scroll < 0 ) { stick_upto_scroll = 0; } // Check if the Elementor Motion Effect class present var stcikyHeaderElementor = document.getElementsByClassName('elementor-motion-effects-parent'); var stickyHeaderFlag = stcikyHeaderElementor.length > 0 ? true : false; if ( jQuery( window ).scrollTop() > stick_upto_scroll ) { var fixed_header = selector; if ( header_builder_active ) { var mobile_parent = selector.closest( '.ast-mobile-header-wrap' ); var desktop_parent = selector.closest( '#ast-desktop-header' ); mobile_parent = ( 0 === mobile_parent.length ) ? selector.find( '.ast-mobile-header-wrap' ) : mobile_parent; desktop_parent = ( 0 === desktop_parent.length ) ? selector.find( '#ast-desktop-header' ) : desktop_parent; mobile_parent.find( '.ast-mobile-header-content' ).css( 'top', selector.outerHeight() + gutter ); if ( 'ast-box-layout' == self.options.site_layout ) { var max_width_mobile = jQuery( 'body' ).width(); mobile_parent.find( '.ast-mobile-header-content' ).css( 'width', max_width_mobile ); } else { mobile_parent.find( '.ast-mobile-header-content' ).css( 'width', max_width ); } desktop_parent.find( '.ast-desktop-header-content' ).css( 'top', selector.outerHeight() + gutter ); desktop_parent.find( '.ast-desktop-header-content' ).css( 'width', max_width ); } if ( '1' === self.options.hide_on_scroll ) { self.hasScrolled( self, 'stick' ); }else if ( 'none' == self.options.header_style ) { if ( ! stickyHeaderFlag ) { selector.parent().css( 'min-height', selector.outerHeight() ); } if ( ! document.querySelector('body').classList.contains( 'fl-builder-edit' ) ) { selector.addClass('ast-sticky-active').stop().css({ 'top': gutter, }); } selector.addClass( 'ast-sticky-active' ).stop().css({ 'max-width' : max_width, 'padding-top' : self.options.shrink.padding_top, 'padding-bottom' : self.options.shrink.padding_bottom, }); if ( ( selector.hasClass( 'ast-stick-primary-below-wrapper' ) || selector.hasClass( 'ast-primary-header' ) ) && 1 == astraAddon.header_above_stick && 70 > selector.closest('#ast-desktop-header').find('.ast-above-header-bar').outerHeight() ) { selector.parent().css("min-height", selector.outerHeight()); } selector.addClass( 'ast-sticky-shrunk' ).stop(); $( document ).trigger( "addStickyClass" ); fixed_header.addClass('ast-header-sticked'); }else if ( 'slide' == self.options.header_style ) { fixed_header.css({ 'top' : gutter, }); fixed_header.addClass('ast-header-slide'); fixed_header.css( 'visibility', 'visible' ); fixed_header.addClass( 'ast-sticky-active' ).stop().css({ 'transform': loginPopup ? 'none' : 'translateY(0)', }); $('html').addClass('ast-header-stick-slide-active'); $( document ).trigger( "addStickyClass" ); fixed_header.addClass('ast-header-sticked'); }else if( 'fade' == self.options.header_style ) { fixed_header.css({ 'top' : gutter, }); fixed_header.addClass('ast-header-fade'); fixed_header.css( 'visibility', 'visible' ); fixed_header.addClass( 'ast-sticky-active' ).stop().css({ 'opacity' : '1', }); $('html').addClass('ast-header-stick-fade-active'); $( document ).trigger( "addStickyClass" ); fixed_header.addClass('ast-header-sticked'); } } else { self.stickRelease( self ); if ( header_builder_active ) { var mobile_parent = selector.closest( '.ast-mobile-header-wrap' ); mobile_parent = ( 0 === mobile_parent.length ) ? selector.find( '.ast-mobile-header-wrap' ) : mobile_parent; if ( !jQuery( 'body' ).hasClass( 'ast-primary-sticky-header-active' ) || !jQuery( 'body' ).hasClass( 'ast-above-sticky-header-active' ) || !jQuery( 'body' ).hasClass( 'ast-below-sticky-header-active' ) ) { mobile_parent.find( '.ast-mobile-header-content' ).removeAttr( 'style' ); } } } } } astExtSticky.prototype.update_attrs = function () { var self = this, selector = jQuery( self.element ), gutter = parseInt( self.options.gutter ), max_width = self.options.max_width; if ( 'none' == self.options.header_style && ! jQuery( 'body' ).hasClass( 'ast-sticky-toggled-off' ) ) { var stick_upto_scroll = selector.offset().top || 0; }else{ if ( $('#masthead').length ) { var masthead = $('#masthead'); var masthead_bottom = masthead.offset().top + masthead.outerHeight() + 100; var stick_upto_scroll = masthead_bottom || 0; } } /** * Update Max-Width */ if ( 'ast-box-layout' != self.options.site_layout ) { max_width = jQuery( 'body' ).width(); } /** * Check dependent element * - Is exist? * - Has attr 'data-stick-support' with status 'on' */ if ( self.options.dependent ) { jQuery.each( self.options.dependent, function(index, val) { if ( ( jQuery( val ).length ) && ( jQuery( val ).parent().attr( 'data-stick-support' ) == 'on' ) ) { dependent_height = jQuery( val ).outerHeight(); gutter += parseInt( dependent_height ); stick_upto_scroll -= parseInt( dependent_height ); } }); } /** * Add support for Admin bar height */ if ( self.options.admin_bar_height_lg && jQuery( '#wpadminbar' ).length && viewPortWidth > 782 ) { gutter += parseInt( self.options.admin_bar_height_lg ); stick_upto_scroll -= parseInt( self.options.admin_bar_height_lg ); } if ( self.options.admin_bar_height_sm && jQuery( '#wpadminbar' ).length && ( viewPortWidth >= 600 && viewPortWidth <= 782 ) ) { gutter += parseInt( self.options.admin_bar_height_sm ); stick_upto_scroll -= parseInt( self.options.admin_bar_height_sm ); } if( self.options.admin_bar_height_xs && jQuery( '#wpadminbar' ).length ){ gutter += parseInt( self.options.admin_bar_height_xs ); stick_upto_scroll -= parseInt( self.options.admin_bar_height_xs ); } /** * Add support for tag */ if ( self.options.body_padding_support ) { gutter += parseInt( jQuery( 'body' ).css( 'padding-top' ), 10 ); stick_upto_scroll -= parseInt( jQuery( 'body' ).css( 'padding-top' ), 10 ); } /** * Add support for tag */ if ( self.options.html_padding_support ) { gutter += parseInt( jQuery( 'html' ).css( 'padding-top' ), 10 ); stick_upto_scroll -= parseInt( jQuery( 'html' ).css( 'padding-top' ), 10 ); } /** * Reduce the stick_upto_scrll by one if filter `astra_addon_sticky_header_stick_origin_position` is set true. * This will make the sticky header appear sticky on initial load. */ if ( stick_origin_position ) { stick_upto_scroll--; } /** * Updated vars */ self.options.stick_upto_scroll = stick_upto_scroll; /** * Update Attributes */ if ( 'none' == self.options.header_style ) { selector.parent() .css( 'min-height', selector.outerHeight() ) .attr( 'data-stick-gutter', parseInt( gutter ) ) .attr( 'data-stick-maxwidth', parseInt( max_width ) ); }else{ selector.parent() .attr( 'data-stick-gutter', parseInt( gutter ) ) .attr( 'data-stick-maxwidth', parseInt( max_width ) ); if ( 'ast-padded-layout' === self.options.site_layout ) { selector.css( 'max-width', parseInt( max_width ) ); } } } astExtSticky.prototype.hasScrolled = function( self, method ) { var st = $( window ).scrollTop(); // Make sure they scroll more than delta if(Math.abs(lastScrollTop - st) <= delta) return; // If they scrolled down and are past the navbar, add class .nav-up. // This is necessary so you never see what is "behind" the navbar. var fixed_header = jQuery(self.element); if (st > lastScrollTop && st > navbarHeight){ // Scroll Down jQuery(self.element).removeClass('ast-nav-down').addClass('ast-nav-up'); } else { // Scroll Up if(st + $(window).height() < $(document).height()) { jQuery(self.element).removeClass('ast-nav-up').addClass('ast-nav-down'); } } lastScrollTop = st; if ( !$(self.element).hasClass('ast-nav-up') && 'stick' == method ) { fixed_header.css({ 'top' : gutter, }); fixed_header.addClass('ast-header-sticked'); fixed_header.addClass('ast-header-slide'); fixed_header.css( 'visibility', 'visible' ); fixed_header.addClass( 'ast-sticky-active' ).stop().css({ 'transform':'translateY(0)', }); $( document ).trigger( "addStickyClass" ); $('html').addClass('ast-header-stick-scroll-active'); }else{ fixed_header.css({ 'transform':'translateY(-100%)', }).stop(); setTimeout(function() { fixed_header.removeClass( 'ast-sticky-active' ); }, 300); fixed_header.css({ 'visibility' : 'hidden', 'top' : '', }); $( document ).trigger( "removeStickyClass" ); $('html').removeClass('ast-header-stick-scroll-active'); fixed_header.removeClass('ast-header-sticked'); } } astExtSticky.prototype.stickRelease = function( self ) { var selector = jQuery( self.element ); var fixed_header = selector; if ( '1' === self.options.hide_on_scroll ) { self.hasScrolled( self, 'release' ); }else{ if ( 'none' == self.options.header_style ) { selector.removeClass( 'ast-sticky-active' ).stop().css({ 'max-width' : '', 'top' : '', 'padding' : '', }); selector.parent().css( 'min-height', '' ); $( document ).trigger( "removeStickyClass" ); fixed_header.removeClass('ast-header-sticked'); selector.removeClass( 'ast-sticky-shrunk' ).stop(); }else if ( 'slide' == self.options.header_style ) { fixed_header.removeClass( 'ast-sticky-active' ).stop().css({ 'transform': loginPopup ? 'translateY(-100vh)' : 'translateY(-100%)', }); fixed_header.css({ 'visibility' : 'hidden', 'top' : '', }); $('html').removeClass('ast-header-stick-slide-active'); $( document ).trigger( "removeStickyClass" ); fixed_header.removeClass('ast-header-sticked'); }else if( 'fade' == self.options.header_style ) { fixed_header.removeClass( 'ast-sticky-active' ).stop().css({ 'opacity' : '0', }); fixed_header.css({ 'visibility' : 'hidden', }); fixed_header.removeClass('ast-header-sticked'); $( document ).trigger( "removeStickyClass" ); $('html').removeClass('ast-header-stick-fade-active'); } } } /** * Init Prototype * * @since 1.0.0 */ astExtSticky.prototype.init = function () { /** * If custom stick options are set */ if ( jQuery( this.element ) ) { var self = this, selector = jQuery( self.element ); /** * Add parent