JavaScript is employed to fade words in and out of an array list

I am trying to achieve a fade in/fade out effect for each word, displayed one after the other. Currently, the script either shows only the last word in the array or displays all words together. I am utilizing animate.css and JQuery for this transition of words. Any suggestions would be appreciated. Thank you.

   <script>
    var classes = [ '<h1 class="animated infinite rotateOutUpLeft">Software        </h1>',
            '<h1 class="animated infinite rotateOutUpLeft">project </h1>',
            '<h1 class="animated infinite rotateOutUpLeft">Engineering</h1>',  
            '<h1 class="animated infinite rotateOutUpLeft">Science</h1>'
        ];

         var display;
           for (i = 0; i < classes.length; i++) {
                document.write(i);
                $("#tst").empty();
                 $("#tst").append(classes[i]);
                // setTimeout(function(){alert('join');}, 10000);
                // $("#tst").append(classes[i]);
                // setTimeout(function(){$("#tst").append(classes[i]);}, 6000);
            }
</script>

Answer №1

Are you searching for a snippet of code that will automatically change the word every six seconds with CSS handling the fading effect? If so, you can try this solution:

var index = 0;
$("#text-element").html(wordArray[index]);
setInterval(function() {
    index = (index + 1) % wordArray.length;
    $("#text-element").html(wordArray[index]);
}, 6000);

jsfiddle

Alternatively, you can use this approach:

(function displayWord(index) {
    $("#text-element").html(wordArray[index]);
    setTimeout(function() {
        displayWord((index + 1) % wordArray.length);
    }, 6000);
})(0);

jsfiddle

If you prefer the code to include fading effects:

var wordArray = ['Software', 'project', 'Engineering', 'Science'];
(function displayWord(index) {
    $('#text-element h1').text(wordArray[index]).fadeIn(1000).delay(600).fadeOut(1000, function() {
        displayWord((index + 1) % wordArray.length);
    });
})(0);

jsfiddle

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

Error type thrown by BrowserSync during initialization

Encountering a TypeError with BrowserSync during initialization: [12:02:53] TypeError: undefined is not a function at Object.init (/Users/conti/dev/foodjournal-web/node_modules/browser-sync/lib/public/init.js:25:25) at Gulp.<anonymous> (/Use ...

Customizing object joining rules in JavaScript arrays

My array consists of different colored items with their respective types and amounts [ { color: 'blue', type: '+', amount: '1' }, { color: 'blue', type: '-', amount: '1' }, { color: 'blu ...

Disappear the div when it has no content and apply a class to a different div

Our product has both a regular price and a sale price. Both prices are styled with a width of 50% and displayed as inline-block elements. I am looking to create a script that will change the style (adding a class for 100% width and changing the font styl ...

The Angular datepicker is failing to trigger the ng-change event

I've run into a snag with the datepicker and ng-change functionality. Oddly enough, the ng-change event isn't triggering when I manually select a date by clicking on it, but it works fine when I input a date manually. Take a look at my code snip ...

Guide on converting JSON to CSV in React by utilizing the map function

When I convert JSON data to CSV by clicking a button, it currently stores the data in the CSV file separated by commas. However, I want each piece of data to be on its own line. How can I achieve this? For example: Minor,Minor What I Want: Each item on a ...

Ensuring the Alignment of Element Borders in HTML CSS

Among my collection of buttons, each displayed in line with <h3> tags, I notice that despite all the <h3> tags being the same length, the edges of the buttons are not perfectly aligned. I am aiming to achieve a clean and seamless look by align ...

The behavior of starting and stopping the setInterval function

I am currently working on a stopwatch project that features a start/stop button, but I'm encountering issues with the behavior of setInterval. When I declare setInterval at the React functional component level, it starts running as soon as it's ...

Check to see if the property of the object does not exist within the array and update as

My goal is to add the variable content into the database content using the $push method, but only if the content.hash doesn't already exist in the database. I want to avoid duplicating information unnecessarily. return shops.updateAsync({ "user": u ...

The npm install command can expose various vulnerabilities

Just starting to learn JAVASCRIPT, I ran the 'npm audit command' after encountering vulnerabilities in the npm install command. All I did was add functionality to my server/client project by incorporating HTTP requests (DELETE, POST) in Axios an ...

Unable to alter the default error message within the jquery-validation plugin

I recently came across a post discussing how to change default error messages in jQuery validation. You can check it out here. After reading the post, I decided to modify my js code by adding the following lines to jquer.validate.min.js: jQuery.extend(jQ ...

How to change the value of feColorMatrix in VueJS

In my Vue Project, I have implemented a component named demo.vue. This particular component features a complex svg within its <template>. When an element is clicked, the Javascript should alter the color of one of the svg's drop shadows. The s ...

Is it possible to craft a miniature duplicate of a div element?

My challenge is to create a smaller version of a dynamically generated div that contains text, photos, and more. The original div has a height:width ratio of around 10:1. I aim to replicate this div on the same page but at 1/8 of the width. UPDATE: All t ...

Using a $watch on a directive that has an isolated scope to monitor changes in a nested object property retrieved from

I have developed a custom directive with an isolated scope and element. I am utilizing the values passed into the directive to construct d3/dc charts. The data goes through crossfilter on the $scope so that the directive attributes can access it. Despite s ...

The floated div is disrupting the flow due to its margin

Two divs are causing some alignment issues: one floated left and the other floated right. The margin on the right div is pushing it down, causing the left div to appear lower than desired. I want both divs to be aligned at the top of the page. HTML: < ...

Encountering an issue while following the Amadeus JavaScript beginner's guide

I have recently started working with Amadeus and encountered a roadblock at the initial stage. I am using the test URL (test.api.amadeus.com). Upon implementing the example code with my API key and API secret, I am receiving the following error: ClientErro ...

Error: The function in Knockout js is not defined as undefined

I am attempting to insert specific data into an array, as shown below: Below is my code: Create.html Field Name: Display Name: ...

How to Mark Completed Tasks and Display Them on Fullcalendar

Is it possible to mark off completed tasks using the Fullcalendar plugin, maybe with the <strike> tag? I am retrieving tasks from my database and passing the results to json_encode(). ...

Utilizing cylon.js with Nest Thermostat

Experiencing errors while trying to retrieve thermostat ambient Temperature with cylon.js I have already replaced ACCESS_TOKEN with my unique access token and device id Sample Code: var Cylon = require('cylon'); Cylon.robot({ connections: { ...

Design the paragraph to resemble text from Microsoft Word

I need to style a paragraph to resemble the formatting in MS Word (see link below for image reference). This is what I have accomplished so far: ol { text-align: justify; } ol.first { list-style: upper-roman; list-style-position: inside; ...

When the mouse is moved, display a rectangle on the canvas

I am having an issue with drawing a rectangle on canvas. The code below works fine, except that the path of the rectangle is not visible while the mouse is moving. It only appears when I release the mouse button. Any assistance would be greatly appreciate ...