Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While adding <code>stop()</code> before <code>animate()</code> will resolve the problem, it may be worth understanding the source of the problem.</p> <p>In this case, the user observes a delay in the animation because multiple animations are in the queue before it.</p> <p>One contributor to the queue backing up because the animation function complete call is executed once per animated element, not once for the animation as a whole. In the following example, the complete call is invoked twice, which in turn invokes another animation, causing 4 animations to enter the queue.</p> <p>For example:</p> <pre><code>$("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)); } }); }); </code></pre> <p>Another contributor to the queue backup, and the primary contribution, is because of a generic selector. In the following example, when a closed link is clicked, it causes all 7 text classes to animate, and when they complete, they cause 2 more animations. Resulting in 21 animations:</p> <pre><code>$("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"); }); }); </code></pre> <p>Thus, when you clicked a menu, closed the page, then clicked the menu again, a delay could be observed waiting for the 21 animations to go through the queue.</p> <p>To resolve this, one can use a flag to indicate if the complete function should run. Additionally, being more specific on the selectors can help prevent unnecessary invocations. Here is a possible solution implementing both:</p> <pre><code>$(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"); }); }); }); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload