【问题标题】:JS Break my Random functionJS 打破我的随机函数
【发布时间】:2012-11-28 22:46:37
【问题描述】:
    $(document).ready(function() {

  elem = new Array('a1','a2','a3','a4','a5','a6','a7','a8','a9');
  $('.graphic').hide();

  hidden = false;
  time = 0;
  elem.sort(randomize);
  $.each(elem, function(i, r) {
    setTimeout(function() {
      $('#'+r).fadeIn(400);
    }, time);
    time += 200;
  });

  time = 0;
  //elem.sort(randomize);
  setTimeout(function() {
    $.each(elem, function(i, r) {
      setTimeout(function() {
        $('#'+r).fadeOut(800);
      }, time);
      time += 200;
    });
    $('.graphic').fadeIn(2400);
      hidden = true;
  }, 3000);

  $('.graphic').mouseenter(function(){
    if(hidden) {
      //time = 0;
      //elem.sort(randomize);
      $('.graphic').fadeOut(400);
      $.each(elem, function(i, r) {
        /*setTimeout(function() {
          $('#'+r).fadeIn(400);
          }, time);
        time += 200;*/
        $('#'+r).fadeIn(400);
      });
      hidden = false;
    }
  });

  $('.content').mouseenter(function(){
    if(!hidden) {
      time = 0;
      //elem.sort(randomize);
      $.each(elem, function(i, r) {
        setTimeout(function() {
          $('#'+r).fadeOut(800);
        }, time);
        time += 200;
      });
      $('.graphic').fadeIn(2400);
      hidden = true;
    }
  });

  $('.tile').click(function(t) {
    $(this).fadeOut(800, function () {
        window.location = $(this).attr("href");
      }
    ); 
    return false;
  });


  function randomize(){
    return (Math.round(Math.random())-0.5); 
  }

});

由于某种原因,我无法打破效果。 当侧面加载时,横幅被隐藏。 9个元素来来去去。 每次进入 div 时,banner 都会淡出,而 9 个 div 会进入。 当我离开输入时,有一个随机的淡出效果。 但是,当我在这种效果进行时进入,一切都会中断。 所以我需要在那里的某个地方休息一下,这样褪色就会停止,它会立即显示我的 9 个 div。 有任何想法吗? :/

【问题讨论】:

  • 尝试使用动画回调,或者.delay(),而不是那么多setTimeouts。通过这种方式使用效果队列,您将能够轻松.stop()它。

标签: javascript function break


【解决方案1】:

也许尝试使用http://api.jquery.com/stop/ 来停止当前正在运行的动画

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 2016-11-02
    • 2019-06-17
    相关资源
    最近更新 更多