When the image transitions, the second dot does not activate

Recently, I developed a small automatic image slider that transitions to a new image every 4 seconds by utilizing the following code:

setInterval(displayImage, 4000);.

The image transitions are functioning correctly; however, there seems to be an issue with the navigation dots. When the first image is displayed, the corresponding dot remains active while the second dot is disabled, which is perfect. But when the image changes to the second one, both dots remain disabled instead of having the first dot disabled and the second dot active.

I have shared my code below for reference:

function displayImage() {
    let nodeList = document.querySelectorAll(".sliderimg");
    let pointer = document.querySelectorAll("ol.controlnav li a")
    for (let i = 0; i < nodeList.length; i++) {
        for (let j = 0; j < pointer.length; j++) {
            if (nodeList[i].classList.contains("active")) {
                nodeList[i].classList.remove("active");
                pointer[j].classList.remove("controlactive");
            }
            nodeList[count].classList.add("active");
            pointer[count].classList.add("controlactive")
        }
    }
    count += 1;
    if (count == nodeList.length) {
        count = 0;
    }
}
.containimg .sliderimg {
    display: none;
}

.containimg .sliderimg.active.imageright {
    display: flex;
    animation: opac 0.8s;
}

.containimg .sliderimg.active.imageright2 {
    display: flex;
    animation: opac 0.8s;
}

.containimg .sliderimg.active.imageright3 {
    display: block;
    position: relative;
}

.imageright {
    background-image: url('https://themewagon.github.io/jackson/images/img_bg_2.jpg');
    background-size: cover;
    background-position: center;
    height: 100vh;
    display: flex;
    align-items: center;
    position: relative;
}

.imageright2 {
    background-image: url('https://themewagon.github.io/jackson/images/img_bg_1.jpg');
    background-size: cover;
    background-position: center;
    height: 100vh;
    display: none;
    align-items: center;
    position: relative;
}

.controlnav {
    position: absolute;
    text-align: center;
    bottom: 20px;
    z-index: 1000;
    left: 20px;
    width: auto;
    margin: 0;
    padding: 0;
    list-style: none;
}

.controlnav li {
    margin: 8px;
}


.controlpaging li a {
    width: 12px;
    height: 12px;
    display: block;
    cursor: pointer;
    text-indent: -9999px;
    box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.3);
    border-radius: 20px;
}

.controlpaging li a.controlactive {
    background: rgba(147, 147, 147, 0.795);
}
<div class="containimg">
    <div class="imageright sliderimg active">
        <div class="textimage">
            <h1>I am</h1>
            <h2>a Designer</h2>
            <h3>100% html5 bootstrap templates Made</h3>
            <h3>by <a href="#" class="underline">colorlib.com</a></h3>
            <button class="viewportfolio mt-4">view portfolio <i
                    class="fa-solid fa-suitcase"></i></button>
        </div>
        <ol class="controlnav controlpaging">
            <li><a href="#" class="controlactive">1</a></li>
            <li><a href="#">2</a></li>
        </ol>
    </div>

    <div class="imageright2 sliderimg">
        <div class="textimage">
            <h1>Hi!</h1>
            <h2>I'm Sarah</h2>
            <h3>100% html5 bootstrap templates Made</h3>
            <h3>by <a href="#" class="underline">colorlib.com</a></h3>
            <button class="viewportfolio mt-4">view portfolio <i
                    class="fa-solid fa-suitcase"></i></button>
        </div>
        <ol class="controlnav controlpaging">
            <li><a href="#">1</a></li>
            <li><a href="#" class="controlactive">2</a></li>
        </ol>
    </div>
</div>

Is there anyone who could provide insight on where I might be making a mistake in my code?

Answer №1

It seems like the issue at hand is that you are dealing with two separate sets of Nodes, but attempting to access them using the same index.

When you target .sliderimg, you are selecting two div elements, while ol.controlnav li a selects four a elements. Your current code muddles these distinct collections together.

To address this, be more precise in how you query your elements so that you have better control and can utilize the same index effectively:

... (Code block continued)

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

Strategies for avoiding the issue of multiple clicks on a like button while also displaying an accurate likes

My latest project involves creating a Like button component that features a button and a likes counter text field. The challenge I am facing is that each time I click on the button, it toggles between like and dislike states. However, if I rapidly press ...

Check to see whether the coordinates fall inside the specified bounding box

I am faced with the task of creating a function that can determine whether a given coordinate c lies within the boundaries of coordinates a and b. All variables in this scenario are of type: type Coordinate = { lat: number; lon: number; }; Initially ...

Utilizing Google Maps and navigation features directly in the mobile application

