停止动画jQ

时间:2008-12-18 12:00:50

标签: javascript jquery

我遇到的问题是我不知道如何使用鼠标悬停停止我的功能并使用mouseout重启它

首先是我的测试代码:

                     

  <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>

我是如何做到这一点来停止我的函数fadeEngine如果我越过contentdiv并启动它,如果我移出div?

非常感谢你的帮助

4 个答案:

答案 0 :(得分:1)

为您的所有#fade X元素提供一个类(比如.faders),然后使用:

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

或者给容器div一个像#faderbox这样的id并说:

$('#faderbox div').stop();

答案 1 :(得分:0)

我不确定您在fadeEngine中关于fadeIn和fadeOut效果的确切想要发生什么,但是,我可以给你两条建议:

您可以使用jQuery效果stop()来停止所选元素上的所有当前jQuery动画。例如:

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

将停止当前状态下该元素的淡入淡出动画。然后,您可以根据需要重置CSS。

要停止以前使用setTimeout排队的函数,必须获取返回值并调用clearTimeout()。例如:

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

这将清除待处理的超时事件并阻止它发生。

答案 2 :(得分:0)

如果仅仅是将动画附加到鼠标上的行为等,请尝试以下操作:

$(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);
});

答案 3 :(得分:0)

尝试将停止行为应用于需要它的每个元素,例如

$('.faders').each(function () {
  $(this).mouseover(function () {
    $(this).stop(); 
  });
});
相关问题