I have a menu composed of a single image (e.g., sprites). In order to display the hover image, I simply move the background image down. I'd like this effect to be controlled by jQuery and animated.
This is a sample menu item.
<ul>
<li id="home"><a href="#"></a></li>
</ul>
This is what I'm toying with. I'm very new to jQuery and am having problems getting the proper selector going.
$(document).ready(function(){
$('#home a');
// Set the 'normal' state background position
.css( {backgroundPosition: "0 0"} )
// On mouse over, move the background
.mouseover(function(){
$(this).stop().animate({backgroundPosition:"(0 -54px)"}, {duration:500})
})
// On mouse out, move the background back
.mouseout(function(){
$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:500})
})
});
Could you point out what I'm doing wrong? I know the selector is probably incorrect but I'm having a hard time figuring out how to manipulate the anchor.
(You don't want the brackets there. There's no brackets in a CSS
background-position
rule.)In any case jQuery doesn't know how to animate a compound value like
backgroundPosition
. In IE you get access to it withbackgroundPositionY
, which, as a simple value, jQuery can animate. However this is non-standard and won't work elsewhere.Here's a plugin that claims to fix it.
i liked this method (just css)
I would imagine your background is bound to the
li
?! Howeverthis
is set to thea
of the selector, so if my assumption is correct you would need to change your code to//Use MouseEnter and MouseLeave whenever possible and you do not need to type out duration with the current version of jQuery. Hope this helps.
Not the answer you're looking for? Browse other questions tagged jquery css menu or ask your own question.
asked
viewed
52,271 times
active
5 years, 6 months ago
Linked
Related
Hot Network Questions