Dynamic JavaScript Animation

Check out this code snippet I currently have. Notice how the text seems to jump when rerun? Give it a try and see for yourself.

The big question is: How can this be fixed?

$("#aboutUsText").delay(1000).fadeOut(1000)
$("#aboutUsText").attr("MyState", "1")
setInterval(function () {
    $("#aboutUsText").delay(1000).fadeOut(1000)
    var text = $('#aboutUsText');
    if (text.attr("MyState") == "1") {
        text.text('Text 1');
        $("#aboutUsText").delay(1000).fadeIn(1000)
        text.attr("MyState", "2");
    } else if (text.attr("MyState") == "2") {
        text.text('Text 2');
        $("#aboutUsText").delay(1000).fadeIn(1000)
        text.attr("MyState", "3");
    } else {
        text.text('Text 3');
        $("#aboutUsText").delay(1000).fadeIn(1000)
        text.attr("MyState", "1");
    }
}, 3000);


<p id="aboutUsText">Hello</p>

Answer №1

It seems like I understand what you're trying to achieve, so I went ahead and created a quick solution for you. You can check it out here.

var showTime = 1000; // time before fade
var fadeOutDuration = 300;
var fadeInDuration = 500;
var textIncrementer = 1;
AnimateText();

function AnimateText() {

    setTimeout(function() {
        jQuery("#aboutUsText").fadeTo(fadeOutDuration, 0, function() {
            if (textIncrementer == 1) {
                displayText = "Second text";
            }
            if (textIncrementer == 2) {
                displayText = "Third text";
            }    
            if (textIncrementer > 2) {
                displayText = "Fourth+ text";
            } 
            textIncrementer++;
            jQuery("#aboutUsText").html(displayText);
            jQuery("#aboutUsText").fadeTo(fadeInDuration, 1, function(){
                AnimateText();
            });

        });
    }, showTime);
}    

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

Unable to switch between navigation items and icons

Currently, I am in the process of learning vanilla JavaScript and I'm trying to incorporate a navigation feature similar to when the hamburger icon is clicked. However, I have encountered an issue where I cannot toggle the hamburger icon to change it ...

Dealing with Array Splicing Issues in Angular

Being fairly new to the world of AngularJS, I suspect that I am just making a simple mistake. My goal is to splice the cardTypes array at the var newCard = cardTypes.shift(); line rather than using .shift() so that I can consider my ng-repeat index. Whil ...

Having trouble with implementing forEach in Google Script

Hey there, I'm having a syntax error in my GoogleScript. This is actually my first script ever, so I'm a bit lost as to why it's not working. The main goal is to extract data from a Google sheet and use it to create labels based on a documen ...

What is the best way to retrieve AWS secret values using JavaScript?

Having recently started using AWS, I have been able to manually obtain the secret I need. However, when attempting to utilize the code snippet provided by AWS to retrieve the secret value, all my attempts result in undefined. Can someone please point out ...

Display the appropriate selection choices based on the knockout condition

In my HTML page, there is a dropdown with certain conditions that determine which options should be rendered in the select element. My attempt at achieving this: <select> <!-- ko if: condition() --> <option value="1">1& ...

Creating designs on the canvas

With this code snippet, I have the ability to create lines using mouse points on a canvas. My goal is to identify when these lines connect to form a shape and then fill that shape with color. <script language="javascript" src="//ajax.googleapis.com/a ...

Setting the overlay video to match the input video size in FFMPEG

Currently, I am incorporating FFMPEG wasm into a NextJS project. However, I believe that general FFMPEG solutions will suffice since FFMPEG wasm is capable of interpreting standard FFMPEG commands. My objective is to superimpose an overlay video onto the ...

Why isn't the function in my React child component passing its parameters to the parent component's function as expected?

In the parent: const [currentPinPosition, setCurrentPinPosition] = React.useState({ lat: 0 , lng: 0 }); const updateCurrentPinPos = (position) => { console.log(position); setCurrentPinPosition({ lat: position.lat, lng: position.lng }); }; / ...

Troubleshooting a deletion request in Angular Http that is returning undefined within the MEAN stack

I need to remove the refresh token from the server when the user logs out. auth.service.ts deleteToken(refreshToken:any){ return this.http.delete(`${environment.baseUrl}/logout`, refreshToken).toPromise() } header.component.ts refreshToken = localS ...

Exploring the Reach and Sequence of AJAX Callbacks

This particular piece of code aims to achieve three main tasks: 1) validate the online status of users, 2) retrieve their information from a slightly different URL, and 3) present both sets of data in HTML format. The implementation appears functional bu ...

Revise the text while keeping the column content unchanged

Is it possible to change the text in a column without affecting the image using Jquery? Can this be achieved solely with Jquery without any modifications to the HTML code? Check out the demo $(".jsNoWrap").text("Change text"); <script src="https://a ...

The hover functionality fails to activate when a z-index is applied

My issue revolves around 2 divs: one containing a link and the other a box-shaped container. The link is set with position:fixed; causing it to fly over the container div. To resolve this, I attempted to assign a negative z-index value to the link, but unf ...

What is the best way to detect the window scroll event within a VueJS component?

Looking to capture the window scroll event in my Vue component. This is what I have attempted so far: <my-component v-on:scroll="scrollFunction"> ... </my-component> I have defined the scrollFunction(event) in my component methods, but it ...

The personalized confirmation dialog is experiencing malfunctions

I have designed my own custom dialogs using Bootstrap and jQuery, such as Alert and ConfirmDialog. Here is a sample: http://jsfiddle.net/eb71eaya/ The issue I am facing is that in the callback function, I make an AJAX call. If it returns true, I want to ...

At what point in time does the LoadingFrameComplete event in Awesomium typically happen?

According to the documentation from Awesomium, the event WebView.LoadingFrameComplete is triggered when a frame finishes loading. This description seems somewhat ambiguous. Does this event coincide with the JavaScript load event of the window? Or perhap ...

What could be causing the issue with the focus not being activated when clicking on the input field in Vue?

I am facing an issue where I have to click twice in order to focus on a specific input field, and I'm having trouble setting the cursor at the end of the input. I attempted to use $refs, but it seems like there may be a deeper underlying problem. Any ...

What steps can I take to generate a JSON array with this unique format?

The data I received from my angular form is in the array format: [{"name":"Rose","number":"+919224512555"},{"name":"smith","number":"+91975555224"}]. The data represents a dynamic table with columns for name and number. I need to convert the array above i ...

Struggling with implementing basic i18n and dynamic routing functionality in Next.js

Despite reading the Next.js documentation, completing the tutorial, and reviewing numerous examples, I am still struggling to accomplish a straightforward task with Next.js. In my application, I have two locales: en and de. The route structure is as foll ...

ReactJS encountering issue with sending POST request, blocked by CORS preflight options

Every time I try to send a POST request to the server, it always responds with an unexpected OPTIONS. My login application built with React and Apollo is experiencing issues. Upon form submission, a mutation should be sent to the server via a POST request ...

Is it time to ditch Internet Explorer for EDGE?

Have you ever noticed that when attempting to access the stackoverflow website on Internet Explorer, the tab mysteriously closes and Microsoft Edge opens with stackoverflow loaded? What is the secret behind this strange phenomenon on stackoverflow's ...