Creating a text slider in Javascript with dual quote sets is a fun and interactive

I am looking to create a dynamic quotes text slider with two sets of data.

For reference, check out the fiddle link here: https://jsfiddle.net/628r3t1h/

(function() {

    var quotes = $(".quotes");

    var quoteIndex = -1;
    function showNextQuote() {
        ++quoteIndex;

        quotes.eq(quoteIndex % quotes.length)
        .fadeIn(1500)
        .delay(1000)
        .fadeOut(1000, showNextQuote);
        
    }

    showNextQuote();

})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- set 1 -->
<h1 style="" class="sec1-head">Set 1<br/>
    <span style="" class="quotes sec1-head-quotes">Text 1.1</span>
    <span style="display: none;"  class="quotes sec1-head-quotes">Text 1.2</span>
    <span style="display: none;"  class="quotes sec1-head-quotes">Text 1.3</span>
</h1>
<!-- set 2 -->
<h1 style="" class="sec1-head">Set 2<br/>
    <span style="" class="quotes sec1-head-quotes">Text 2.1</span>
    <span style="display: none;"  class="quotes sec1-head-quotes">Text 2.2</span>
    <span style="display: none;"  class="quotes sec1-head-quotes">Text 2.3</span>
</h1>

The goal is to have "Set 1" displayed first followed by "Set 2", and then cycle through them continuously.

Answer №1

Is this what you're looking for?

const sets = $(".set");

let set = sets[0], quote = 0;

sets.hide();

$(sets[0]).fadeIn(1500);

function showQuote() {
    if($(set).children().eq(quote).is(':last-child')) {
        if($(set).hasClass("last")) {
            set = $(".set").first();
        } else {
            set = $(set).next();
        }
        sets.hide();
        $(set).fadeIn(1500);
        quote = 1;
    } else {
        ++quote
    }
    
    $(set).children().eq(quote)
        .fadeIn(1500)
        .delay(1000)
        .fadeOut(1000, showQuote);
}

showQuote();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- set 1 -->
<h1 style="" class="sec1-head set">Set 1<br/>
    <span style="" class="quotes sec1-head-quotes">Text 1.1</span>
    <span style="display: none;"  class="quotes sec1-head-quotes">Text 1.2</span>
    <span style="display: none;"  class="quotes sec1-head-quotes">Text 1.3</span>
</h1>
<!-- set 2 -->
<h1 style="" class="sec1-head set last">Set 2<br/>
    <span style="" class="quotes sec1-head-quotes">Text 2.1</span>
    <span style="display: none;"  class="quotes sec1-head-quotes">Text 2.2</span>
    <span style="display: none;"  class="quotes sec1-head-quotes">Text 2.3</span>
</h1>

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

Verifying the success of an AJAX post using jQuery

What is the best way to set up the success and failure functions for an ajax $.post request? ...

