Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript How to make CSS animation show on page for a set time on page reload

so I have a js loading animation with a progress bar that I want to show on page load. I put this under the JS section because I assume js is needed for this to work. If the page loads quicker than the 1s animation, then I want it to hide everything else and play the animation, then show content of the page. I built a sample loading animation for testing. Thank you!
HTML:
<!DOCTYPE html>
<html>
    <head>
        <style>
            #container {
                height:50vw;
                width:100%;
            }
            #progress-bar-container {
                justify-self: center;
                height:30px;
                width:300px;
                border:2px solid gray;
            }
            #progress-bar {
                animation: webpage-loading 1s forwards;;
                animation-duration: 1s;
                justify-self: left;
                width:10px;
                height:30px;
                background-color:black;
            }
            
            @keyframes webpage-loading {
                0% {width:10px}
                30% {width:90px}
                45% {width:150px}
                60% {width:190px}
                75% {width:230px}
                100% {width:300px}
            }
            #progress-percentage {
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                font-weight: bold;
            }
        </style>       
    </head>
    <body>
        <div id="container">
            <div id="progress-bar-container">
                <div id="progress-bar">
                </div>
            </div>
        </div>
    </body>
</html>
 
Hey there!

Got it. Let's dive right in. Think of your page as a theater. When someone enters, the curtains (your loading animation) are the first thing they see, teasing an exciting performance (your page content).

Here's the plan:

  1. Curtains Up: At the beginning, the curtains are the star of the show, and the main act (your webpage content) is waiting behind them.
  2. Showtime: After the curtains finish their 1-second dance, it's time to check if the main act is ready. If so, curtains go up! If not, the curtains keep everyone entertained until the main act's set.
  3. Full Performance: Even if the main act is ready super early, we'll ensure the curtains dance for the whole 1-second, giving everyone the complete experience.
And now, the code that makes the magic happen:

HTML:
<!-- Your HTML structure with the loader and content -->
<div id="loader">
    <div id="container">
        <!--... your progress bar code ...-->
    </div>
</div>
<div id="content" style="display: none;">
    Your amazing page content here!
</div>

<script>
    // Here's our JS director, ensuring everything goes smoothly:
    window.addEventListener('DOMContentLoaded', function () {
        // Let the curtains dance for at least 1 second
        setTimeout(function () {
            document.getElementById('loader').style.display = 'none';  // Hide the curtains
            document.getElementById('content').style.display = 'block';  // Reveal the main act
        }, 1000);  // That's the 1-second timer!
    });
</script>


There you have it! A mini theater experience for your users. If you run into any snags or need more backstage details, I'm here to help. Break a leg! 💪🎭
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom