I’ve updated an old javascript of mine to work with Jquery recently.
It does the following:
When a user hovers over a link with a class of content-fade it causes content with an associated id to fade-in and then within a set time fade-out unless the user leaves their mouse on the content.
I need to change the script again and have it loop through a set of ids aka id1, id2, id3…etc. Ideally causing the only the content with the individual associated id to show. I’ve just started working with JQuery and I’m not sure how best to approach this. The code is below if anyone could point me in the right direction I would appreciate it.
$(document).ready(function() {
var hide = false;
$(".content-fade").hover(function(){
if (hide) clearTimeout(hide);
$("#id").fadeIn();
}, function() {
hide = setTimeout(function() {$("#id").fadeOut("slow");}, 250);
});
$("#id").hover(function(){
if (hide) clearTimeout(hide);
}, function() {
hide = setTimeout(function() {$("#id").fadeOut("slow");}, 250);
});
});
If I understand your requirements correctly, you are on the right track.
data-hover-target)mouseenter(hover), fade the target element in.mouseleavefade the target out with a delaymouseenterto prevent the fadeOut if it is pending. Thus, if the user moves the cursor away from the link, then immediately back again – before the delayed fadeOut has started – the fadeOut will be cancelled and the target element will remain visible.See below for the code and http://jsbin.com/uruzaw/11/ for a working example.
EDIT I think I misunderstood your requirements. If you wish to prevent fadeOut of the content when on hover of both the link and the content itself, the following should suffice. It’s basically what you have written but using jQuery animation queue with
.delay()in place ofsetTimeout(). See http://jsbin.com/uruzaw/13/