Exploring the use of the Next.js page router for implementing internationalization (i18

I need assistance implementing Dutch and English language translations for my entire website. Can anyone provide guidance? I have tried using i18n localizely, but it only works for individual pages. I am looking to implement translations on a larger scale ...

Set the height of the div to be the full height of the page when designing for responsiveness

For my responsive design, I am looking to have a footer that sticks to the bottom of the page while allowing the content to fill up the remaining space. You can view an example here: http://jsfiddle.net/rzjfhggu/1/ In this example, the header has a fixed ...

Learn the steps to assign a Base64 URL to an image source

I am currently facing an issue with an image that is being used with angular-cli: <img src="" style="width: 120px; padding-top: 10px" alt="" id="dishPhoto"> The image has a Base64 url named imgUrl. My intention is to set the image source using the ...

Is there a way to send multiple parameters in an array to a route?

I am working on deleting multiple rows from a MySQL database using checkboxes in my ejs view with node.js and React app. I have successfully deleted a single row, but I am struggling to figure out how to pass an array of parameters for deleting multiple ro ...

How to send a global variable to an event callback function in JavaScript?

I have encountered an issue in the following code where I am attempting to pass mydata to the callback function. This is just a small part of a larger problem that I am facing and I suspect it may be related to scope. Can anyone help me identify what is wr ...

Video background function that mimics CSS's background size cover for a specific element, instead of the entire webpage

In the process of developing a new website, I am looking to incorporate a large splash background as a "hero" image. Similar to the website found at , I am hoping to use a video as the background image showing a woman unloading a car. One key requirement ...

When you hover over the image, the text will disappear

When I hover over the image of LTC, a thumbnail appears with text and a button. However, I do not want the text "LTC" to be shown when hovering over the image. <div class="col-md-4"> <div class="view view-eighth"> <div class=" ...

encountering a problem with retrieving the result of a DOM display

private scores = [] private highestScore: number private studentStanding private studentInformation: any[] = [ { "name": "rajiv", "marks": { "Maths": 18, "English": 21, "Science": 45 }, "rollNumber": "KV2017-5A2" }, { "n ...

Error: ytcfg and __ytRIL are undefined and cannot be referenced

For several months, I had smooth sailing with the YouTube IFrame Player API until recently when an unexpected exception popped up upon loading the player: Uncaught ReferenceError: ytcfg is not defined Upon inspecting the iframe, I noticed that yt.setConfig ...

Whenever I attempt to execute yarn build within next.js, an error always seems to occur

When attempting to compile my next.js project using the yarn build command, an error consistently occurs: Error: Export encountered errors on following paths: /settings at D:\web3\futnft\frontend\node_modules\next\ ...

two adjacent elements filling the entire height of the window at 100% dimensions

Is there a way to place two elements side by side, with one of them (the textarea) always being 100% height of the window? I've looked at similar questions here but can't figure out how to make it work. Can you help me with this? html: <div ...

Passing $index variable from a bootstrap modal window does not work in AngularJS

I'm running into a wall here. My issue involves using ng-repeat to populate a table with two buttons in each row - one for updating the row content and another for uploading files. The upload button triggers a bootstrap modal window where users can se ...

Tracking and monitoring the advancement of multiple ajax post requests

Currently, I am utilizing jQuery for multiple ajax POST requests. In order to effectively manage the success or failure of each request and track the overall progress of the entire batch, my goal is to update the UI with a progress bar and information on t ...

What is the best way to change a String into an Array within MongoDB?

I need help with converting the type of an object that has been changed. Is there a way to transform this: { "_id" : NumberLong(257), "address" : "street Street, house 50, appartment 508, floor 5" } into this: { "_id" : NumberLong(257), "userAddres ...

Incorporating smooth sliding animation to ng-repeat elements

Our team has developed a custom widget in ServiceNow that displays a row of icons and reveals or hides additional details in a div when icons are clicked. Below is the structure of our html and client controller: <div class="icons"> <ul class=" ...

Disable browser autocomplete for Material-UI TextField

I am currently using Material UI version 0.20.0 and I am facing an issue where I need to prevent the password from being saved for a user in a TextField. I tried adding props to the TextField with autocomplete='nope' since not all browsers suppor ...

Is there a way to restrict the amount of user input that is allowed?

Is it possible to restrict the input quantity when using the built-in arrow icon in the text field, but not when typing manually? https://i.sstatic.net/jjogP.png <TextField variant="outlined" label="Quantity" onChan ...

Retrieving text from an XPath element with Selenium WebDriver in JavaScript

I've been experimenting with Selenium and Node to extract text content from websites. For instance, I'm currently working with the URL: and using this XPath /html/body/section/div[1]/div[3]/div[1]/div[3]/form/button When I check in an Xpath se ...

Transform an Angular application built using the Meteor framework to an Express JS-Angular application

Is there an automated or minimalist way to transform an application with a meteor backend and angular frontend to use a backend in Express js and keep the frontend using vue js instead? I have not been able to find any resources or documentation that prov ...