Issues with the sliding mechanism

When the condition is that the slider should be fixed and switched by pressing a button, each component works fine individually but breaks when used together. The issue arises when the first slide appears and the buttons work, but once the animation transitions to the second slide, the button functionality stops. Pressing a button should switch back to the corresponding slide, however, it only works if the other button is pressed first.

  
var toggle1 = document.querySelector(".slide-1");
var toggle2 = document.querySelector(".slide-2");
var slide1 = document.querySelector(".first");
var slide2 = document.querySelector(".second");

toggle1.addEventListener("click", function(event) {
    slide2.classList.add("display-slide-2");
    slide1.classList.add("display-slide-1");
});

toggle2.addEventListener("click", function(event) {
    slide2.classList.add("display-slide-1");
    slide1.classList.add("display-slide-2");
});

.promo .slider-promo {
    position: relative;
    display: inline-block;
    float: left;
    width: 620px;
    height: 265px;
    background-color: #f2f6f8;
    margin-right: 20px;
    color: #fff;
    font-family: "CuprumRegular";
    font-size: 18px;
    line-height: 24px;
    overflow: hidden;
}

.slider-promo .slide-item {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url(../img/promo-slide-1.jpg) no-repeat top left;
}

.slider-promo .second {
    /* display: none; */
    background: url(../img/promo-slide-2.jpg) no-repeat top left;
}

.slider-promo .toggle {
    position: absolute;
    z-index: 100;
    bottom: 35px;
    left: 50%;
    margin-left: -40px;
    width: 80px;
    height: 11px;
}
...
<div class="slider-promo">
            <div class="slide-item first display-slide-1">
                <div class="name">
                    <h3>First slide</h3>
                    <span>Lorem lorem lorem</span>
                </div>
                <a href="" class="btn">More info</a>
            </div>

            <div class="slide-item second display-slide-2">
                <div class="name">
                    <h3>Second slide</h3>
                    <span>Lorem lorem lorem</span>
                </div>
                <a href="" class="btn">More info</a>
            </div>

            <div class="toggle">
                <div class="label slide-1"></div>
                <div class="label slide-2"></div>
            </div>

Answer №1

When toggling, ensure that the animation of both slides stops by removing a particular class from the node element. Afterwards, display the appropriate slide with a delay that suits your requirements before adding the animation back to the node element.

In this scenario, I have outlined the logic for displaying slide 2 when clicking on toggle-2.

toggle2.addEventListener("click", function(event) {
           slide2.classList.remove("display-slide-2");
           slide1.classList.remove("display-slide-1");

           slide1.style.left = "-620px";
           slide2.style.left = "0px";
           slide2.style.zIndex = "2";
           slide2.style.display = "block";

            setTimeout(function(){
               slide2.classList.add("display-slide-2");
               slide1.classList.add("display-slide-1");
           },1000); // adjust delay as needed

        });

I trust this aligns with your expectations.

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

What causes an "Undefined index" error in jQuery when making AJAX requests?

Whenever I make an AJAX request in my main.js file, I encounter the following error message: Undefined index: id in sqlinfo.php on line 13 I am puzzled because I believe that I am populating the request object correctly. What's even more perplexing i ...

Webpack fails to handle CSS background images

I'm having trouble with my Webpack configuration as it's not processing CSS images set in the background property: background: url('./hero.jpg') no-repeat right; This is resulting in an error message that reads: ERROR in ./src/app/comp ...

UTC Time Picker using Material UI

As I was exploring the material ui documentation and experimenting with it myself, I couldn't find any options to select the time in UTC. The time picker always seems to default to my local timezone, even when I provide a UTC Date object. Does anyone ...

What is the best way to extract value from an input text that is being repeated using ng-repeat within a Tr

Having a table with repeated rows in angular, you can also dynamically add new rows by clicking a button. If there are already 3 repeated rows and 2 extra rows are added with data entered, how can we retrieve all the data from the table in the controller ...

Single input results in array output

