Reputation: 457
Hello guys I'm trying to show you hide and nailed above navbar, I got it showing, but his animation it's not working while hiding .. Someone may help me fix it?
Check it out on fiddle
// CSS
.margin-top-0 { margin-top: 0px !important; }
.margin-top-20 { margin-top: 20px !important; }
.margin-top-40 { margin-top: 40px !important; }
.alert-server {
border-radius: 0;
position:fixed;
top:0;
width:100%;
padding:10px 0;
text-align:center;
display:none;
}
// Shows #notificationBar
setTimeout(function () {
// working
$("#notificationBar").css({"display":"block"});
$('#notificationBar').html('Hello User! Welcome!').addClass('margin-top-0', 2000);
$('body, .navbar').addClass('margin-top-40', 2000);
}, 2000);
// Hides #notificationBar
setTimeout(function () {
// not working
$('#notificationBar').removeClass('margin-top-0', 2000).html().css({"display":"none"});
$('body, .navbar').removeClass('margin-top-40', 2000);
}, 6000);
// HTML
<body>
<div id="notificationBar" class="alert alert-success alert-server" role="alert" style="display: none; overflow: hidden; z-index: 9999; margin-top: -40px;">
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<div class="navbar-spacer" style="min-height:60px;"></div>
<!-- Fixed navbar -->
<nav class="website-nav navbar navbar-default navbar-fixed-top">
<div class="navbar-inner">
<div class="nav-center">
</div>
</div>
</nav>
Upvotes: 0
Views: 1220
Reputation: 21
showAlert = function() {
$('body').addClass('alert-show');
}
hideAlert = function() {
$('body').removeClass('alert-show');
}
// auto show
setTimeout(function() {
showAlert();
}, 1000);
// auto hide
setTimeout(function() {
hideAlert();
}, 5000);
@import "http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css";
BODY {
min-height: 75rem;
padding-top: 4.5rem;
transition: padding .5s ease-in-out;
}
.navbar.fixed-top {
transition: top .5s ease-in-out;
}
.alert {
position: fixed;
width: 100%;
top: -50px;
transition: top .5s ease-in-out;
}
BODY.alert-show {
padding-top: calc(4.5rem + 50px);
}
BODY.alert-show .alert {
top: 0;
}
BODY.alert-show .navbar.fixed-top {
top: 50px;
}
<div class="alert alert-success mb-0 text-center" role="alert">
I'm going to disapear in 5000ms
</div>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="#">Fixed navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline mt-2 mt-md-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<main role="main" class="container">
<div class="jumbotron">
<h1>Navbar example</h1>
<p class="lead">This example is a quick exercise to illustrate how fixed to top navbar works. As you scroll, it will remain fixed to the top of your browser's viewport.</p>
<a class="btn btn-lg btn-primary" href="../../components/navbar/" role="button">View navbar docs »</a>
</div>
</main>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
Upvotes: 0
Reputation: 5088
Here is a basic solution.
Original non-fixed .navbar
version http://jsfiddle.net/joshmoto/vr4xto0a/1/
With the fixed navbar, I have had to take a slightly different approach by adding the class on the body instead to reveal the alert as well animate the navbar position and increase the body padding.
See new .navbar.fixed-top
version http://jsfiddle.net/joshmoto/1a620ho8/
showAlert = function() {
$('body').addClass('alert-show');
}
hideAlert = function() {
$('body').removeClass('alert-show');
}
// auto show
setTimeout(function() {
showAlert();
}, 1000);
// auto hide
setTimeout(function() {
hideAlert();
}, 5000);
@import "http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css";
BODY {
min-height: 75rem;
padding-top: 4.5rem;
transition: padding .5s ease-in-out;
}
.navbar.fixed-top {
transition: top .5s ease-in-out;
}
.alert {
position: fixed;
width: 100%;
top: -50px;
transition: top .5s ease-in-out;
}
BODY.alert-show {
padding-top: calc(4.5rem + 50px);
}
BODY.alert-show .alert {
top: 0;
}
BODY.alert-show .navbar.fixed-top {
top: 50px;
}
<div class="alert alert-success mb-0 text-center" role="alert">
I'm going to disapear in 5000ms
</div>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="#">Fixed navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline mt-2 mt-md-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<main role="main" class="container">
<div class="jumbotron">
<h1>Navbar example</h1>
<p class="lead">This example is a quick exercise to illustrate how fixed to top navbar works. As you scroll, it will remain fixed to the top of your browser's viewport.</p>
<a class="btn btn-lg btn-primary" href="../../components/navbar/" role="button">View navbar docs »</a>
</div>
</main>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
Upvotes: 1