javascriptcsstransitionpage-transition

Call Javascript Function on hyperlink click and wait for animation to finish?


I have a page-out transition animation that I am using with css. It relies on changing the class of a container to start a css animation. I'm looking for a way to activate this animation (by adding the class to the container) then waiting for it to finish, then go to the link the href has. Here is the JSFiddle.

       <div class="navigationbar">
            <a href="home.html">
                <h2 class="logo">The Lowdown.</h2>
            </a>    
            <ul>
                <li><a href="text.html">CONTACT US</a></li>
                <li><a href="edition.html">EDITIONS</a></li>
                <li><a href="articles.html">ARTICLES</a></li>
                <li><a href="#aboutus">ABOUT US</a></li>
                <li><a class="active" href="https:/google.com">HOME</a></li>
                
              </ul>
        </div>

        
<div class="loader loader--active">
    <div class="loader__icon">
    </div>
    <div class="loader__tile"></div>
    <div class="loader__tile"></div>
    <div class="loader__tile"></div>
    <div class="loader__tile"></div>
    <div class="loader__tile"></div>
    <div class="loader__tile"></div>
  </div>

Is there any way to do this?


Solution

  • Can you try the below solution, if it works for you

    var el = document;
    //el.onclick = showFoo;
    
    $(document).ready(function(){
    $(".jqLink").on("click",function(event){
    var myRef=this;
    console.log("ref=>",$(myRef).attr("href"));
    showFoo(event);
    setTimeout(function(){
    
     $loader.classList.remove('loader--active');
     
     setTimeout(function(){
     window.location.href=$(myRef).attr("href");
     },1000);
    },2000);
    });
    
    })
    
    function showFoo(e) {
      $loader.classList.add('loader--active');
      return e.preventDefault();
    }
    
    var $loader = document.querySelector('.loader')
    
    window.onload = function() {
      $loader.classList.remove('loader--active')
    };
    
      window.setTimeout(function () {
        $loader.classList.remove('loader--active')
      }, 2000)
    

    Add "jqLink" css class to your every element, like below

     <li><a href="text.html" class="jqLink">CONTACT US</a></li>
      <li><a href="edition.html" class="jqLink">EDITIONS</a></li>
      <li><a href="articles.html" class="jqLink">ARTICLES</a></li>
      <li><a href="#aboutus" class="jqLink">ABOUT US</a></li>
      <li><a class="active" href="https:/google.com" class="jqLink">HOME</a></li>
    

    Try this, if it works for you.