Shrinking sticky header on scroll
-
Firstly, congrats on such a fantastic theme! What I love about it is it’s so flexible.
I managed to add a sticky header and now I want the header to shrink as you scroll down. Here is the CSS:
header { width: 100%; height: 200px; overflow: hidden; position: fixed; left: 0; z-index: 999; background: transparent!important; -webkit-transition: height 0.3s; -moz-transition: height 0.3s; -ms-transition: height 0.3s; -o-transition: height 0.3s; transition: height 0.3s; } header .shrink { width: 100%; height: 50px; overflow: hidden; position: fixed; left: 0; z-index: 9999; background: #fff!important; -webkit-transition: height 0.3s; -moz-transition: height 0.3s; -ms-transition: height 0.3s; -o-transition: height 0.3s; transition: height 0.3s; } header.shrink #thelogo { display:none; }
And here is the Javascript code:
jQuery(document).ready(function($) { $(window).scroll(function () { if ($(window).scrollTop() > 100) { $('header').addClass('shrink'); } else{ $('header').removeClass('shrink'); } }); });
I enqueued the script in my child theme functions.php with:
‘function virtue_child_load_shrink_header() {
wp_register_script( ‘shrink-header’, get_stylesheet_directory_uri() . ‘/js/shrink-header.js’, array(‘jquery’), ‘1.0’, true );
wp_enqueue_script( ‘shrink-header’ );
}
add_action( ‘wp_enqueue_scripts’, ‘virtue_child_load_shrink_header’ );`There were no errors and when I scroll down, the logo disappears as intended. However, the header does not change in height from 200px to 50px (used a smaller number just for testing), and the background does not change to #fff.
What am I doing wrong? Your help would be appreciated, thanks! ??
- The topic ‘Shrinking sticky header on scroll’ is closed to new replies.