Reputation: 485
Using the mdbootstrap I have navbar:
<nav class="navbar navbar-expand-lg white">
And it is positioned like this:
While scrolling I add the 'fixed-top' class to the navbar with this script:
let nav = $('.navbar').offset().top;
$(window).bind('scroll', function () {
if ($(window).scrollTop() >= nav) {
$('.navbar').addClass('fixed-top');
} else {
$('.navbar').removeClass('fixed-top');
}
});
This works but: I want to animate the transition between the original and the fixed-top state. I tried with setting a transition time to .navbar{} but it does not work. Do I need to wrap the navbar into another div?
Upvotes: 0
Views: 779
Reputation: 2093
let nav = $('.navbar').offset().top;
Make a variable to store the offset and then update the condition for setting .fixed-top
if ($(window).scrollTop() >= nav) {
$('.navbar').addClass('fixed-top');
}
To animated:
Add a new class along with .navbar-default
, say animate
.animate{
transition: top 1s ease-in-out;
}
it will add an ease-in-out animation to the navbar.
1s
is the time for animation, you can increase or decrease depending on your requirement.
Upvotes: 3