Bootstrap navbar turns grey when clicked after applying onclick - html

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.

Related

Change side bar bootstrap text color from text-light to text-dark on click using jQuery

Help with adding jQuery that will remove text-light when the side menu is open or when the navbar-toggler button is clicked.
window.onload = function() {
if (window.jQuery) {
$(document).ready(function() {
$(".sidebarNavigation .navbar-collapse").hide().clone().appendTo("body").removeAttr("class").addClass("sideMenu").show();
$("body").append("<div class='overlay'></div>");
$(".navbar-toggle, .navbar-toggler").on("click", function() {
$(".sideMenu").addClass($(".sidebarNavigation").attr("data-sidebarClass"));
$(".sideMenu, .overlay").toggleClass("open");
$(".overlay").on("click", function() {
$(this).removeClass("open");
$(".sideMenu").removeClass("open")
})
});
$("body").on("click", ".sideMenu.open .nav-item", function() {
if (!$(this).hasClass("dropdown")) {
$(".sideMenu, .overlay").toggleClass("open")
}
});
$(window).resize(function() {
if ($(".navbar-toggler").is(":hidden")) {
$(".sideMenu, .overlay").hide()
} else {
$(".sideMenu, .overlay").show()
}
})
})
} else {
console.log("sidebarNavigation Requires jQuery")
}
}
<nav class="navbar navbar-expand-md bg-body navbar-dark sidebarNavigation" data-sidebarClass="navbar-dark bg-light ">
<a class="navbar-brand" href="index.html"><img src="Media/David-Sanders-Logo-Transparent-White-Writing.png" width="210wh" class="img-fluid" alt="Responsive logo"></a>
<!-- Toggler/collapsibe Button -->
<button class="navbar-toggler" type="button" data-toggle="collapse">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Navbar links -->
<div class="collapse navbar-collapse justify-content-center sidenav">
<ul class="navbar-nav">
<li class="nav-item">Home</li>
<li class="nav-item">Portfolio</li>
<li class="nav-item">About</li>
<li class="nav-item">Services</li>
<li class="nav-item">Skills</li>
<li class="nav-item">Contact</li>
</ul>
</div>
</nav>
It would be helpful if this could be implemented into the current jQuery code.
I first took away the text-light class from all list items.
<li class="nav-item">Contact</li>
Then I was able to achieve the desired result by accessing the nav in css and overriding the bootstrap.
nav .navbar-nav li a{
color: white;
}
The problem is solved but not in jQuery which is what I wanted, however, either way achieves the same outcome.

How to add images for backgrounds in each section of a one-page scrollable webpage?

I'm doing the FreeCodeCamp web dev course, and I'm stuck on making my profile page. The website needs to have more than 3 sections and should have only one page. What I wanted is a different image as a background for each. So far, I have only been able to add the first image and text, and have smooth scrolling. I have been trying to add the other images since weeks now, but it just doesn't work.
Also, I want the text to stay at the same place, but the images to scroll, which again isn't happening.
I'm using codepen.io and Bootstrap. I will add a link of my pen so that you guys can see the code.
I'm not sure if this is the correct way to ask the question, but I have added all the information I can provide. Thanks in advance!
Link to me pen: http://codepen.io/AnInternetUser/pen/LNKGMY
Code:
$(document).ready(function() {
$('.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
}, 750);
return false;
}
}
});
});
body {
padding-top: 70px;
}
.hello {
padding-top: 70px;
background-image: url('http://res.cloudinary.com/aninternetuser/image/upload/v1463638974/hd-wallpaper-40_cdz9co.jpg');
repeat: no-repeat;
background-size: cover;
}
.hello h1 {
font-family: Oswald;
text-align: center;
color: white;
font-size: 125px;
}
.hello h3 {
font-family: Oswald;
text-align: center;
color: white;
}
.jumbotron {
background-color: grey;
}
.experience {
background-image: url('http://res.cloudinary.com/aninternetuser/image/upload/v1463648167/desktop-desktops-highway-night-photo-hdwallpapers2016com_hcpltl.jpg');
repeat: no-repeat;
background-size: cover;
}
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container-fluid">
<!-- 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" aria-expanded="false">
<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="#">Krunal Rindani</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<ul class="nav navbar-nav navbar-right">
<li>
Home
</li>
<li>
Projects
</li>
<li>
About
</li>
<li>
Contact
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class="hello" id="home" style="min-height:1000px;min-width:1000px;">
<h1></h1>
<h1></h1>
<h1>Hello.</h1>
<div class="jumbotron">
<h3>This is my portfolio website. You can find my contact details and past work here. You can also learn more about me here!</h3>
</div>
</div>
<div id="projects" class="experience" sytle="min-height:1000px;min-width:1000px;">
<h2>This area will be updated with all the web projects done by me. Stay tuned!</h2>
</div>
<div id="about" style="min-height:1000px;">
</div>
<div id="contact" style="min-height:1000px;">
</div>
</body>
</html>