I have a pair of items in this array list. $.each(data.recalls,function(i) { var recall = data.recalls[i].nnaId; var description = data.recalls[i].remedyDescription; console.log(recall); console.log(description); $('textarea[name= ...

What is the best way to convert the response from an XMLHttpRequest to a JavaScript array and display it

Looking to transform the data from xmlhttp.responseText into a JavaScript array I have this {"name":"Eswara Manikanta Varma","email":"contact@email.com","mobile":"9966578911"} retrieved from xmlhttp.responseText, and now I need to convert it into a JavaSc ...

Looking to eliminate the extra space around elements inside the Body section

I'm diving back into HTML5 after years of coding in the old ways. Starting from scratch, I began working on a website design and code. However, I've hit a roadblock with the padding around my images that I can't seem to remove. I want all th ...

Latest News: The store is now received in the mutation, instead of the state

An update has been made to this post, please refer to the first answer After thorough research, I couldn't find a solution to my issue despite checking several threads. This is my first time using the Quasar framework and it seems like I might have o ...

Arrange the array of items according to the order specified in a separate array

Related Questions: JavaScript - Sorting an array based on another array of integers Sort an array in JavaScript based on another array If I come across an array structured like this: ['one', 'four', 'two'] And then ...

Separating unique values in an array of objects and storing the rest of the values in a separate

I have an array of JavaScript objects that I need to merge into a single JavaScript object based on their same property value. The remaining values should be grouped into another array named info. Original array: const array = [ { price: 27, ...

The contrast between FormData and jQuery's serialize() method: Exploring the distinctions

Recently I came across a situation where I needed to submit a form using AJAX. While researching the most efficient method, I discovered two popular approaches - some developers were utilizing jQuery#serialize() while others were opting for FormData. Here ...

What is the best way for me to utilize top-level await in my

I'm in the process of developing a react app and I need to make a function call like this: if (typeof window !== 'undefined') { var token = (await getItem('secure_token')); } if (typeof window !== 'undefined') { var user_ ...

Differentiating between CSS layouts for mobile and desktop devices

Is there a way to create this specific layout using CSS? It's important to note that the order of elements should change on mobile compared to desktop! https://i.sstatic.net/GRIxV.png Currently I'm working with bootstrap 4. The code below "wor ...

Exploring the capabilities of qTip2 integrated with jQuery Vector Maps (jqvmap)

Is there a way to disable the default tooltip in jqvmap and replace it with qTip2? Check out this example. Here's the jQuery code: jQuery('#vmap').vectorMap({ map: 'world_en', backgroundColor: null, color: '#ffff ...

Is it possible to use jQuery to search an unordered list by simply pressing a key on the keyboard?

I have a list of countries displayed as an unordered list inside a table column. I would like to implement keyboard search functionality for quicker navigation within the list. <ul class="country-search"> <li id="1">Country 1</li> ...

Guide to updating the value of an input field with Selenium in Python

I have an input element that I need to clear its current value and input a new one. The HTML structure is as follows: <input class="input-mini" type="text" name="daterangepicker_start" value=""> To locate this element, I used the following code: ...

Revamp your AngularJS controller for a fresh new look

How can a controller be refreshed in Angular? Take into account the scenario below: <div ng-controller="MsgCtrl" ng-repeat="m in messages">{{m}}<br></div> <script> var ws = new WebSocket(something, something); function MsgCtrl($sco ...

Building a Wordpress website with AJAX functionality using only raw Javascript and no reliance on Jquery

So, I have a custom script named related-posts.php, and I want to insert it into posts only when the user scrolls. In addition, I have an enqueued script file in WordPress that loads in the footer, where I have written AJAX code like this: var xmlhttp = ...

React Native Issue: Missing propType for native property RCTView.maxHeight

Upon upgrading to RN 0.30, I encountered the error message below even when trying to build the most basic app: react-native init AwesomeProject react-native run-ios What's peculiar is that the warnings include components BlurView, VibrancyView, an ...

Getting the selection within a particular div – how can it be done?

Currently, I am working on a contenteditable div feature (identified by id 'editor1') which allows users to input text. Additionally, there is a function that enables users to highlight and change the color of selected text. In my JavaScript code ...