Add CSS Class To Nav When User Scrolls

When user is at the top of the page, the Site Header is transparent. When the user scrolls down, the Site Header has the class of 'dark' added, and can be styled with a dark background.

This is from the StudioPress Altitude Pro theme. When user is at the top of the page, the site-header is transparent. When the user scrolls the page, the ‘dark’ class is added to the site-header div, allowing you to style the .site-header.dark element as you see fit.

jQuery(function( $ ){
	if( $( document ).scrollTop() > 0 ){
		$( '.site-header' ).addClass( 'dark' );			
	}
	// Add opacity class to site header
	$( document ).on('scroll', function(){
		if ( $( document ).scrollTop() > 0 ){
			$( '.site-header' ).addClass( 'dark' );			
		} else {
			$( '.site-header' ).removeClass( 'dark' );			
		}
	});
});

Hire a WordPress Expert

Get hourly and project based theme customization, theme tweaks, and development help from an experienced Kelowna WordPress developer.  We’ve been building WordPress websites full-time for over a decade. We can help you.