Bootstrap Fixed Navigation and Parallax: Highlight does not works

i'm working on this website builted using Bootstrap.
As you can see i've a Fixed Top Nav Bar and the Homepage is a "One Scroll" page. What i need is to highlight this 2 anchorpoints when they are active:
<li><a class="page-scroll" href="#ark">Architektur</a></li>
<li><a class="page-scroll" href="#ausstattung">Ausstattung</a></li>
With this CSS Style:
background-color: #ffffff;
color: #009ee0 !important;
I tried with this with no results.
Any tips?
EDIT:
I just added this:
<body data-spy="scroll" data-target=".navbar" data-offset="50">
And works, ut as soon i click on
<li><a class="page-scroll" href="#ark">Architektur</a></li>
<li><a class="page-scroll" href="#ausstattung">Ausstattung</a></li>
the alink has a Grey Background. But as soon i click somewehere then looks perfect. Is a JS problem?
just add this in your css
.navbar-default .navbar-nav > .active > a, .navbar-default .navbar-nav > .active > a:focus, .navbar-default .navbar-nav > .active > a:hover {
background: yellow !important;}
and you are done,
Ok so there are a few things to address here with your code. First you need to add the body scrollspy tag. So it will look like this:
<body data-spy="scroll" data-target="#navbar" data-offset="50">
Then you have the id #navbar stated twice. If you resize your window to a mobile size you will see that your collapsable menu doesn't collapse. This is because you have your data-target set to #navbar and you have this stated twice. It doesn't have anything to do with scrollspy but I figured I would give you a heads up anyway to save you a future headache and we will set your button to target the .navbar-collapse. Then remove the class of page scroll from everything except the href's that have an anchor tag in it. Long story short your navbar should look like the following:
<nav id="navbar" class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse" aria-expanded="false" aria-controls="navbar">
<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">
<ul class="nav navbar-nav">
<li><a class="selected" href="http://develop.nowcommu.myhostpoint.ch/">Home</a></li>
<li><a class="page-scroll" href="#ark">Architektur</a></li>
<li><a class="page-scroll" href="#ausstattung">Ausstattung</a></li>
<li>Wohnungen</li>
<li>Lage</li>
<li>Galerie</li>
<li>Kontakt</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
Then I always like to give the following jquery code because it gives you more control over the scrollspy plugin like how fast the animation is and better control over the top offset so add this as well:
$(".navbar a.page-scroll").on('click', function(event){
event.preventDefault();
var hash = this.hash;
$('html, body').animate({
scrollTop: $(hash).offset().top - 50
}, 800, function(){
window.location.hash = hash;
});
});
So now your body is targeting the #navbar and your collapsable menu will work and your markup will be correct.
Here is a fiddle to show you this working Fiddle

how can I change the background after scroll

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. :)

how do I change the background of my active <li> in twitter bootstrap nav

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
}