Commandrea
Commandrea

Reputation: 561

Stop animation stops working, jQuery/hover

I am using jQuery to animate the background colors of 3 different divs, repeated to make a 3x3 grid. All's well except the hover out/stop animation seems to quit working if the hover time is extended past a second.

If you hover for one second and move out, the colors freeze in place as desired. However, hovering an extended amount of time seems to break the hover-out/stop function.

Can someone help to explain how the hover/stop function works when trying to control multiple functions simultaneously?

As the only way to show working jQuery is via JSfiddle, I've linked to the a working example here: http://jsfiddle.net/Commandrea/KbLDq/3/

        function pixelColors(){
    var color = '#'+(Math.random()*0xFFFFFF<<0).toString(16);

    $('.logo').animate({
    backgroundColor: color
    }, 1000, pixelColors);

    }

    function pixelColors2(){
    var color = '#'+(Math.random()*0xFFFFFF<<0).toString(16);

    $('.logo2').animate({
    backgroundColor: color
    }, 2000, pixelColors2);

    }

    function pixelColors3(){
    var color = '#'+(Math.random()*0xFFFFFF<<0).toString(16);

    $('.logo3').animate({
    backgroundColor: color
    }, 1500, pixelColors3);

    }


    $('#logo_back').hover(
    pixelColors,
    function() {
    $('.logo').stop()
    }
    );

    $('#logo_back').hover(
    pixelColors2,
    function() {
    $('.logo2').stop()
    }
    );

    $('#logo_back').hover(
    pixelColors3,
    function() {
    $('.logo3').stop()
    }
    );


    .header {
    position: relative;
    margin: 0 auto;
    }


    #home_header {
    position: relative;
    z-index: 7;
    }

    .pixCol {
    float: left;
    height: 288px;
    margin-right: 20px;
    margin-top: 12px;
    min-height: 1px;
    padding-left: 15px;
    width: 227px;
    }



    #pixelBox {
    left: 14px;
    position: relative;
    top: 20px;
    }

    .logo {
    height: 69px;
    width: 69px;
    z-index: 8;
    background-color:#9C3;
    display:inline-block;
    float:left;
    background-color:#F60;
    }


    .logo2 {
    height: 69px;
    width: 69px;
    z-index: 8;
    background-color:#9C3;
    display:inline-block;
    float:left;
    background-color:#F90;
    }

    .logo3 {
    height: 69px;
    width: 69px;
    z-index: 8;
    background-color:#9C3;
    display:inline-block;
    float:left;
    background-color:#F33;
    }

    .logo_back{
    background:none;
    height: 220px;
    margin-top: 51px;
    position: absolute;
    width: 229px;
    z-index: 9;
    }

    .logo_back2 {
    background: url("images/logo/batterEmpty.png") no-repeat scroll 0 0 transparent;
    height: 220px;
    margin-top: 51px;
    position: absolute;
    width: 229px;
    z-index: 9;
    }

    ​


    <div class="header">
    <div class="pixCol">
    <div id="home_header">
    <div id="logo_back" class="logo_back"></div>
    <div id="pixelBox">

    <div class="logo"></div>
    <div class="logo2"></div>
    <div class="logo3"></div>
    <div class="logo3"></div>
    <div class="logo"></div>
    <div class="logo2"></div>
    <div class="logo2"></div>
    <div class="logo3"></div>
    <div class="logo"></div>

    </div>
    </div>
    </div>
    </div>
    ​

Upvotes: 1

Views: 569

Answers (2)

The Alpha
The Alpha

Reputation: 146191

You can use one hover event instead of three and already know stop(true) will solve the problem

 $('#logo_back').hover(
     function(){
         pixelColors();
         pixelColors2();
         pixelColors3();
     },
    function() {
        $('.logo, .logo2, .logo3').stop(true);
    }
); 

DEMO.

Upvotes: 3

bhamlin
bhamlin

Reputation: 5187

I'm pretty confused by your code, why are you kicking off 3 animations at the same time? [edit: Oh I see they're on different elements] Regardless, that's why it's not stopping - you have to call stop(true) to stop queued animations as well, and since you're constantly firing 3 animations at the same time, there are plenty of animations in the queue. Just pass true and your fiddle works fine.

Upvotes: 2

Related Questions