Performance issues with transitions
<p>I have code that works here, but I am getting some performance issues and I would like to ask more experienced developers if there is something I could be doing to improve it. My code is fairly simple. What I want to achieve is that when you click on an image, the image slides down and another one slides in it's place. My first approach was this:</p>

<p>html</p>

<pre><code>&lt;div id="myDiv"&gt;

    &lt;div class="page page-1"&gt;&lt;img src="img1.jpg" width="100%"&gt;&lt;/div&gt;
    &lt;div class="page page-2"&gt;&lt;img src="img2.jpg" width="100%"&gt;&lt;/div&gt;

&lt;/div&gt;
</code></pre>

<p>css</p>

<pre><code>.page-2 {display: none;}

.page-moveFromTop {
    -webkit-animation: moveFromTop 2s ease both;
    animation: moveFromTop 2s ease both;
}

.page-moveToBottom {
    -webkit-animation: moveToBottom 2s ease both;
    animation: moveToBottom 2s ease both;
}


@keyframes moveFromTop {
    from { -webkit-transform: translateY(-100%); transform: translateY(-100%); }
}

@keyframes moveToBottom {
    from { }
    to { -webkit-transform: translateY(100%); transform: translateY(100%); }
}
</code></pre>

<p>then on my js:</p>

<pre><code>$(document).ready( function () {


   $('#myButton').click(function(){

      $('.page-1').addClass('page-moveToBottom');

      $('.page-2').css('display', 'block');

      $('.page-2').addClass('page-moveFromTop');

   });
});
</code></pre>

<p>So this is working, but i am getting issues in which sometimes there is a quick flick of the second image as I press the button and before the transition happens, and sometimes the transition is bypassed altogether. This only happens about 20% of the time, but it's still not good enough. I thought maybe the problem was the fact that the display was set to "none" to then be applied by the javascript which I've read is sometimes not the best option so I've tried a different approach by removing the second image from the html altogether:</p>

<pre><code>&lt;div id="myDiv"&gt;

    &lt;div class="page page-1"&gt;&lt;img src="img1.jpg" width="100%"&gt;&lt;/div&gt;

&lt;/div&gt;
</code></pre>

<p>and the </p>

<pre><code>.page-2 {display: none;}
</code></pre>

<p>from my css and modifying the javascript using the append() function instead:</p>

<pre><code>$(document).ready( function () {

   $('#myButton').click(function(){

      $('.page-1').addClass('page-moveToBottom');

      $('&lt;div class="page page-2"&gt;&lt;img class="image" src="ecran2.jpg" width="100%"&gt;&lt;/div&gt;').appendTo("#pt-main");

      $('.page-2').addClass('page-moveFromTop');
      });
 });
</code></pre>

<p>Again, this also works, but I am getting the inconsistency issues I mentioned before. So what I would like to know is if there is a best practice way to do this that is more reliable when it comes to performance. Thanks for your time. P</p>