gaurav jain
gaurav jain

Reputation: 1345

reset state to original on hover out

I have something like:

 $(".leftCues").hover(
        function(){
                $(".item1", ".item2", ".item3", ".item4", ".item5").stop(true,true)
                $(".item1").animate({opacity:1.0}, 2000, function()  {
                  $(".item1").animate({opacity:0.0},2000, function() {
                    $(".item2").animate({opacity:1.0},2000, function()  {
                      $(".item2").animate({opacity:0.0}, 2000, function() {
                        $(".item3").animate({opacity:1.0}, 2000, function()  {
                          $(".item3").animate({opacity:0.0}, 2000, function() {
                            $(".item4").animate({opacity:1.0},2000, function()  {
                              $(".item4").animate({opacity:0.0}, 2000, function() {
                                $(".item5").animate({opacity:1.0}, 2000, function()  {
                                });
                              });
                            });
                          });
                        });
                      });
                    });
                  });
                });
        },
        function(){
                $(".item5").css('opacity', 0.0);
        }       
  );    

CSS:

.item5, .item3, .item1 {
  clear: both;
  float:left;
  margin-top: 6%;
  color: #999999;
  font-size: 40px;
  margin-left: 25px;
  opacity:0.0;
}

.item2, .item4 {
  clear: both;
  float:right;
  margin-top: 6%;
  color: #999999;
  font-size: 40px;
  margin-right: 25px;
  opacity:0.0;
}

My animation works fine when I hover on but when I hover out, my previous state is not getting reset. Now when hover on again, a new animation starts while the previous animation is still running.

How can I reset the my previous hover on state? On hover out, I want to go back to original state (like before my very first hover on)

In my animation, I just toggle opacity from 1.0 to 0.0 and my css starts with opacity 0.0.

Please help.

Upvotes: 0

Views: 1401

Answers (2)

VVV
VVV

Reputation: 7593

You could use .fadeIn() or fadeOut() if there is nothing other to animate.

Something like this

$('#item').hover( function() {
      $('#item2, #item3, #item4, #item5').stop(true, true).fadeIn();
  },
  function () {
    $('#item2, #item3, #item4, #item5').stop(true, true).fadeOut();
  }
);

The .stop(true, true) is important because it makes it jump at the end of the animation. Here's the documentation.

Upvotes: 3

Alfred
Alfred

Reputation: 21396

You may use jQuery's .mouseover() and .mouseout() functions.

http://api.jquery.com/mouseover/

http://api.jquery.com/mouseout/

$(".leftCues").mouseover(function(){
                $(".item1").animate({opacity:1.0}, 2000, function()  {
                  $(".item1").animate({opacity:0.0},2000, function() {
                    $(".item2").animate({opacity:1.0},2000, function()  {
                      $(".item2").animate({opacity:0.0}, 2000, function() {
                        $(".item3").animate({opacity:1.0}, 2000, function()  {
                          $(".item3").animate({opacity:0.0}, 2000, function() {
                            $(".item4").animate({opacity:1.0},2000, function()  {
                              $(".item4").animate({opacity:0.0}, 2000, function() {
                                $(".item5").animate({opacity:1.0}, 2000, function()  {
                                });
                              });
                            });
                          });
                        });
                      });
                    });
                  });
                });
}).mouseout(function(){
    $(".item5").css('opacity', 0.0);
});

Upvotes: 1

Related Questions