So Im setting up a menu that has different background :hover colors. The buttons backgrounds will be a grey color and upon hover the button animate() to its respective color.
I can grab the original default color of whatever button I hover over like this:
var origBackgroundColor = $(this).css('background-color');
But is it possible to get the color from a css :hover property that I set? I will have the :hover colors set for each button because if someone doesn't have JS enabled the navigation will still have :hover effects working.
Something like this(Or is there another way):
var hoverColor = $(this).css('background-color:hover');
Any ideas? is this possible?
jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.
You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css("propertyName");
The :hover selector is used to select elements when you mouse over them. Tip: The :hover selector can be used on all elements, not only on links. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :active selector to style the active link.
The hover() method specifies two functions to run when the mouse pointer hovers over the selected elements. This method triggers both the mouseenter and mouseleave events. Note: If only one function is specified, it will be run for both the mouseenter and mouseleave events.
I'm pretty sure that in order to get the background-color
of the :hover
pseudo, it will first require a browser event to apply the style. In other words, I think you could get it when you do a hover with the mouse, but not until then.
Could be that you could wait until the user does a hover, then grab the color, override it with the default, store it for future reference, then do your animation, but that may be more trouble that simply coordinating your JS and CSS.
Something like this: http://jsfiddle.net/UXzx2/
// grab the default color, and create a variable to store the hovered.
var originalColor = $('div').css('background-color');
var hoverColor;
$('div').hover(function() {
// On hover, if hoverColor hasn't yet been set, grab the color
// and override the pseudo color with the originalColor
if( !hoverColor ) {
hoverColor = $(this).css('background-color');
$(this).css('background-color',originalColor);
}
// Then do your animation
$(this).animate({backgroundColor:hoverColor});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With