Reputation: 461
I have the following jquery which animates on hover:
$('#footerNetwork').hover(function(){
$('#popupNetwork').animate({top:'-62px'},{queue:true,duration:500});
}, function(){
$('#popupNetwork').animate({top:'30px'},{queue:true,duration:500});
});
$('#footerPort').hover(function(){
$('#popupPort').animate({top:'-62px'},{queue:true,duration:500});
}, function(){
$('#popupPort').animate({top:'30px'},{queue:true,duration:500});
});
$('#footerAirport').hover(function(){
$('#popupAirport').animate({top:'-62px'},{queue:true,duration:500});
}, function(){
$('#popupAirport').animate({top:'30px'},{queue:true,duration:500});
});
etc...
how can I combine these into on function which recognises which link has been hovered (ie: footerNetwork) and targets the appropriate div to animate (popupNetwork)?? thanks!
Upvotes: 0
Views: 117
Reputation: 630379
You could do something like this:
var tops = { footerNetwork:'-80px', footerPort:'-62px', footerAirport:'-62px' };
$('#footerNetwork, #footerPort, #footerAirport').hover(function(){
$('#'+this.id.replace('footer','popup')).animate({top: tops[this.id]}, 500);
}, function(){
$('#'+this.id.replace('footer','popup')).animate({top:'30px'}, 500);
});
If you add a class to those elements say class="footer"
then you can change the .hover()
to $('.footer').hover(function(){
to make it even cleaner. To get the appropriate #popup_____
element we're just take the current ID, e.g. footerNetwork
and doing a .replace()
to get the popup ID. The tops
object is to store the various top values since they differ.
Upvotes: 1
Reputation: 5776
Since the offsets are not fixed, it's not really possible to get the same result with one call, but, a function like this will do the trick:
function hoverIt(name, topIn, topOut, duration)
duration = (duration != undefined) ? duration : 500;
$('#footer' + name).hover(function(){
$('#popup' + name).animate({top: topIn + 'px'},
{queue: true, duration: duration});
}, function(){
$('#popup' + name).animate({top: topOut + 'px'},
{queue: true, duration: duration});
});
}
Then just call the function for each animation:
hoverIt('Network', -80, 30, 300);
hoverIt('Port', -62, 30);
hoverIt('Airport', -62, 30);
Is much better I guess. When there'll be a lot of them, you could use something like:
var hovers = [['Network', -80, 30, 300],
['Port', -62, 30],
['Airport', -62, 30]];
for (var i = 0; i < hovers.length; i++) {
hoverIt(hovers[i][0], hovers[i][1], hovers[i][2], hovers[i][3]);
}
note: Not tested
Upvotes: 0
Reputation: 23943
I'll assume you have a class foo
on the items to which you want to add the hover behavior. Then it's just a matter of following the (apparent) footer...
pattern:
$(document).ready( function(){
$('.foo').hover(
function(){
$('#popup' + this.id.substr(6,this.id.length-6) ).animate({...});
// ^remove "footer" portion of id
},
function(){
$('#popup' + this.id.substr(6,this.id.length-6) ).animate({...});
}
);
});
Depending on how your document is structured, you could also identify the "footer..." elements by their container, rather than by class.
Upvotes: 0