Search code examples
javascriptanimationgsap

Page transition - wait until the ajax request is made


I am trying to reproduce this animation in a project - https://codepen.io/victorwork/pen/bQPBvQ

the goal is that during the ajax request to get the new page, the screen with the black background and the logo and only when the ajax request is finished is that the curtain disappears.

I have already tried with .pause () and then use .resume () but it does not work very well in pause times, and there are times when resume is faster.

I already tried to divide the timeline, in which one function has the beginning of the animation and in another the end, the first time turns out well, but the following ones do not and I can not quite understand why.

any suggestion?

here's the code.

const entryTransitionPage = new TimelineMax( { paused: true });

entryTransitionPage
    .fromTo(transitionElementBlue , 1.2, { scaleX: 0 },{ scaleX: 1, transformOrigin:'left', ease: Power4.easeInOut},)
    .fromTo(transitionElementBlack , 1.2, {scaleX: 0},{scaleX: 1, transformOrigin:'left', ease: Power4.easeInOut}, .2)
    .fromTo(transitionContent , .6, {xPercent: -100, autoAlpha:0 }, {xPercent: 0, autoAlpha:1, ease: Power4.easeInOut}, .7)
    .set(transitionElementBlue, { scaleX:0 })
    .to(transitionElementBlack , 2, { scaleX: 0, transformOrigin:'right', ease: Power4.easeInOut })
    .to(transitionContent , .2, { autoAlpha:0 }, '-=1.2');



const entryPageAnimation = () => {
    entryTransitionPage.play(0);
    setTimeout(() => { entryTransitionPage.pause()}, 2000);
}

const endPageAnimation = () => {
    entryTransitionPage.resume();
}


const changePage = (url) => {
    entryPageAnimation();
    loadPage(url) 
}


const loadPage = (url) => {
  const xhr  = new XMLHttpRequest();
    xhr.onerror = () => { throw 'Request failed. HTTP code ' + xhr.status; };

    xhr.onload = () => {

        if (!xhr.status || (xhr.status >= 400)) throw 'Request failed. HTTP code ' + xhr.status;

        const documentAjax = (new DOMParser()).parseFromString( xhr.response,'text/html');
        document.body.className = documentAjax.body.className;
        const pageContent = documentAjax.getElementById('pageContent');
        const newPage = documentAjax.getElementById('pageSelector');
        const page = document.getElementById('pageContent');

        exitPageAnimation();
        setTimeout( () => {
          window.scrollTo(0, 0);
          if (newPage)  page.innerHTML = newPage.outerHTML;

        }, 700);
    }

    xhr.open('GET', url, true);
    xhr.send();

}

by the way, is there any way to just insert the contents of the new page, just when the transition animation is finished, other than the timeout?


Solution

  • What about splitting the animation into two different animations - startLoadingAnimation and stopLoadingAnimation. Where the first animation will just display the black overlay and the loading text and the second one will hide those.

    Check my snippet and implement you HTML fetch in getPageAsync() function.

    Basically there are 3 promises that we need to fulfil when new page HTML is requested.

    1. Start animation complete.
    2. Get new page HTML complete.
    3. Stop animation complete.

    var page = document.querySelector('.page');
    var loadingText = document.querySelector('.transition__loding');
    var frameBlack = document.querySelector('.page-transition__black');
    var frameRed = document.querySelector('.page-transition__red');
    var button = document.querySelector('#button');
    var startLoadingPromise;
    var stopLoadingPromise;
    
    var startLoadingAnimation = new TimelineMax({
        paused: true,
        onComplete: startLoadingComplete
      })
      .fromTo(frameRed, 2.2, {
        scaleX: 0
      }, {
        scaleX: 1,
        transformOrigin: 'left',
        ease: Power4.easeInOut
      })
      .fromTo(frameBlack, 2.2, {
        scaleX: 0
      }, {
        scaleX: 1,
        transformOrigin: 'left',
        ease: Power4.easeInOut
      }, .2)
      .fromTo(loadingText, 1.6, {
        xPercent: -100,
        autoAlpha: 0
      }, {
        xPercent: 0,
        autoAlpha: 1,
        ease: Power4.easeInOut
      }, .7);
    
    var stopLoadingAnimation = new TimelineMax({
        paused: true,
        onComplete: stopLoadingComplete
      })
      .set(frameRed, {
        scaleX: 0
      })
      .to(frameBlack, 2.2, {
        scaleX: 0,
        transformOrigin: 'right',
        ease: Power4.easeInOut
      })
      .to(loadingText, .2, {
        autoAlpha: 0
      }, '-=1.2');
    
    button.addEventListener('click', () => {
      button.style.display = 'none';
      startLoading().then(function() {
        return getPageAsync();
      }).then(function(html) {
        page.innerHTML = html;
        return stopLoading();
      }).then(function() {
        button.style.display = 'initial';
      })
    });
    
    function getPageAsync() {
      return new Promise(function(resolve, reject) {
        setTimeout(function() {
          resolve(getRandomContent());
        }, 500);
      });
    }
    
    function getRandomContent() {
      var text = "";
      var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    
      for (var i = 0; i < 30; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    
      return text;
    }
    
    function startLoadingComplete() {
      startLoadingPromise.resolve();
    }
    
    function startLoading() {
      let res, rej;
      startLoadingPromise = new Promise(function(resolve, reject) {
        res = resolve;
        rej = reject;
      });
    
      startLoadingPromise.resolve = res;
      startLoadingPromise.reject = rej;
    
      startLoadingAnimation.play(0);
    
      return startLoadingPromise;
    }
    
    function stopLoadingComplete() {
      stopLoadingPromise.resolve();
    }
    
    function stopLoading() {
      let res, rej;
      stopLoadingPromise = new Promise(function(resolve, reject) {
        res = resolve;
        rej = reject;
      });
    
      stopLoadingPromise.resolve = res;
      stopLoadingPromise.reject = rej;
    
      stopLoadingAnimation.play(0);
    
      return stopLoadingPromise;
    }
    body {
      overflow: hidden;
    }
    
    .page-transition__black {
      position: absolute;
      left: 0;
      top: 0;
      height: 100vh;
      width: 100vw;
      background: #000;
    }
    
    .page {
      background-color: green;
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      color: white;
      font-size: 30px;
      text-align: center;
      padding-top: 20px;
    }
    
    .page-transition__red {
      position: absolute;
      left: 0;
      top: 0;
      height: 100vh;
      width: 100vw;
      background: red;
    }
    
    .transition__loding {
      text-transform: uppercase;
      font-family: sans-serif;
      font-size: 32px;
      position: absolute;
      z-index: 1;
      color: #fff;
      font-weight: bold;
      top: 50vh;
      left: 50vw;
      transform: translate(-50%, -50%);
    }
    
    button {
      bottom: 15vh;
      left: 5vw;
      text-transform: uppercase;
      font-family: sans-serif;
      font-size: 16px;
      position: absolute;
      z-index: 1;
      color: #fff;
      background: transparent;
      padding: 20px;
      border: 2px solid #fff;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenMax.min.js"></script>
    
    
    
    <div class="page-transition">
      <div class="page">
        Page 1 content ...
      </div>
      <div class="page-transition__red"></div>
      <div class="page-transition__black"></div>
      <div class="transition__loding"> Loading ...</div>
    </div>
    
    <button id="button">Next Page</button>