I use a secondary fille called custom.css to overwrite the bootstrap code and I would like to know how to create a code that is activating only when the visitor of my site i
To avoid the performance hit of using the scroll, load and resize events, you can now use the Intersection Observer API.
It will allow you to detect if the content on your page has been scrolled, and set the nav bar transparency accordingly (by adding or removing a class).
Have a look at this answer for more details.
$(document).ready(function() {
$(window).scroll(function() {
if($(this).scrollTop() > height) {
$('.navbar').addClass('scrolled');
} else {
$('.navbar').removeClass('scrolled');
}
});
});
Ok you need the following code to achieve this effect: (I am going to use jQuery as it is the bootstrap supported language).
jQuery:
/**
* Listen to scroll to change header opacity class
*/
function checkScroll(){
var startY = $('.navbar').height() * 2; //The point where the navbar changes in px
if($(window).scrollTop() > startY){
$('.navbar').addClass("scrolled");
}else{
$('.navbar').removeClass("scrolled");
}
}
if($('.navbar').length > 0){
$(window).on("scroll load resize", function(){
checkScroll();
});
}
You can also use ScrollSpy
to do this.
and your CSS (example):
/* Add the below transitions to allow a smooth color change similar to lyft */
.navbar {
-webkit-transition: all 0.6s ease-out;
-moz-transition: all 0.6s ease-out;
-o-transition: all 0.6s ease-out;
-ms-transition: all 0.6s ease-out;
transition: all 0.6s ease-out;
}
.navbar.scrolled {
background: rgb(68, 68, 68); /* IE */
background: rgba(0, 0, 0, 0.78); /* NON-IE */
}