Create an endless loop of two distinct animations with animate.css

Lately, I've encountered an issue with animate.css while working on my latest project.

What I'm aiming to achieve is to update a paragraph of text on my website every five to ten seconds using jQuery. However, I don't want the text to simply change abruptly. Instead, I would like the previous text to smoothly disappear with the fadeOut animation from animate.css, and the new text to appear seamlessly with the fadeIn animation.

Currently, I have this code snippet (just an example):

setInterval(function() {
  $("#myp").addClass('fadeOut');
  $("#myp").text(sometext);
  $("#myp").removeClass('fadeOut');
  $("#myp").addClass('fadeIn');
  $("#myp").removeClass('fadeIn');
}, 5000);

Each cycle uses a different value for sometext for simplicity.

Initially, I faced some issues with this code as the animation was not smooth but rather flickery. I attempted to improve it by introducing delays in the process using setTimeout between adding and removing classes. My thought was that removing the class before the CSS animation completes could be causing the problem, but the flickering issue still persisted.

Answer №1

If you want to have more control over the timing of each step in an animation, you can nest setTimeOut methods within the setInterval function.

Additionally, when using animate.css and the animation property, don't forget to specify the animation-duration and animation-fill-mode in your CSS.

The animation-duration property determines how long the animation cycle should last, while setting animation-fill-mode to "forwards" will ensure that the element retains its final state after the animation completes.

var text = "some other text";

setInterval(function() {
  
    var myText = $("#myText");
    myText.addClass('fadeOut');
  
    setTimeout(function() {
      myText.text(text);
      myText.removeClass('fadeOut');
      myText.addClass('fadeIn');
      
      setTimeout(function() {
         myText.removeClass('fadeIn');
      }, 1000);
      
    }, 1000);
  
}, 5000);
#myText {
  animation-duration: 1s;
  animation-fill-mode: forwards;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="myText">lorem ipsum dolor sit amet</div>

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

Working with DOT in URLs using AngularJS UI-Router

I am currently facing an issue while trying to authenticate users using the Google API. The problem arises when the return data in the parameters contain a DOT within the token, causing the server to break as it requests a page that does not exist. However ...

Combining text and images in XSLT using CSS: A comprehensive guide

Greetings! I am facing a situation where I have an XML file containing image tags like this: <pb facs="ciao.jpg">. Additionally, I have an XSL file that includes a JavaScript snippet as shown below: <script type="text/javascript> function ...

Leverage the power of Angular's route resolve function

Can anyone assist me with a problem I'm facing? I am looking for a way to delay the display of a view until my data is loaded. After some research, I was thinking of using a resolve in the route, but I can't seem to figure out how to implement it ...

Steps for appending a string to a variable

Currently working on creating a price configurator for a new lighting system within homes using Angular 7. Instead of using TypeScript and sass, I'm coding it in plain JavaScript. Page 1: The user will choose between a new building or an existing one ...

Deleting a page reference from the page history stack in Angular 4

I am working on my angular web application and I am looking for a way to remove a specific page reference from the angular page history stack. For example, if I navigate from the login page to the dashboard page, I want to remove the login page reference f ...

The function defineCall does not exist, causing a sequelize error to occur

A challenge I've been facing is resolving an error that is popping up in my index.js file. I'm currently utilizing Node, Express, and sequelize in my project. /app/node_modules/sequelize/lib/sequelize.js:691 server_1 | this.importCache ...

The value remains unchanged when using Renderer2.setProperty()

I am attempting to update the value using the rendered.setproperty() method, where the value is updating the second time on a listen event These are the values that I am sending for the first time as blank in some widget <ols-giftcard-payment-widget ...

The error message "app.use() function requires middleware function" appears when the app

Currently, I am in the process of learning node.js with express template engine by following a Udemy course called "Learn Node.js by Building 10 Projects". During one of the lectures, when the professor ran npm start localhost:3000, it worked fine for him, ...

Bring back enhanced assortment using Mongoose

I am currently working with a stack that includes nodejs, express, mongoose, and angularjs. My task involves updating a collection called Lists which contains various properties, including an array of items. The issue I am facing is that when I push a new ...

Interacting with three.js using the "requestAnimationFrame" function when clicking

I would like the camera to rotate continuously when pressing the button + = And it seems that there is a trigger for 2 seconds (with a click) and then stops. let btn = document.querySelector('.btn--left'); btn.addEventListener('click',f ...

What is the best method for utilizing dynamically assigned keys within a JSON object?

I am currently working with Rhino and I need to find a way to utilize a variable in order to define the key of a map. Here's an example of what I'm trying to achieve: var name = "Ryan"; var relationshipType = "brother"; var relatedName = "David" ...

Combining audio tracks using NodeJS: A step-by-step guide

Looking to combine multiple audio files in nodejs. The audio files I have are: audio1.mp3, audio2.mp3, audio4.mp3. Here's how I want them mixed: at 0:00 seconds : add audio1.mp3 to the final mix at 0:30 seconds : add audio2.mp3 to the final mix at 00 ...

Did the menu get shifted downwards on the mobile version by using bootstrap?

Here is the desktop version of a navigation bar. https://i.sstatic.net/e595L.png This image shows the mobile version after clicking on the hamburger button. https://i.sstatic.net/ng1tK.png I would like the menu to open when I click on the hamburger, bu ...

Unable to access property 'map' of undefined - having trouble mapping data retrieved from Axios request

When working with React, I have encountered an issue while trying to fetch data from an API I created. The console correctly displays the response, which is a list of user names. However, the mapping process is not functioning as expected. Any insights or ...

Implementing a document update event using jQuery

On my WordPress site, I am using a responsive lightbox plugin. When an image is clicked, it opens a popup box with the ID fullResImage. Now, I would like to incorporate the Pinch Zoomer function to it. Do I need to bind a function for this, or is there a s ...

Convert HTML element to a JSON object

Here is an example of an HTML element: <div class='myparent'> <div> <div class="pdp-product-price"> <span> 650 rupees</span> <div class="origin-block"> <sp ...

I am noticing multiple quantities of the same item being added to my shopping

Recently, I encountered a problem with my online shop that seems to be related to the Javascript. The issue arises when I add an item to my shopping cart from this particular page: . Initially, when I click 'Add to Cart,' the item is correctly ad ...

Creating responsive lines with CSS on top of an image

I would like to add lines on an image using CSS3 and HTML5 Canvas. I came across a tutorial and demo that utilizes an html div: However, when attempting this effect on an image, the line appears outside of the image. How can I ensure that the line is dra ...

The JavaScript string in question is: "accepted === accepted && 50 > 100". I need to determine whether this string is valid or not by returning a boolean answer

I am developing a dynamic condition builder that generates a JavaScript string such as, tpc_1 === accepted && tpc_6 > 100 After the replace function, the new string becomes, accepted === accepted && 50 > 100 Now my challenge is to va ...

Cordova plugin for Ionic has not been defined

I am facing a challenge in my Ionic app. The issue arises when the cordova.js file containing a crucial plugin does not initialize until after the controllers.js script has been downloaded and executed. As a result, attempts to use the plugin in the contro ...