I'm in the process of building a navigation toolbar for a site and struggling to make the hover+click work. I have been searching for days and rebuilding several times using methods from live sites and tutorials and I just cant solve this simple problem.
Issue: I have a navigation toolbar sprite image with three states, passive, hover and active. I have the classes set up for all three states but can only get the hover state to correctly appear with jquery.
What I want to do: When the cursor hovers over the text, I want it to animate the opacity for the hover section of the sprite running under a tag (which works and fades in and out perfectly). Next I want to animate the opacity for the active section of the sprite so it also fades in from the hover sprite to active sprite. The website will also be single page navigation so I need the active button to go back to the passive sprite when another button is clicked.
Here's what I have so far:
CSS:
ul#navMenu {
width:1020px;
height: 110px;
list-style:none;
margin:250px auto;
}
ul#navMenu li {
float:left;
}
ul#navMenu li a {
background: url(menu-sprite.png) no-repeat scroll top left;
display: block;
height: 110px;
text-indent: -9999px;
position:relative;
}
ul#navMenu li a.gallery {
width: 230px;
background-position: 0 0;
}
ul#navMenu li a.wip {
width: 229px;
background-position: -230px 0;
}
ul#navMenu li a.home {
width: 103px;
background-position: -459px 0;
}
ul#navMenu li a.scripts {
width: 229px;
background-position: -562px 0;
}
ul#navMenu li a.contact {
width: 229px;
background-position: -791px 0;
}
ul#navMenu li a span {
background: url(menu-sprite.png) no-repeat scroll bottom left;
display: block;
position:absolute;
top:0px;
left:0px;
height:100%;
width:100%;
z-index:75;
}
ul#navMenu li a span:hover {
cursor:pointer;
}
ul#navMenu li a.gallery span{
background-position: 0 -110px;
}
ul#navMenu li a.wip span{
background-position: -230px -110px;
}
ul#navMenu li a.home span{
background-position: -459px -110px;
}
ul#navMenu li a.scripts span{
background-position: -562px -110px;
}
ul#navMenu li a.contact span{
background-position: -791px -110px;
}
ul#navMenu li a :active span {
background: url(menu-sprite.png) no-repeat scroll bottom left;
display: block;
position:absolute;
top:0px;
left:0px;
height:100%;
width:100%;
z-index:100;
}
ul#navMenu li a span:active {
cursor:pointer;
}
ul#navMenu li a .gallery:active span {
background-position: 0 -220px;
}
ul#navMenu li a .wip:active span {
background-position: -230px -220px;
}
ul#navMenu li a .home:active span {
background-position: -459px -220px;
}
ul#navMenu li a .scripts:active span {
background-position: -562px -220px;
}
ul#navMenu li a .contact:active span {
background-position: -791px -220px;
}
...
HTML:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>NavMenu Test</title>
<link rel="stylesheet" href="styles.css" />
<script src="jquery-1.8.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
// set opacity to nill on page load
$("ul#navMenu span").css("opacity","0");
// on mouse over
$("ul#navMenu span").mouseover (function () {
// animate opacity to full
$(this).stop().animate({
opacity: 1
}, 'slow');
});
// on mouse out
$("ul#navMenu span").mouseout (function () {
// animate opacity to nill
$(this).stop().animate({
opacity: 0
}, 'slow');
});
// on mouse click
$("ul#navMenu :active span").click(function () {
// animate opacity to full
$(this).unbind('mouseover'),
$(this).unbind('mouseout'),
$(this).stop().animate({
opacity: 1
}, 'slow');
});
});
</script>
</head>
<body>
<ul id="navMenu">
<li><a href="#" class="gallery"><span style="opacity: 0;"></span></a></li>
<li><a href="#" class="wip"><span style="opacity: 0;"></span></a></li>
<li><a href="#" class="home"><span style="opacity: 0;"></span></a></li>
<li><a href="#" class="scripts"><span style="opacity: 0;"></span></a></li>
<li><a href="#" class="contact"><span style="opacity: 0;"></span></a></li>
</ul>
</body>
</html>
The functionality I'm going for can be found here: http://pixelpimps.net/#!/home
I'm trying to accomplish the same functionality (hover over animation, click animation, click switching animation) except with an image sprite.
Thanks in advance and please ask if you need anything else.