I'm experiencing a sudden appearance of headers just before the animation slides them in from the side. Is there a way to remove this?

I'm currently in the process of creating a personal website to showcase all my projects and resume. To add some visual appeal, I've incorporated animations to slide in the title elements from the sides upon loading. However, I've noticed that when the page first loads, there's a quick flash of the title text before the JavaScript kicks in to animate them. Could this be due to a preloading issue that I haven't been able to resolve?

Could it possibly be an ordering problem with how the elements/JavaScript are loaded? Or am I overlooking a specific line in my JavaScript code that could prevent this flashing effect?

window.addEventListener('DOMContentLoaded', function () {

    /* Function to bring in navbar from top staggered */
    gsap.from(".btn",{duration:2,opacity:0,y:-300,stagger:0.25,});

    /* Function to bring h1 in from the left */
    gsap.from("h1",{duration:2.5,ease:"power2.out",x:-2000});

    /* Function to bring in h2 from right */
    gsap.from("h2",{duration:2.5,ease:"power2.out",x:2000});

    /* Function to register gsap scroll plugin */
    gsap.registerPlugin(ScrollToPlugin);

    /* Function to scroll to About me when button is clicked */
    document.getElementById('scroll').addEventListener('click', function clicked() {
    gsap.to(window, {duration: 0.8, scrollTo:'#red' });
    });

});
* {
    box-sizing: border-box;
}

body {
    background-color: #24305E;
    height: 100vh;
}

h1 {
    color: #A8D0E6;
    font-display: fallback;
    font-size: 15vh;
    font-family: 'Noto Sans SC', sans-serif;
    margin-left: 12vw;
    text-shadow: 
    -1px 1px 1px rgba(0,0,0,1),
    -2px 2px 1px rgba(0,0,0,0.6),
    -4px 4px 1px rgba(0,0,0,0.4),
    -6px 6px 1px rgba(0,0,0,0.2),
    -8px 8px 1px rgba(0,0,0,0.18),
    -10px 10px 1px rgba(0,0,0,0.16),
    -12px 12px 1px rgba(0,0,0,0.15),
    -14px 14px 1px rgba(0,0,0,0.14),
    -16px 16px 1px rgba(0,0,0,0.13),
    -18px 18px 1px rgba(0,0,0,0.12),
    -20px 20px 1px rgba(0,0,0,0.11),
    -22px 22px 1px rgba(0,0,0,0.1),
    -24px 24px 1px rgba(0,0,0,0.09),
    -26px 26px 1px rgba(0,0,0,0.08),
    -28px 28px 1px rgba(0,0,0,0.07),
    -30px 30px 1px rgba(0,0,0,0.06),
    -32px 32px 1px rgba(0,0,0,0.05),
    -34px 34px 1px rgba(0,0,0,0.04),
    -36px 36px 1px rgba(0,0,0,0.03),
    -38px 38px 1px rgba(0,0,0,0.02);
} 

// remaining CSS properties removed for brevity

</body>
</html>

Answer №1

According to the documentation:

The DOMContentLoaded event is triggered when the main HTML document has finished loading and parsing, regardless of whether stylesheets, images, or subframes have completed loading.

This means that your webpage will be visible to users before the callback function is run.

In this case, I recommend using gsap.to () instead. You can define the initial position in your CSS and then animate it with gsap.to.

Answer №2

To optimize the loading of your website, consider moving the

<script src="main.js"></script>
tag to the bottom of the HTML body. This way, all other content will load before the script runs. I recently added some animations to a project and found this method effective:

window.onload(animate());

