views:

203

answers:

3

I'm trying to change the background color of a div on mouseover and mouseout. Instant change to yellow on MouseOver, and slow fade on MouseOut.

function hilightel(keydiv)
{
$('#'+keydiv).animate({ backgroundColor: '#ffffd3' },1);
}
function lolightel(keydiv)
{
$('#'+keydiv).animate({ backgroundColor: '#ffffff' },300);
}

< div onMouseOver=javascript:highlightel('item1'); onMouseOut=javascript:lolightel('item1'); id='item1'>CONTENT< /div>

When the mouse moves over text within the div, though, it thinks I've moused-out and so flickers badly.

Alternatives that don't work:
- animateToClass doesn't support background-color so I'm using the 'color' plugin
- I hear that switchClass doesn't work in Chrome
- Can't use .hover because their will be dynamically named divs in the page so need a general function

Thanks in advance...

+1  A: 

Why not simply assign the div(s) a class, and then use .hover to target them?

EDIT

Try this:

$(".myclass").hover(
function(){
    $(this).animate({ backgroundColor: '#ffffd3' },1);
    },
    $(this).animate({ backgroundColor: '#ffffff' },300);
    }
);
graphicdivine
Hi GD, if I assign them a class and target the class then they'll all change colour at the same time and won't be highlighting which one you've moused-over?I'm thinking I could parse through all the elements on the page when it loads and bind them to monitor mouseover/out but that seems pretty backwards...?
Just found this on another thread which I think is what you were referring to - will give it a bash...$('.MyPanels').live("mouseover", function() { //do stuff });
A: 

Final solution:

$(document).ready(function()
{
$('.a_editableitem').bind('mouseenter', function() { $(this).animate({ backgroundColor: '#ffffd3' },25); });
$('.a_editableitem').bind('mouseleave', function() { $(this).animate({ backgroundColor: '#ffffff' },250); });
});

Flickering has stopped although still gets 'stuck' on yellow sometimes.

A: 

mouseenter / mouseleave helped me solving the background images flickering problems. Thanks buddy!

snowalker
No worries, click the 'useful' up arrow!