0

ive got the problem that i dont know how to stop my function with mouseover and restart it with mouseout

first here is my test-code:

  <script type="text/javascript">

  function fadeEngine(x) {

  var total_divs=3; //setze hier die nummer der gewollten divs

                  var y=x;

                  if(x==total_divs) y=1; else y++;

                  $("#fade"+x).css("display","none");

                  $("#fade"+y).fadeIn("slow");

                  setTimeout('fadeEngine('+y+')',3000); //modifi alle 3000 miliseconds nen neuen div
          }

        fadeEngine(0); //Initialisation des Scripts

  </script>

<script type="text/javascript">
$(document).ready(function(){
/*
    $("#container").hover(function(){
            stop('mouse over');
    },function(){
            alert('mouse out');
    });
*/
/*
$("#container").hover(function()
   {
      $(this).stop().fadeTo("slow", 1.00);
   },
   function()
   {
      $(this).stop().fadeTo("fast", 0.50);
   });
*/



});

</script>
</head>
<body>

<div id="container" style="width:200px;height:200px;background:#afafaf;color:#red;">

    <div id="fade1">Content  one</div>
    <div id="fade2" style="display:none">Content  two</div>
    <div id="fade3" style="display:none">Content three</div>
</div>
    <div class="blocker">&nbsp;</div>
</body>
</html>

How i can do this to stop my function fadeEngine if im go over the contentdiv and start it if im move out of the div?

thanks a lot for help

Adam Bellaire
  • 108,003
  • 19
  • 148
  • 163

4 Answers4

1

Give all of your #fadeX elements a class (say .faders) and then use:

$('.faders').stop();

Or give the container div an id like #faderbox and say:

$('#faderbox div').stop();
geocar
  • 9,085
  • 1
  • 29
  • 37
0

I'm not sure exactly what you want to happen with regards to your fadeIn and fadeOut effects in your fadeEngine, however, I can give you two pieces of advice:

You can use the jQuery effect stop() to stop all current jQuery animations on selected elements. For example:

$("#fade"+y).stop();

Will stop the fading animation for that element in its current state. You can then reset the CSS if you wish.

To stop a function from being called that you previously queued with setTimeout, you must obtain the return value and call clearTimeout(). For example:

var timeout = setTimeout('fadeEngine('+y+')',3000);
// later...
clearTimeout(timeout);

This will clear the pending timeout event and prevent it from occurring.

Adam Bellaire
  • 108,003
  • 19
  • 148
  • 163
0

If it's simply a case of attaching the animation to the mouse over bevahiour etc try this :

$(this).mouseover(function () {

    // stops the hide event if we move from the trigger to the popup element
    if (hideDelayTimer) clearTimeout(hideDelayTimer);

    // don't trigger the animation again if we're being shown, or already visible
    if (beingShown || shown) {
      return;
    } else {
      beingShown = true;

      // (we're using chaining) now animate
      this.animate({
        //some animation stuff
      }, function() {
      // once the animation is complete, set the tracker variables
        beingShown = false;
        shown = true;
      });
    }
}).mouseout(function () {

    // reset the timer if we get fired again - avoids double animations
    if (hideDelayTimer) clearTimeout(hideDelayTimer);

     // store the timer so that it can be cleared in the mouseover if required
    hideDelayTimer = setTimeout(function () {
      hideDelayTimer = null;
      this.animate({
         //some animation stuff
      }, function () {
      // once the animate is complete, set the tracker variables
        shown = false;
         });
      }, hideDelay);
});
0

Try applying the stop behaviour to each element that requires it e.g.

$('.faders').each(function () {
  $(this).mouseover(function () {
    $(this).stop(); 
  });
});