tags:

views:

3857

answers:

8

In css class "employee_mouseover" I make the bg color red.

        $(".employee").bind("mouseenter", function() {
            $(this).addClass("employee_mouseover");
        });
        $(".employee").bind("mouseleave", function() {
            $(this).removeClass("employee_mouseover");
        });

This works fine.

But, when I set a speed to make it look more pretty, the element stays red when I quickly do a mouseenter+mouseleave;

    $(".employee").bind("mouseenter", function() {
        $(this).addClass("employee_mouseover", "fast");
    });
    $(".employee").bind("mouseleave", function() {
        $(this).removeClass("employee_mouseover", "fast");
    });

This doesn't work well, unless I move in and out of the element very slowly.

Is there a better way to do this?

Thanks in advance.

+2  A: 

Yes, do it in CSS!

.employee:hover{background:pink;}

Also, there is no speed parameted for addClass, speed only exists for effects.

svinto
That doesn't enable me to set the speed to make it look pretty, no?Besides, I need to enable/disable the mouse events sometimes when the user does stuff..
Thomas Stock
"Also, there is no speed parameted for addClass, speed only exists for effects."That doesn't seem to be true, as it works when I do it slowly.I described both of these things in my original post.
Thomas Stock
@Thomas: actually it's very true.
Crescent Fresh
@Thomas: It works, but it's apparently an undocumented feature that doesn't work correctly.
R. Bemrose
It can't possibly work at all. A class is something an element either has or does not have; there is no in-between state like a tweened position or blended colour possible. Whatever you're seeing, it's not a slow class transition.
bobince
@R.Bemrose: No, there is no such feature. jQuery trunk confirms addClass/removeClass are synchronous operations: http://dev.jquery.com/browser/trunk/jquery/src/core.js
Crescent Fresh
Thanks for researching this. What's weird tho is intellisense in visual studio 2008 told me that the second parameter to be passed to addClass() is the "speed". I wonder why they've put that in (AND made it work) without specifying this on the jquery site
Thomas Stock
Microsoft strikes again. Turn off the quite-poorly-named 'intellisense' and always go by the real documentation.
thenduks
Actually, I think the intellisense file I use is third party made.Next to that, this is the first time in 3 years of heavily using intellsense that I found a mistake in it..
Thomas Stock
+1  A: 

addClass is for adding CSS classes to elements. If you're looking to change some CSS property by tweening, then you need to do that explicitly using Effects.

Your code:

$(this).addClass("employee_mouseover", "fast");

Will add two classes, employee_mouseover and fast to this, which is obviously not what you're after.

obeattie
Think you pasted the wrong code there. Also, "fast" is ignored.
Crescent Fresh
Wow you are totally right. So it is. :) Guess it's a comma-delimited string instead then.
obeattie
+3  A: 

You're using the wrong event. You want:

$(".employee").hover(function() {
 $(this).addClass("employee_mouseover");
}, function() {
 $(this).removeClass("employee_mouseover");
});

And there's no speed argument on add or remove class.

cletus
+3  A: 

It can be done, but you need to install the jquery coloranimate plugin. Then you can use the code below to change the color slowly:

$(".employee").bind("mouseenter", function() {
  $(this).animate({backgroundColor: "#bbccff"}, "slow");

});
$(".employee").bind("mouseleave", function() {
  $(this).animate({backgroundColor: "white"}, "slow");
});
Marius
Thank you for this constructive answer. The other replies were helpful as well in an educational way, but this one gave me a way to achieve what I was trying to do. Thanks!
Thomas Stock
+5  A: 

from the jQuery UI docs:

The jQuery UI effects core extends the base class API to be able to animate between two different classes. The following methods are changed. They now accept three additional parameters: speed, easing (optional), and callback.

So @Thomas must have included both jQuery and jQuery UI libraries on his page, enabling the speed and callback parameters to addClass and removeClass.

David Carlson
that's really nice info, thanks
Thomas Stock
A: 

Also, there is no speed parameted for addClass, speed only exists for effects.

Correct.

But perhaps this Plugin might help:

http://plugins.jquery.com/project/animate-to-class

Mike Gledhill
A: 

Even if you include JqueryUI properly, these all appear to fail outside of FF when you use the "duration" parameter. Causes JS errors in IE. I would stick to animate() which sucks.

http://jqueryui.com/docs/addClass/

http://jqueryui.com/docs/removeClass/

http://jqueryui.com/docs/switchClass/

http://jqueryui.com/docs/toggleClass/

There is no note of this in the docs on the jqueryui site.

jozecuervo
A: 
$(".employee").hover(function() {
    $(this).stop().animate({ backgroundColor: "#bbccff" }, "slow");
}, function() {
    $(this).stop().animate({ backgroundColor: "white" }, "slow");
});
TiuTalk