Bootstrap 4 Animated header

I’m not sure if I should post this here or in the Javascript section. So excuse me if this is not the right place. I try to create an animated header (From Transparant To Solid white + Sliding down) For this to happen I have included Bootstrap.css, Animate.css as well as jQuery.js and Bootstrap.js as you can see below:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Untitled Document</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/4.1.3/cerulean/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.min.css">

<style>
body {
	background: #000;
	height: 3000px;	
}
.navbar-light {
	background-color: RGB(255, 255, 255) !important;		
}

.navbar-light .navbar-brand,
.navbar-light .navbar-nav .nav-link
 {
	color: RGB(85, 85, 85) !important;	
}

.navbar-transparant {
	background-color: transparent !important;
	border-color: RGBA (255, 255, 255, .4) 	
}

.navbar-transparant .navbar-brand,
.navbar-transparant .navbar-nav .nav-link
 {
	color: RGB(255, 255, 255) !important;	
}

</style>
</head>

<body>
<nav class="navbar navbar-expand-md navbar-transparant fixed-top animated fadeIn">
	<div class="container">
		<a class="navbar-brand" href="/admin/">Titel</a>
        
        <div class="collapse navbar-collapse" id="topNav">
        	<ul class="navbar-nav ml-auto">
            	<li class="nav-item">
                	<a href="/" class="nav-link">Home</a>
                </li>
            </ul>
        </div>
    </div>
</nav>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" ></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script>	
	var element	= 	$('.navbar');
	$(window).on("scroll", function () {
		if ($(this).scrollTop() > 300) {
			$(element).addClass('.navbar-light');
			$(element).removeClass('.navbar-transparant');	
			$(element).addClass('.fadeInDown');
		} else {
			$(element).removeClass('.navbar-light');
			$(element).addClass('.navbar-transparant');	
			$(element).removeClass('.fadeInDown');			
		}
	});
</script>
</body>
</html>

I furthermore declared the css properties for .navbar-light and .navbar-transparant.
But for some reason the add and remove classes are not executing. I quess I am looking at it to long allready because nomatter what I have tried nothing seems to help. Does anybody see what I am dong wrong.

Thank you in advance

Hi there donboe,

your script should be…

<script>	
	var element	= 	$('.navbar');
	$(window).on("scroll", function () {
		if ($(this).scrollTop() > 300) {
			$(element).addClass( 'navbar-light' );
			$(element).removeClass( 'navbar-transparant' );	
			$(element).addClass( 'fadeInDown' );
		} else {
			$(element).removeClass( 'navbar-light' );
			$(element).addClass( 'navbar-transparant' );	
			$(element).removeClass( 'fadeInDown' );			
		}
	});
</script>

coothead

3 Likes

@coothead Those dammed dots :banghead: Thanks a lot for your attention

1 Like

 
 
        No problem, you’re very welcome. :winky:
 
 
        coothead

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.