/* global miMain */ var MonsterInsightsTheme = window.MonsterInsightsTheme || {}; MonsterInsightsTheme.FloatHeader = MonsterInsightsTheme.FloatHeader || (function ( document, window, $ ) { 'use strict'; /** * Elements. * * @since 2.3.0 * * @type {Object} */ var $el = { markup : $( '#mi-floatheader-markup' ), }; /** * Public functions and properties. * * @since 2.3.0 * * @type {Object} */ var app = { /** * Start the engine. DOM is not ready yet, use only to init something. * * @since 2.3.0 */ init: function () { // Do that when DOM is ready. $( document ).ready( app.ready ); }, /** * DOM is fully loaded. * * @since 2.3.0 */ ready: function () { app.addMarkup(); app.bindEvents(); }, /** * Add markup to DOM. * * @since 2.3.0 */ addMarkup: function () { var html = $el.markup.html(); $el.markup.remove(); $( 'body' ).prepend( html ); $el.floatheader = $( '#mi-floatheader' ); }, /** * Work with events. * * @since 2.3.0 */ bindEvents: function () { $( window ).scroll( app.debounce( function() { var scroll = $( window ).scrollTop(), isSticky = $el.floatheader.hasClass( 'sticky' ); if ( scroll >= 200 && ! isSticky ) { $el.floatheader.addClass( 'sticky' ); } if ( scroll < 200 && isSticky ) { $el.floatheader.removeClass( 'sticky' ); } }, 50 ) ); }, /** * Debounce. * * @since 2.3.0 */ debounce: function( func, wait, immediate ) { var timeout; return function() { var context = this, args = arguments; var later = function() { timeout = null; if ( !immediate ) { func.apply( context, args ); } }; var callNow = immediate && !timeout; clearTimeout( timeout ); timeout = setTimeout( later, wait ); if ( callNow ) { func.apply( context, args ); } }; } }; // Provide access to public functions/properties. return app; } )( document, window, jQuery ); // Initialize. MonsterInsightsTheme.FloatHeader.init();