This is some JavaScript I have for a simple navigation bar but I have issues with the drop down disappearing before you can click on them so I want to add a delay after the mouse leaves the bar before they hide.
How would I do that?
<script type="text/javascript">
$(document).ready(function () {
// Navigation bar drop-down
$("nav ul li").hover(function () {
$(this).addClass("active");
$(this).find("ul").show().animate({ opacity: 1 }, 400);
}, function () {
// Delay on hiding should go here
$(this).find("ul").hide().animate({ opacity: 0 }, 200);
$(this).removeClass("active");
});
$('nav ul li ul li:first-child').prepend('<li class="arrow"></li>');
$('nav ul li:first-child').addClass('first');
$('nav ul li:last-child').addClass('last');
$('nav ul li ul').parent().append('<span class="dropdown"></span>').addClass('drop');
});
</script>
Thanks in advance to anyone who can help
P.S. This is probably really obvious but I know very little about JavaScript. :L
This is some JavaScript I have for a simple navigation bar but I have issues with the drop down disappearing before you can click on them so I want to add a delay after the mouse leaves the bar before they hide.
How would I do that?
<script type="text/javascript">
$(document).ready(function () {
// Navigation bar drop-down
$("nav ul li").hover(function () {
$(this).addClass("active");
$(this).find("ul").show().animate({ opacity: 1 }, 400);
}, function () {
// Delay on hiding should go here
$(this).find("ul").hide().animate({ opacity: 0 }, 200);
$(this).removeClass("active");
});
$('nav ul li ul li:first-child').prepend('<li class="arrow"></li>');
$('nav ul li:first-child').addClass('first');
$('nav ul li:last-child').addClass('last');
$('nav ul li ul').parent().append('<span class="dropdown"></span>').addClass('drop');
});
</script>
Thanks in advance to anyone who can help
P.S. This is probably really obvious but I know very little about JavaScript. :L
Share Improve this question edited Apr 9, 2012 at 17:51 Pointy 414k62 gold badges595 silver badges629 bronze badges asked Apr 9, 2012 at 17:50 user1320511user1320511 3- 6 Java != Javascript Two pletely different languages; two very different purposes, environments and uses – paulsm4 Commented Apr 9, 2012 at 17:51
- 2 jQuery api.jquery./delay – Ersel Aker Commented Apr 9, 2012 at 17:53
- Javascript is awesome and powerful, glad you're learning it! Good luck! – murftown Commented Jul 30, 2013 at 15:04
6 Answers
Reset to default 3I have a simple navigation bar
Don't use JavaScript then. This can and should be done with CSS. CSS transitions and selectors allow to define exactly what you want.
See also Delay :Hover in CSS3? and the excellent example from there.
Don't use a huge function such as delay()
. Just use setTimeout()
.
var that = this
setTimeout(function() {
$(that).hide() // Do your stuff, just don't forget that "this" has changed
}, 1000) // Define your delay in milliseconds here
The function inside the setTimeout
will execute after the delay specified as a second argument.
You can do it like this. You use the delay()
method to set up the delay and you use .stop(true)
on both hover functions in case the user goes out and es back in during the delay. The .stop(true)
will clear any queued animations. I also switched the code to fadeIn()
and fadeOut()
because those automatically do the show()
and hide()
as needed.
<script type="text/javascript">
$(document).ready(function () {
// Navigation bar drop-down
$("nav ul li").hover(function () {
$(this).addClass("active");
$(this).find("ul").stop(true).fadeIn(400);
}, function () {
// Delay on hiding should go here
var self = $(this);
self.find("ul").stop(true).delay(1500).fadeOut(400, function() {
self.removeClass("active");
});
});
$('nav ul li ul li:first-child').prepend('<li class="arrow"></li>');
$('nav ul li:first-child').addClass('first');
$('nav ul li:last-child').addClass('last');
$('nav ul li ul').parent().append('<span class="dropdown"></span>').addClass('drop');
});
</script>
I think you can do something like this:
<script type="text/javascript">
$(document).ready(function () {
// Navigation bar drop-down
$("nav ul li").hover(function () {
$(this).addClass("active");
$(this).find("ul").show().animate({ opacity: 1 }, 400);
}, function () {
// Delay on hiding should go here
$(this).find("ul").hide().delay(1000).animate({ opacity: 0 }, 200, function() {
$(this).removeClass("active");
});
});
$('nav ul li ul li:first-child').prepend('<li class="arrow"></li>');
$('nav ul li:first-child').addClass('first');
$('nav ul li:last-child').addClass('last');
$('nav ul li ul').parent().append('<span class="dropdown"></span>').addClass('drop');
});
</script>
You could use delay().
<script type="text/javascript">
$(document).ready(function () {
// Navigation bar drop-down
$("nav ul li").hover(function () {
$(this).addClass("active");
$(this).find("ul").show().animate({ opacity: 1 }, 400);
}, function () {
// Delay on hiding should go here
$(this).find("ul").delay(5000).fadeOut();
$(this).removeClass("active");
});
$('nav ul li ul li:first-child').prepend('<li class="arrow"></li>');
$('nav ul li:first-child').addClass('first');
$('nav ul li:last-child').addClass('last');
$('nav ul li ul').parent().append('<span class="dropdown"></span>').addClass('drop');
});
</script>
Very interesting. Nothing hides, until you mouseout. FIDDLE
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745340045a4623284.html
评论列表(0条)