Doing some magic with JQuery
Hello all,
I have this problem that I can't solve...
I have two links. I want to show a text when none of them is hover, and then, when the mouse is over one of the links, fade out the current text and fade in the related one.
The thing is, when doing it slowly and in order it works fine. But if you pass the mouse over both links quickly you can get the effect that both text are activated. I also tried the "if :animated then stop()" but I couldn't make it work.
I have tried many things, unsuccessfully. I don't want to modify the markup and create a dummy div containing the text to be shown.
This is the code. Thank you.
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html><head><title>Example</title>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
- <script type="text/javascript">
- $(document).ready(function () {
- $('#element1').hover(function() {
-
- $('#text1').fadeOut(1000, function() {
- $('#text3').fadeOut(1000, function() {
- $('#text2').fadeIn(1000);
- });
- });
- },function(){
- $('#text2').fadeOut(1000,function() {
- $('#text1').fadeIn(1000); });
- });
- });
- $(document).ready(function () {
- $('#element2').hover(function() {
-
- $('#text1').fadeOut(1000, function() {
- $('#text2').fadeOut(1000, function() {
- $('#text3').fadeIn(1000);
- });
- });
-
- },function(){
- $('#text3').fadeOut(1000,function() {
- $('#text1').fadeIn(1000); });
- });
- });
- </script>
- <style type="text/css">
- #text2,#text3{
- display:none;
- }
- </style>
- </head>
- <body>
- <h2 id="text1">When no hover</h2>
- <h2 id="text2">When hover element1</h2>
- <h2 id="text3">When hover element2</h2>
- <ul>
- <li><a id="element1" href="#">Show text2</a></li>
- <li><a id="element2" href="#">Show text3</a></li>
- </ul>
- </body>
- </html>