JQuery动画 - 延迟问题

Dav*_*vid 1 jquery menu delay

我有这个网站:

http://p33.yamandi.com/

几乎一切都很好,除了一个小烦人的东西 - 延迟.如果单击Rossman菜单项,则使用"zamknij"图标将其关闭,然后尝试一次单击另一个菜单项.你会注意到1-2秒的延迟.我不知道这个问题的原因是什么.它发生在所有浏览器中.有人可以帮忙吗?

问候,大卫

ros*_*dia 11

不完全确定这是否是你的问题,但尝试stop()animate()你动画的所有元素之前调用.像这样的东西:

$(window).load(function() {
  mCustomScrollbars();
  $(function(){
    $("ul#menu a").click(function() {
      myId = this.id;
      $('.text').stop();
      $("ul#menu, h1#logo").stop().animate({left: '-=572'}, 500, function() {
        $("#lang").css("display", "none");
        $("#"+myId+"pr").stop().animate({left: 0}, 200);
        if(myId == "dojazd") {
          $("#outer-mapka").css("left", "50%").css("margin-left", "-213px");
        } else {
          api.goTo(myId.charAt(myId.length-1));
        }
      });
    });
    $("a.close").click(function() {
      api.goTo(1); 
      $(".text").stop().animate({left: "-=950"}, 200, function() { 
        $(".text, #outer-mapka").css("left", "-950px");
        $("ul#menu, h1#logo").stop().animate({left: '0'}, 500, function() {} );
        $("#lang").css("display", "block");
      });
    });
  });
});
Run Code Online (Sandbox Code Playgroud)


Tan*_*ury 5

虽然stop()之前添加animate()将解决问题,但可能值得了解问题的根源.

在这种情况下,用户观察到动画中的延迟,因为多个动画在其之前的队列中.

队列备份的一个贡献者,因为每个动画元素执行一次动画函数完成调用,而不是整个动画执行一次.在以下示例中,将调用两次完整调用,然后调用另一个动画,从而导致4个动画进入队列.

例如:

$("ul#menu a").click(function() {
  myId = this.id;
  $("ul#menu, h1#logo").animate({left: '-=572'}, 500, function() {
    // This code will run twice, once when ul#menu finishes animating
    // and once when h1#logo finishes animating.  Is this the desired
    // behavior?  Is it safe to call api.goTo() twice?       
    $("#lang").css("display", "none");
    $("#"+myId+"pr").animate({left: 0}, 200);

    if(myId == "dojazd") {
      $("#outer-mapka").css("left", "50%").css("margin-left", "-213px");
    }
    else {      
      api.goTo(myId.charAt(myId.length-1));      
    }  
  });
});
Run Code Online (Sandbox Code Playgroud)

队列备份的另一个贡献者和主要贡献是由于通用选择器.在下面的示例中,单击关闭的链接时,会导致所有7个文本类生成动画,完成后会生成2个以上的动画.导致21个动画:

$("a.close").click(function() {
  api.goTo(1);
  // The .text selector matches seven different elements.  Thus, a when
  // clicking the close link, seven animations are added to the queue.
  $(".text").animate({left: "-=950"}, 200, function() { 
    $(".text, #outer-mapka").css("left", "-950px");
    // And two more animations are added to the queue.
    $("ul#menu, h1#logo").animate({left: '0'}, 500, function() {} ); 
    $("#lang").css("display", "block");
  });
});
Run Code Online (Sandbox Code Playgroud)

因此,当您单击菜单,关闭页面,然后再次单击菜单时,可以观察到等待21个动画通过队列的延迟.

要解决此问题,可以使用标志来指示是否应运行完整函数.此外,在选择器上更具体,可以帮助防止不必要的调用.以下是实现两者的可能解决方案:

$(window).load(function() {
  mCustomScrollbars();
  $(function(){         
    var menu_visible=true; // Flag to indicate if menu is visible.
    $("ul#menu a").click(function() {
      myId = this.id;      
      $("ul#menu, h1#logo").animate({left: '-=572'}, 500, function() {
          // If the menu is not visible, then return as this function has
          // already ran.
          if (!menu_visible) return;

          // Will be hiding the menu, so set the flag to false.
          menu_visible = false;

          $("#lang").css("display", "none");
          $("#"+myId+"pr").animate({left: 0}, 200);

          if(myId == "dojazd") {
            $("#outer-mapka").css("left", "50%").css("margin-left", "-213px");
          }
          else {          
            api.goTo(myId.charAt(myId.length-1));          
          }
        });
    });

    // For each text class.
    $(".text").each(function() {
      // Store a handle to the text.
      var $text=$(this);
      // Add a click event to a close link within the text.
      $("a.close", $text).click(function() {
        api.goTo(1); 
        // Only animate the text containing the close link.
        $text.animate({left: "-=950"}, 200, function() { 
          $(".text, #outer-mapka").css("left", "-950px");
          $("ul#menu, h1#logo").animate({left: '0'}, 500, function() {
            // The menu is visible so set the flag.
            menu_visible=true;
          }) ;
          $("#lang").css("display", "block");
        });
      });
    });  
  });
});
Run Code Online (Sandbox Code Playgroud)