Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

jQuery fadeIn(), fadeOut() on hover.

Posted on 2010-09-08
5
941 Views
Last Modified: 2012-05-10
I've got the following jQuery code below...
It is applied to several buttons along the top of my site so that when the mouse passes over them it will generate a nice fadeIn/fadeOut hover effect.  It works perfectly except that if I pass the mouse over the buttons very rapidly the fadeIn/fadeOut effects get backed up and when I stop shaking the mouse over the buttons I can sit back an watch all my buttons fade in and fade out several times before they finally come to a rest as they get all caught up to the events I triggered.

That looks stupid.  I would like for one fade effect to be canceled and another one to start immediately when the event is triggered.  So if the mouse passes into and out of the div very quickly then I would want the fadeIn to barely to have begun before it reverses from where it is back to the start.  Hope that makes sense.

I've tried using the stop() method, like this...

$('#btnTransact').click(function () {
  alert('Transactions');
}).hover(function () {
  $('#btnTransact').find('.MasterButtonHover').stop().fadeIn(200);
}, function () {
  $('#btnTransact').find('.MasterButtonHover').stop().fadeOut(200);
});

But this completely breaks it so nothing happens at all.

Any help on this would be greatly appreciated.

Thanks
<script type="text/javascript">
  $(function () {

    $('#btnTransact').click(function () {
      alert('Transactions');
    }).hover(function () {
      $('#btnTransact').find('.MasterButtonHover').fadeIn(200);
    }, function () {
      $('#btnTransact').find('.MasterButtonHover').fadeOut(200);
    });

    $('#btnPlanning').click(function () {
      alert('Planning');
    }).hover(function () {
      $('#btnPlanning').find('.MasterButtonHover').fadeIn(200);
    }, function () {
      $('#btnPlanning').find('.MasterButtonHover').fadeOut(200);
    });

    $('#btnConfig').click(function () {
      alert('Configuration');
    }).hover(function () {
      $('#btnConfig').find('.MasterButtonHover').fadeIn(200);
    }, function () {
      $('#btnConfig').find('.MasterButtonHover').fadeOut(200);
    });

    $('#btnCharts').click(function () {
      alert('Charts');
    }).hover(function () {
      $('#btnCharts').find('.MasterButtonHover').fadeIn(200);
    }, function () {
      $('#btnCharts').find('.MasterButtonHover').fadeOut(200);
    });

    $('#btnLogout').click(function () {
      alert('Logout');
    }).hover(function () {
      $('#btnLogout').find('.MasterButtonHover').fadeIn(200);
    }, function () {
      $('#btnLogout').find('.MasterButtonHover').fadeOut(200);
    });

  });
</script>

Open in new window

0
Comment
Question by:josephdaviskcrm
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33633614
have you checked this?
http://bavotasan.com/tutorials/creating-a-jquery-mouseover-fade-effect/ 
there is a demo at the bottom of page, it is also using stop function

and also this
http://aintyomamasblog.com/test/hover/
0
 
LVL 5

Expert Comment

by:drakeshe
ID: 33633620
You can call
$(#myelement").stop();
THis will stop all animation.
0
 
LVL 5

Expert Comment

by:drakeshe
ID: 33633726
Didnt notice that you already had tried stop. But I did then notice that you had not used stop correctly!
Try this new code:
$('#btnTransact').click(function () {
  alert('Transactions');
}).hover(function () {
  $('#btnTransact').find('.MasterButtonHover').stop(true, true).fadeIn(200);
}, function () {
  $('#btnTransact').find('.MasterButtonHover').stop(true, true).fadeOut(200);
});

Open in new window

0
 

Author Closing Comment

by:josephdaviskcrm
ID: 33636843
This is exactly what I was looking for.  Using the stop(true, true) method forces the animation to jerk to the finish before it starts the next animation.  But this technique will reverse the animation from where ever it is currently when the next even is called.

Thanks for this.
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33636948
thanks for the points
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question