I'm practicing jQuery by making a simple navigation. There are 4 main tabs. Each has its own hover color, i.e., little dark from the main navigation bar. I have used CSS for this thing. But I want to change the tab color when I click it, and on clicking again, I want it to revert. For this, I've used toggleClass() function. But things are not working and even hover stops working after placing this function. My queries are:
Why is this happening, the hover is being disabled? How do I use toggleClass() correctly? Is there any way to perform these actions (displaying hidden menus) by hover event of jQuery, instead of click? [I've tried this, the sub menus are displayed but they disappear as soon as mouse moves out. How do I fix it?] Here's the snippet:
body{
margin: 0px;
}
#nav{
height: 40px;
background-color: #009999;
}
#navbar{
height: inherit;
width: 590px;
margin: auto;
}
#slide{
height: 400px;
background-color: #999999;
}
#content{
height: 100px;
background-color: #666666;
}
ul li{
float: left;
list-style: none;
padding: 5px 25px 5px 25px;
line-height: 30px;
background-color: #009999;
cursor: pointer;
text-decoration: none;
color: white;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
border-left: 1px solid #006666;
transition-property: background-color;
transition-duration: 0.5s;
}
.c1{
border-right: 1px solid #006666;
}
ul li:hover{
background-color: #006666;
}
ul{
margin-top: -16px;
}
#dropdown{
position: absolute;
width: 92.5%;
//display: none;
padding:10px 0 10px 100px;
color: white;
font-family: Arial, Helvetica, sans-serif;
background-color:#00CCCC;
display: none;
}
.c7{
background-color: #006666;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="css.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script>
$(document).ready(function(){
$("li").eq(0).click( function(){
$(".c2").fadeToggle(300);
$("li").eq(0).toggleClass("c7");
$(".dropcontainer #dropdown").eq(0).siblings().fadeOut("fast");
$("li").eq(0).siblings().css("background-color", "#009999");
});
$("li").eq(1).click( function(){
$(".c3").fadeToggle(300);
$("li").eq(1).toggleClass("c7");
$(".dropcontainer #dropdown").eq(1).siblings().fadeOut("fast");
$("li").eq(1).siblings().css("background-color", "#009999");
});
$("li").eq(2).click( function(){
$(".c4").fadeToggle(300);
$("li").eq(2).toggleClass("c7");
$(".dropcontainer #dropdown").eq(2).siblings().fadeOut("fast");
$("li").eq(2).siblings().css("background-color", "#009999");
});
$("li").last().click(function(){
$(".c5").fadeToggle(300);
$("li").eq(3).toggleClass("c7");
$(".dropcontainer #dropdown").eq(3).siblings().fadeOut("fast");
$("li").eq(3).siblings().css("background-color", "#009999");
});
});
</script>
</head>
<body>
<div id="nav">
<div id="navbar">
<ul>
<li>Tech</li>
<li>National</li>
<li>Sports</li>
<li class="c1">Contact</li>
</ul>
</div>
</div>
<div class="dropcontainer">
<div id="dropdown" class="c2">
<p>Tech</p>
<p>Tech2</p>
<p>Tech3</p>
</div>
<div id="dropdown" class="c3">
<p>National</p>
<p>National2</p>
<p>National3</p>
</div>
<div id="dropdown" class="c4">
<p>Sports</p>
<p>Sports2</p>
<p>Sports3</p>
</div>
<div id="dropdown" class="c5">
<p>Contact</p>
<p>Contact2</p>
<p>Contact3</p>
</div>
</div>
<div id="slide"></div>
<div id="content"></div>
</body>
</html>