I'm trying to do something simple. I have a nav bar and I want it to change the background color when scrolling. I can't get it to work. Below is my code.
I've got this in my head section of my html.
<script type="text/javascript" src="/js/jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="/js/HeaderScroll.js"></script>
Here is the Javascript. I tried putting a ready function around it, but that didn't work either.
$(function() {
$(window).on("scroll", function() {
if($(window).scrollTop() > 50) {
$(".navbar").addClass("active");
} else {
//remove the background property so it comes transparent again (defined in css)
$(".navbar").removeClass("active");
}
}); });
Here is my CSS.
/* nav */
.navbar {
right: 0;
left: 0;
margin: 0 auto;
position: fixed;
width: 95%;
background-color: transparent;
}
.navbar .active {
background: #000;
}
Here is the head section of my html. I changed the link for jquery to the Google hosted one.
<!doctype html>
<html <?php language_attributes(); ?> class="no-js">
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<title><?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' :'; } ?> <?php bloginfo('name'); ?></title>
<link href="//www.google-analytics.com" rel="dns-prefetch">
<link href="<?php echo get_template_directory_uri(); ?>/img/icons/favicon.ico" rel="shortcut icon">
<link href="<?php echo get_template_directory_uri(); ?>/img/icons/touch.png" rel="apple-touch-icon-precomposed">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="<?php bloginfo('description'); ?>">
<?php wp_head(); ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript" src="/js/HeaderScroll.js"></script>
<script>
// conditionizr.com
// configure environment tests
conditionizr.config({
assets: '<?php echo get_template_directory_uri(); ?>',
tests: {}
});
</script>
</head>