title: How do you combine CSS & JS fade out onClick + fade in on page load?
body: <p>I'm trying to accomplish the following on my website:</p>
  <ol>
  <li>When a button is clicked (onClick), fade out the entire screen to black.</li>
  <li>Then fade in from black to load the next page (the URL that the button linked to).</li>
  </ol>
  <p>I'm finding solutions to points 1 and 2 separately, but no solution that combines these. See below. Can someone help achieve 1 and 2 following each other?</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>/*JS code I found online to create a fade out to black onClick */
  
  $(document).ready(function() {
        function toggle() {
          $('#overlay').toggleClass('backdrop');
          }
        $('[data-toggle="active"]').click(toggle);
      });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>/*CSS code #1 I found online to create a fade out to black onClick */
  
      #overlay {
          width: 100%;
          height : 100%;
          opacity : 0;
          background: '#000';
          top: 0;
          left: 0;
          transition: opacity .25s;
          -moz-transition: opacity .25s;
          -webkit-transition: opacity .25s;
      }
      .backdrop {
          opacity: .4 !important;
      }
  
  /*CSS code #2 I found online to create a fade in on pageload */
  
      #overlay {
      -webkit-animation: fadein 2s; /* Safari, Chrome and Opera &gt; 12.1 */
         -moz-animation: fadein 2s; /* Firefox &lt; 16 */
          -ms-animation: fadein 2s; /* Internet Explorer */
           -o-animation: fadein 2s; /* Opera &lt; 12.1 */
              animation: fadein 2s;
  }
  
  @keyframes fadein {
      from { opacity: 0; }
      to   { opacity: 1; }
  }
  
  /* Firefox &lt; 16 */
  @-moz-keyframes fadein {
      from { opacity: 0; }
      to   { opacity: 1; }
  }
  
  /* Safari, Chrome and Opera &gt; 12.1 */
  @-webkit-keyframes fadein {
      from { opacity: 0; }
      to   { opacity: 1; }
  }
  
  /* Internet Explorer */
  @-ms-keyframes fadein {
      from { opacity: 0; }
      to   { opacity: 1; }
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>   
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;body&gt;
          &lt;a class="btn btn-default" data-toggle="active" href="nextpage.com"&gt;Button&lt;/a&gt;
          &lt;a class="btn btn-default" data-toggle="active" href="nextpage2.com"&gt;Button&lt;/a&gt;
          &lt;div id="overlay"&gt;&lt;/div&gt;
      &lt;/body&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>The above JS code goes with CSS code #1, but that only creates a toggle function (<a href="https://stackoverflow.com/questions/24982924/css-jquery-background-fade-to-black-upon-clicking-button-unknown-bugs">source</a>).</p>
  <p>For the fade in on page load, I found CSS code #2 as seen above, but that doesn't fade in from black <a href="https://stackoverflow.com/questions/11679567/using-css-for-a-fade-in-effect-on-page-load">source</a>:</p>