I would like to build a navigation-bar effect like it is on http://dootrix.com/ on my page (after scrolling down the bar getting smaller and the logo changes). Im using bootstrap 3 for my page. Is there an easy way to realize it with bootstrap?
This question is related to
css
twitter-bootstrap
twitter-bootstrap-3
You can combine "scroll" and "scrollstop" events in order to achieve desired result:
$(window).on("scroll",function(){
$('nav').addClass('shrink');
});
$(window).on("scrollstop",function(){
$('nav').removeClass('shrink');
});
For those not willing to use jQuery here is a Vanilla Javascript way of doing the same using classList:
function runOnScroll() {
var element = document.getElementsByTagName('nav') ;
if(document.body.scrollTop >= 50) {
element[0].classList.add('shrink')
} else {
element[0].classList.remove('shrink')
}
console.log(topMenu[0].classList)
};
There might be a nicer way of doing it using toggle, but the above works fine in Chrome
I am using this code for my project
$(window).scroll ( function() {
if ($(document).scrollTop() > 50) {
document.getElementById('your-div').style.height = '100px'; //For eg
} else {
document.getElementById('your-div').style.height = '150px';
}
}
);
Probably this will help
toggleClass works too:
$(window).on("scroll", function() {
$("nav").toggleClass("shrink", $(this).scrollTop() > 50)
});
If you are using AngularJS, and you are using Angular Bootstrap : https://angular-ui.github.io/bootstrap/
You can do this so nice like this :
HTML:
<nav id="header-navbar" class="navbar navbar-default" ng-class="{'navbar-fixed-top':scrollDown}" role="navigation" scroll-nav>
<div class="container-fluid top-header">
<!--- Rest of code --->
</div>
</nav>
CSS: (Note here I use padding as bigger nav to shrink without padding you can modify as you want)
nav.navbar {
-webkit-transition: all 0.4s ease;
transition: all 0.4s ease;
background-color: white;
margin-bottom: 0;
padding: 25px;
}
.navbar-fixed-top {
padding: 0;
}
And then add your directive
Directive: (Note you may need to change this.pageYOffset >= 50
from 50 to more or less to fulfill your needs)
angular.module('app')
.directive('scrollNav', function ($window) {
return function(scope, element, attrs) {
angular.element($window).bind("scroll", function() {
if (this.pageYOffset >= 50) {
scope.scrollDown = true;
} else {
scope.scrollDown = false;
}
scope.$apply();
});
};
});
This will do the job nicely, animated and cool way.
Source: Stackoverflow.com