Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery addclass/removeclass doesn't always work when "speed" is set (mouse events)

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.

like image 263
Thomas Stock Avatar asked Mar 03 '09 17:03

Thomas Stock


7 Answers

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.

like image 156
David Carlson Avatar answered Nov 15 '22 06:11

David Carlson


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");
});
like image 40
Marius Avatar answered Nov 15 '22 07:11

Marius


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.

like image 37
cletus Avatar answered Nov 15 '22 06:11

cletus


Yes, do it in CSS!

.employee:hover{background:pink;}

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

like image 40
Svante Svenson Avatar answered Nov 15 '22 05:11

Svante Svenson


There is a duration parameter for removeClass (http://docs.jquery.com/UI/Effects/removeClass), but it is not working in FF. Is there anything wrong with my code? I am new to JQuery. I am going to try animate function now.

What I am trying to do here is use the anchors and then highlight the destination anchor location when the anchor is clicked. Here's my HTML code -

<ul>
                <li><a href="#caricatures">Caricatures</a></li>
                <li><a href="#sketches">Sketches</a></li>
</ul>

My destination anchor is -

<a href="#" id="caricatures"><h3>Caricatures</h3></a>

This is where I might the background color to change.

Here's my CSS -

<style>
            .spotlight{
                background-color:#fe5;
            }
</style>

Here's my JQuery code -

<script>
    $('a[href$="caricatures"]').click(function(){
        $('a[id="caricatures"] h3').addClass("spotlight");
        $('a[id="caricatures"] h3').removeClass("spotlight",1000);
    });
    </script>
like image 34
designplusdev Avatar answered Nov 15 '22 06:11

designplusdev


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.

like image 27
obeattie Avatar answered Nov 15 '22 05:11

obeattie


Here's my transition with jQuery:

$("#layoutControl .actionList").click(
    function(){
        $("#assetsContainer").fadeOut("fast",function(){
            $(this).removeClass("assetsGrid").addClass("assetsList");
            $("#iconList").attr("src", "/img/bam/listIcon.png");
            $("#iconGrid").attr("src", "/img/bam/gridIconDim.png");
            $(this).fadeIn("fast");
        });
    }
);
like image 34
Eugenio Avatar answered Nov 15 '22 06:11

Eugenio