This seems to be a very common problem, although I’ve tried the recommendations to use addClass / removeClass rather than direct CSS manipulation, and I’ve tried mouseleave / mouseout but both cause problems.
All I want is a simple roll over that changes the class! Everything I try either inconsistently flickers the class on or off. The only other requirement I’ve been trying to accommodate is to have the listener live in a function rather than inline. Is that what’s making this impossible?
function highlight(_event){
$(this).addClass("Highlighted");
}
function unhighlight(_event){
$(this).delay(2000,function(){
$(this).removeClass("Highlighted");
});
}
$(document).ready(function () {
$(".Content").live('mouseenter',highlight);
$(".Content").live('mouseout',unhighlight);
});
Edit
Adding .stop(true,true) seems to help quite a bit.
If you want to use live, you could do the following using
.toggleClass(),.mouseover()&.mouseout():See a demo here
Update: I couldn’t leave this one, so carried on messing with your example – after the comment you use the functions in other places. So here is what I found:
Instead of calling
.mouseout()use.mouseleave()– for some reason.mouseout()get’s called multiple times when the mouse moves round – it even gets called when the mouse first enters the object….. See the demo at the end to see what i mean hereSecondly, the
.delay()isn’t really being used correctly here – it is really meant for queuing effect’s – but what you really want is to add a delayed function (albeit it is an effect that you are after) so instead use the.setTimeout()to do the call for you.As per the doc’s:
So the finished functions would look like this:
See the demo here this one will show you what I mean about the
.mouseout()event being fired multiple times, where as the.mouseleave()function is only called once when the mouse actually leaves.Note: use your enter button to press the alert ok, don’t move your mouse!!
See the demo here to see the final version working here as close to your original as possible.