Advertisement
Guest User

Tower Theme - Sticky Header Fix

a guest
Jan 7th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. /**
  2.  * Enqueue theme's main script.
  3.  * Hook it in late to make sure any dependencies have been loaded.
  4.  */
  5. function tower_child_enqueue_theme_script() {
  6.     wp_enqueue_script('tower-child', get_stylesheet_directory_uri() . '/assets/js/theme.js', ['jquery'], false, true);
  7.  
  8.     $header_position = ( is_front_page() ) ? 'absolute' : 'relative';
  9.  
  10.     /**
  11.      * STICKY HEADER ENFORCEMENT (BUG FIX)
  12.      */
  13.     $custom_js = "
  14.     jQuery(document).ready(function () {
  15.         'use strict';
  16.  
  17.         //const header = jQuery('#header');
  18.         const header_wrapper = jQuery('.header_wrapper');
  19.         const body = jQuery(document.body);
  20.  
  21.         window.onscroll = function () {
  22.             var top = window.scrollY;
  23.             if (top >= 120) {
  24.                 body.addClass('sticky_header');
  25.                 header_wrapper.addClass('open');
  26.                 header_wrapper.css({'position': 'fixed', 'visibility': 'visible'});
  27.             }
  28.             else {
  29.                 body.removeClass('sticky_header');
  30.                 header_wrapper.removeClass('open');
  31.                 header_wrapper.css({'position': '{$header_position}', 'visibility': 'visible'});
  32.             }
  33.         }
  34.     });
  35.    ";
  36.  
  37.     wp_add_inline_script( 'tower-child', $custom_js );
  38. }
  39. add_action('wp_enqueue_scripts', 'tower_child_enqueue_theme_script', 100);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement