I’m new with jQuery and I’m trying to solve an issue with my navigation. I built a vertical menu with many nested lists. It’s purely CSS but my client requested that the sub menu currently open stays open for a second or so when you’re no longer hovering over it.
Here’s the site I’m working on: http://dev.musgraveagencies.com/
And here is the site they currently have, and how they want the menu to delay: http://www.musgraveagencies.com/
I believe I can use jQuery to achieve this, but I want the site to work as is if Javascript isn’t enabled.
Edit: Thanks for everyone’s help! Here is the final code below.
My HTML:
<body class="nojs">
<div id="nav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Development Projects</a>
<ul>
<li><a href="#">Design Build</a>
<ul>
<li><a href="#">Interior Design</a>
<ul>
<li><a href="#">Project Example</a></li>
</ul>
</li>
<li><a href="#">Project Example 1</a></li>
<li><a href="#">Project Example 2</a></li>
<li><a href="#">Project Example 3</a></li>
</ul>
</li>
<li><a href="#">Industrial</a>
<ul>
<li><a href="#">Project Example 1</a></li>
<li><a href="#">Project Example 2</a></li>
<li><a href="#">Project Example 3</a></li>
<li><a href="#">Project Example 4</a></li>
</ul>
</li>
<li><a href="#">Residential</a></li>
<li><a href="#">Neighbourhood</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
</body>
My CSS:
#nav {
width: 156px;
font: 9pt 'TeXGyreScholaRegular', Arial, sans-serif;
text-align: left;
}
#nav ul a {
color: #fff;
display: block;
overflow: auto;
line-height: 19pt;
text-indent: 2px;
}
#nav ul a:hover {
color: #fff;
text-decoration: none;
}
#nav ul li:hover {
background: #484848;
}
#nav ul {
width: 156px;
}
#nav ul li {
list-style: none;
background: #000;
position: relative;
z-index: 100;
}
#nav ul li ul {
position: absolute;
width: 156px;
top: 0;
left: 156px;
}
body.nojs #nav ul li ul {
display: none;
}
body.nojs #nav ul li:hover > ul {
display: block;
}
My jQuery:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.hoverIntent.minified.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#nav ul li ul').hide();
$('body').removeClass('nojs');
var config = {
sensitivity: 2,
interval: 200,
timeout: 700,
};
var animating = false;
$('#nav ul li').hoverIntent(
function () {
animating = true;
$('> ul', this).fadeIn(100, function() {
animating = false;
});
},
function () {
animating = true;
$('> ul', this).delay(400).fadeOut(200, function() {
animating = false;
});
});
});
</script>
The problem with the code is, you are triggering a transition (fade) with jQuery but hiding the element immediately with your CSS. So this happens with no delay.
You can use the
transition-delayoranimation-delayproperties with CSS3 but these are not supported in IE. All others support them but not with most versions.So, I think you should migrate most of the CSS (hover pseudo-classes) to javascript/jQuery and hide/show with delay using the
setTimeout()method or jQuery’sdelay()method.UPDATE:
Since you are concerned if javascript is not enabled in the user’s browser; without javascript and CSS delay related properties, I don’t think it is possible to delay a change on an HTML element’s appearance.
So what you can do is;
<noscript>tags for non-javascript content and your CSS-only menu and place them inside it.This way you will have both users satisfied.