I have a navbar with background transparent. How can make background black after scrol.
Here is template live
and there is html with navbar
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand"><img src="img/logo.svg" class="brand"></a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li>Despre noi</li>
<li>Servicii</li>
<li>Galerie</li>
<li>Contact</li>
</ul>
</div>
</div>
</nav>
Do it with Jquery and add a class
jQuery
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 1) { // Adjust with your needs
$(".navbar").addClass("navbar-black");
} else {
$(".navbar").removeClass("navbar-black");
}
});
CSS
.navbar-black{
background: #000 !important;
}
so you should be able to achieve this with ScrollTop,
Something like:
onscroll = function()
{
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if (scrollTop > 830)
{
$('.navbar').css('background-color','black');
}
if (scrollTop < 830)
{
$('.navbar').css('background-color','transparent');
}
}
I would perhaps consider using an anchor rather than an absolute height.
If this is what you wanted. :)
Related
i added at my hoe page a navbar with toggable icon on small screen; It is the typical navbar of bootstrap.
I have the problem that when i go on developer tool and resize the screen in order to test the responsive part, happen that the icon doesn't toggle, it is like is not active.
Any help?
Here is the code:
<nav id="header" class="navbar navbar-default">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navmenu" aria-expanded="false" aria-controls="navmenu">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><img id="logo" src="images/art-of-hair.png"></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div id="navmenu" class="collapse navbar-collapse navbar-right">
<ul class="nav navbar-nav">
<li class="active">Home<span class="sr-only">Home</span></li>
<li>Hair Styles</li>
<li class="">About</li>
<li class="">Contact</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
and css:
* {
margin: 0;
}
body {
font-family: 'Alice', serif;
}
#header {
height:200px;
}
#logo {
width: 300px;
height:150px;
}
#navmenu {
margin:30px 120px 0 0;
}
In case of toggle, I guess you also need some jQuery to make the toggle function.
Maybe try something like this:
<script>
$("button").click(function(){
$("#navmenu").slideToggle();
});
</script>
I have done the code for navbar as follows.
<div class="header-bottom ">
<div class="container">
<nav class="navbar navbar-default">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="logo grid">
<div class="grid__item color-3">
<h1><a class="link link--nukun" href="index.html"><i></i><span>PRISM</span></a></h1>
</div>
</div>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse nav-wil" id="bs-example-navbar-collapse-1">
<nav class="menu menu--horatio">
<ul class="nav navbar-nav menu__list">
<li class="menu__item menu__item--current">Home</li>
<li class="menu__item">About</li>
<li class="menu__item">Short Codes</li>
<li class="menu__item">Gallery</li>
<li class="menu__item">Contact</li>
</ul>
</nav>
</div>
</nav>
</div>
</div>
the output of code is This is output for above code
i want to make only navbar as fixed please help to do so.
Are you using Bootstrap? If so, change
<nav class="navbar navbar-default">
to
<nav class="navbar navbar-default navbar-fixed-top">
Here you have an Example.
If not using bootstrap:
.navbar-fixed-top {
position: fixed;
right: 0;
left: 0;
top: 0;
z-index: 1030;
}
Edit:
I missunderstood your question.
You should use affix. And take away the navbar-fixed-top from my previous comment
$('#nav').affix({
offset: {
top: $('header').height()
}
})
where #nav is the nav that you want to get fixed, and .heading is the first nav or heading. So when you scroll the height of the first one, the second one will be fixed.
My navbar worked just fine before I added an onclick function.
My onclick function looks like this:
$('.nav li a').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
&& location.hostname == this.hostname) {
var $target = $(this.hash);
$target = $target.length && $target
|| $('[name=' + this.hash.slice(1) +']');
if ($target.length) {
var targetOffset = $target.offset().top;
$('html,body')
.animate({scrollTop: targetOffset}, 2000);
return false;
}
}
});
When I click on one of my items in my navbar it does the onclick function, but when I hover my mouse outside of the navbar item that I just clicked on, it turns grey. When I click on anything after that it removes the grey color again.
My navbar looks like this:
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<img src="../Images/Logo.png" width=200px" height="50px"/>
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li><a class="home" href="#home">Home</a></li>
<li>My Projects</li>
<li>Contact</li>
</ul>
</div>
</div>
The onclick event is adding a focus pseudo class to your li > a element. Until you click away bootstrap is styling the nav item with the following css:
.navbar-default .navbar-nav>li>a:focus, .navbar-default .navbar-nav>li>a:hover {
color: #333;
background-color: transparent;
}
You could overwrite the :focus styling or put this jquery: $(this).blur(); at the end of your function.
I am facing an issue with Bootstrap 3. I have a single page navigation with sections and a fix after scroll navbar. As it scrolls the navbar gets fixed and changes background color. However when it comes to small screens the navbar isnt collapsing and it doesnt remain fixed.
This is part of the html.
<div class="navigate clearfix">
<div class="inner">
<h3 class="navigate-brand">Brand</h3>
<nav class="navbar" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#menu" >
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="menu">
<ul class="nav navigate-nav menu-right" >
<li class="active">Home</li>
<li>s1</li>
<li>S2</li>
<li>s3</li>
<li>s4</li>
</ul>
</div>
</nav>
</div>
</div>
The js with the background change:
$(document).ready(function(){
$(window).scroll(function(){
if($(window).scrollTop() > $(window).height()){
$(".navigate").css({"background-color" : "red"});
}else{
$(".navigate").css({"background-color":"transparent","background":"transparent"});
}
})
})
You may want to follow the instructions in: https://getbootstrap.com/components/#navbar
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
...
</div>
</nav>
Based on the above, I have prepared a fiddle for you where navbar remains fixed at any given width of screen: http://jsfiddle.net/dmv73fzr/
Hope it helps you!
Ok so I have a standard twitter bootstrap nav and I just wanted to change the background color of the active link when it is selected such as Home or Contact us.. Rite now if you are in a certain page the background of the li in the navigation bar is just plain black to demonstrate that it is selected. I wanted it to be blue.
Here is the code:
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" href="#">Logo</a>
<div class="nav-collapse collapse text-center">
<ul class="nav pull-right">
<li class="active" id="active">Home</li>
<li>About</li>
<li>Events</li>
<li>Contact</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
I hear you would put something like .nav > li > a:active {background:blue} but that doesn't seem to work
a:active
Is wrong for what you're trying to achieve
The :active pseudo selector - A link becomes active when you click on it.
Try
.nav .active { background:blue }
The > symbol will select the direct child element.
try this.
<style>
#home.active, #contract.active{
background-color : blue
}
</style>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" href="#">Logo</a>
<div class="nav-collapse collapse text-center">
<ul class="nav pull-right">
<li class="active" id="home">Home</li>
<li>About</li>
<li>Events</li>
<li id="contract">Contact</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
Add following javascript to update active tab by highlight
$('ul.nav > li').click(function (e) {
e.preventDefault();
$('ul.nav > li').removeClass('active');
$(this).addClass('active');
});
I had to target the a element inside of the active li for the color to change
.active a {
background-color: #FFFFFF !important;
}
.nav-fixed=top>li.active>a
{
//styling
}