function animate(){
    const timeline = gsap.timeline({defaults: {ease: "power1.out"}});
    
    timeline.to(".intro", {y: "-100%", duration: 1, delay: 1});
    timeline.fromTo(".intro", {opacity: 1}, {opacity: 0, duration: 1}, "-=1");
    timeline.fromTo(".card", {y: "100%"}, {y: "0%", duration: 1, stagger: 0.25}, "-=1");
    timeline.fromTo("#logo-blk", {y: "-400%", rotation: "-30"}, {y: "-100%", rotation: "0", 
        duration: 0.5}, "-=0.5");
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating divs with randomized positions using JS and jQuery

I encountered a problem with my code when running it on certain viewport sizes. It seems to freeze, requiring me to close the tab. I attempted to implement a backup script that would terminate the loop if it froze, but it hasn't been effective. Coul ...

What is the best way to level out the content in the article using CSS and HTML?

Can anyone help me with CSS and HTML? I have a travel blog and I want to include a related post section similar to Time.com in my articles, but I'm facing an issue. What do I need to achieve this? I want to create a section that looks like the relat ...

Attempting to add a variable into an array results in an invalid operation

Attempting to display the message counter on the navbar, but I believe I'm doing it the wrong way. I've tried searching for a solution to this error online, but can't seem to find the correct method to insert the $rec_count variable. Fatal ...

The radio buttons are spread out too widely

While working on my website, I encountered an issue with a form that requires the user to input their name, email, gender, and date of birth. I utilized label for="" to ensure the CSS aligns accurately in each section. <form> <table> ...

The process of transferring information from a JSON API to TypeScript models

When working with my JSON API in my services, I need to pass the data to my models. What is the most efficient way to accomplish this task? Currently, I am following this process: import { Attachment } from '.'; export class Contact { id: nu ...

I seem to be experiencing difficulty receiving the JavaScript alert on my controller page

In my ActionResult function, I have the following code: public ActionResult Copy( int bvVariableid ) { var iReturn = _bvRepository.CopyBenefitVariable( bvVariableid, CurrentHealthPlanId, CurrentControlPlanId, _bvRepository.GetSecInfo( ).UserId ...

Troubleshooting the NestJS @InjectModel() dependency issue: Solutions and Fixes

userLink.api.ts import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document, Types } from 'mongoose'; import { User } from 'src/users/schemas/users.schema'; export type UserLinkDocument = UserLink & ...

How can I make the left column stay in place while allowing the right column to scroll in Bootstrap 4 in a

I'm currently working with Angular 4 and Bootstrap 4, attempting to create a layout with a fixed scrollable right div and a fixed left div. I want it to resemble the design on Trulia. Since Bootstrap removed the Affix jQuery plugin in version 4, usin ...

Enhancing Chat: Updating Chat Messages in Real-Time with Ember.js and Firebase

I recently started working with ember.js and firebase. Right now, I have successfully implemented a feature to post messages and view them in a list format as shown below: //templates/show-messages.hbs {{page-title "ShowMessages"}} <div clas ...

Masonry layout organizes images in a vertical stack, with one image per row

After implementing Masonry on my website, I encountered an issue where all the images stack vertically in a single row once the page loads. Instead, I would like them to stack both horizontally and vertically. You can view the problem on My Jsfiddle. This ...

WebClient executes JavaScript code

On my aspx page, there are JavaScript functions that handle paging. I am currently using the WebBrowser control to run these JavaScript functions by calling WebBrowser1_DocumentCompleted. WebBrowser1.Document.Window.DomWindow.execscript ("somefunction(); ...

Response from plupload in JSON format

I'm having trouble converting the response into a json object. Within the ajax function (url parameter for plupload), the response is echoed in this format: echo json_encode(array( 'foo' => 3434, 'error' => 'om ...

Is it possible to showcase a FullCalendar event in full width while being positioned behind other events scheduled for the same time slot?

Is there a way to customize FullCalendar so that concurrent events in the same timeslot are not vertically divided? I have a Google calendar named "time-blocking template" where I want all events to take up 100% of the timeslot width, even if there are ot ...

Browsersync and Gulp Continuously Refreshing CSS

My Gulp and Browsersync setup is causing the CSS in the Style.css file to reset every time I run npm start. While the changes to index.html persist and I can successfully push the initial CSS changes to Github, I am puzzled why the CSS additions keep reset ...

Guide on looping through an array of objects and displaying the key and value pairs using pug

I'm having trouble displaying errors from an incomplete form. In my array of objects, I want to iterate through and retrieve the errors, but it seems the list is currently empty. Upon debugging, it appears that the errors variable contains [ { e ...

Is there a way to create a Swiper slider similar to the one used in the App Store

I am looking to create a slider using Swiperjs similar to the carousel on the Apple App Store (seen on the Games tab). I attempted to implement it using Vue Swiper, a package for vue, as shown below: HTML code: <div id="app"> <h1>Slider< ...

Printing documents from a database using Mongoose version 7.3.1: A comprehensive guide

Currently, with Mongoose 7.3.1, I have inserted some documents into the MongoDB database. However, when I try to log these documents using console.log() by using Fruit.find({}) and outputting it to the console, I get a massive dataset of unwanted objects. ...

Angular Protractor: Leveraging Browser Context for Executing Scripts

Within my index.html file, I explicitly define the following: window.myAppInstance = new MyApp.myAppConstructor(); In the todo-spec.js file, I set up the following structure: describe('verifying my web page', function() { it('should con ...

error being triggered due to relative positioning

On my webpage, I have a set of buttons located at the bottom. When I first open the page, all the buttons are displayed properly. However, sometimes the first three buttons become hidden mysteriously and the layout changes. I am puzzled as to what could be ...

What could be causing the Vue Js computed message to not appear on the webpage?

Can anyone help me figure out why the text in a Vue instance isn't displaying correctly in a h1 element when I try to reverse it? new Vue({ el: '#comp-prop2', data: { message: 'Hello World...again!' ...