I am currently working on an app with the Ionic framework and I'm looking to incorporate Google Maps and navigation directly into the app without having to redirect users to the Google Maps application. My development stack includes TypeScript, HTML, ...

Steps for filtering a table with Vue and Element-UI

I am currently working with Vue and Element UI, and I have a requirement to display only elements in my table that have a status of 0. To retrieve my data, I am using Express and Axios. The code for this looks like: axios.get('http://127.0.0.1:8000/ ...

Having difficulty retrieving keys from a JSON object returned by a mongoose findOne() query

Recently, I've come across a strange issue. I used mongoose to search for a document in my mongoDB using model.findOne() with the following code: Model.findOne({ ID: ID }).then(existingDoc => { console.log(existingDoc ); res. ...

Issues with div misalignment during jQuery animation

I have successfully created an animation where a child div inside a parent div moves up by -60px when the mouse enters the parent div, and returns to its original position when the mouse leaves. However, I am facing an issue where if the mouse moves direct ...

Adding headers to json-server: Step-by-step guide

I am currently facing a situation where my frontend and backend are located on different servers, requiring me to make cross-domain requests. Specifically, I am using angular2 on localhost:4200, while also utilizing json-server on localhost:3000. The serv ...

Navigating using JavaScript dot notation is a powerful way to

Is there a way to implement JavaScript or jQuery code that will assign an active class to a dot when it is clicked, while also removing the active class from any other dots in the sequence? HTML: <div class="dotnav"> <div class="dot active" ...

In what way can the jQuery .each() function make use of the index as a variable?

Consider the jQuery .each() function, which comes with a useful feature. For example: $('.element').each(function(index){ console.log(index); }); Here, you can access the index of the currently selected element using the "index" variable. ...

Tips for utilizing the json_encode function with an array

I am trying to extract specific data from an object created using the json_encode function in PHP. while($locations=$req->fetch()){ $t = $locations->titre; $la = $locations->latitude; $lo = $locations->longitude; $typ = $lo ...

Flexbox wrap allows you to determine which child element will move to the next line

Is there a way to control which child element in flexbox drops to the next line, instead of just the last one every time? For example: Full-size, no breaking: https://i.sstatic.net/proUB.png Default behavior, breaking the last element: https://i.sstatic.n ...

Efficient ways to temporarily store form data in React JS

When filling out a registration form and clicking on the terms and conditions link, the page redirects to that content. Upon returning to the registration page, all fields are empty and need to be filled in again from scratch. I am looking for a way to ha ...

Progressive Web App with Vue.js and WordPress Rest API integration

When creating an ecommerce website using Wordpress, I utilized Python for scraping data from other websites to compare prices and bulk upload products through a CSV file. My next goal is to learn Vue and transform the website into a PWA as it will be esse ...

Converting form data into an object using JavaScript (Encountering an error: Cannot access property 'name' of undefined)

I recently started learning about MongoDB and I am following this tutorial here. The tutorial shows how to create a submit form that adds a person's name, age, and nationality to the database. However, I encountered the following error: TypeError: Can ...

What is the best way to utilize findOneAndUpdate() in Mongoose while maintaining a consistent value?

I have a programming challenge where I need to increment one value if condition x is true, and another value if condition y is true. The issue I'm facing is that my current system also resets the value that is not being updated back to 0, which is not ...

The tooltips on a resized jQuery Flot chart are displaying in the incorrect location

Currently, I am utilizing the flot library for my chart. I am facing an issue with getting the correct tooltips to display when I scale the chart using this CSS rule: transform: scale(0.7); The flot source relies on the function findNearbyItem to locate ...

Eliminate styling that is specific to certain browsers

Recent browser updates have introduced new styling and functionality to input[type=number], including up and down buttons. In Chrome, I was able to eliminate this added styling by accessing the shadow DOM and identifying the specific element. However, de ...

What are the steps to incorporate swipe functionality into my component?

I've created a carousel using React-slideshow-image, but the issue is that it doesn't support swiping on mobile devices. I would like to implement swipe functionality myself, but I'm not sure how to go about it. Can anyone provide guidance ...

What is the best way to use inline-block for older versions of Internet Explorer, specifically under

Looking to display rating stars in a row using repeated images with the inline-block property: <span class="stars" style="width:20px; margin-top:15px;"> <span style="width:40px;"></span> </span> span.stars, span.stars span { ...

Combine and blur multiple background images using CSS blending techniques

I am currently working on a website, which is just a demo built in ReactJS: The issue I'm facing is related to the background. The idea behind the app is simple - it consists of 4 parts, with the top part displaying weather information without a bac ...