views:

36

answers:

3

Hi, 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!

A: 

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.

Ken Redler
thanks ken, this works perfectly
galilee
A: 

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

GuidoH
Sorry, typo in the example, they are all exactly the same offset.....
galilee
Lol! Then, nevermind, just ignore my post.
GuidoH
+1  A: 

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.

Nick Craver