Interactive JavaScript component that smoothly slides in the direction of the arrow that was pressed

Story Behind the Slider: I decided to create a slider for my website, so I created an additional "subpage" to simplify the process. It's currently just a prototype with basic CSS styling - when the user clicks the arrow, a slide moves in the corresponding direction (e.g., pressing the right arrow makes the slide move to the right). Although the JavaScript part seems straightforward (I even wrote code for it), I encountered a problem.

Issue at Hand: To make it work, I needed two extra classes called .slideLeft and

.slideRight</code) - one setting <code>trasnslateX
to -100% and the other to 100% (I wanted to keep them as separate classes to isolate the issue).
I moved some code from the base .slide class to .slideLeft, added it to the HTML as instructed, but unfortunately, it didn't work as expected.

Here lies the question - why does it function perfectly in the .slide class but causes glitches in the whole slider when separated into another class?

PROCEED WITH CAUTION :

I have included the functional code. The commented-out parts of the CSS can break the slider.
Additionally, the slider may appear choppy due to me reducing the translateX value to -50% (to ensure visibility of slides after uncommenting).

CODE SAMPLE:

CODEPEN


<!DOCTYPE html>
<html lang="en">
<head>
    <title>slider</title>
    <script src="script.js"></script>
    <link rel="stylesheet" href="style.css">
</head>
<body>

    <div class="container">
        <div class="center">
            <div id="slider">
                <span class="sButton" onclick="NextPrevButton(-1)"> &#10094;</span>
                <div class="slide slideLeft" id="s1" > PERFECT </div> <!-- two classes here -->
                <div class="slide slideLeft" id="s2" > FLAWLESS </div> <!-- two classes here -->
                <div class="slide slideLeft" id="s3" > GORGEOUS </div> <!-- two classes here -->
                <div class="slide slideLeft" id="s4" > SLIDER </div> <!-- two classes here -->
                <span class="sButton" onclick="NextPrevButton(1)"> &#10095;</span>
            </div>
            <div id="buttons">
                <span class="sDot" onclick="chooseSlide(0)"></span>
                <span class="sDot" onclick="chooseSlide(1)"></span>
                <span class="sDot" onclick="chooseSlide(2)"></span>
                <span class="sDot" onclick="chooseSlide(3)"></span>
            </div>
        </div>
    </div>
</body>
</html>


.slide {
    opacity: 1;
    visibility: hidden;
    display: none;
    /* Uncommenting .slideLeft (activated in HTML) messes up the slider, 
       despite being identical to that class */
    transition: all 0.7s cubic-bezier(0.92, 0.05, 0.74, 0.76);
    transform: translateX(-50%); /* Translating only 50% to show the error, intended value is -100% */
}
    .active{
        opacity: 1;
        z-index: -10;
        visibility: visible;
        transform: translateX(0);
    }
    /* .slideLeft{
        transition: all 0.7s cubic-bezier(0.92, 0.05, 0.74, 0.76);
        transform: translateX(-50%);
    }
    .slideRight{
        transition: all 0.7s cubic-bezier(0.92, 0.05, 0.74, 0.76);
        transform: translateX(50%);
    } */

Answer №1

To fix the issue, make sure to place your css classes .slideLeft and .slideRight before .active.

The problem arises when you override transform: translateX(0) from the .active selector.


 .slideLeft{
        transition: all 0.7s cubic-bezier(0.92, 0.05, 0.74, 0.76);
        transform: translateX(-50%);
 }
 .slideRight{
        transition: all 0.7s cubic-bezier(0.92, 0.05, 0.74, 0.76);
        transform: translateX(50%);
 } 
 .active{
        opacity: 1;
        z-index: -10;
        visibility: visible;
        transform: translateX(0);
 }
    

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 are some ways to disable text selection when working with VueJS?

Let's say I have a component that displays some text. When I click on the text at the beginning and then shift-click on another part, the text between those two points gets selected. Is there a way to prevent this behavior using only VueJS? ...

Hidden Div on Google Maps no longer concealing

Since early December, the Google map on my site has been working perfectly. However, the other night, I noticed that the map now defaults to open when entering the site and cannot be closed. Strangely, all my Google maps are behaving this way even though n ...

Unable to adjust the canvas size or scale using Fabric.js

Is there a way to display a large canvas as a smaller one for better user interaction without compromising the size of the uploaded canvas later on? I'm currently working with Fabric.js and exploring the Canvas CSS trick that involves setting the widt ...

When attempting to upload multiple images to my backend, I am consistently receiving an empty array as a result

I have developed an API on the backend that is designed to store images in the database. I have implemented a function for uploading these images, but unfortunately, I am encountering an issue where my database receives an empty array. Interestingly, when ...

An unexpected error occurs when attempting to invoke the arrow function of a child class within an abstract parent class in Typescript

Here is a snippet of code that I'm working on. In my child class, I need to use an arrow function called hello(). When I try calling the.greeting() in the parent class constructor, I encounter an error: index.ts:29 Uncaught TypeError: this.hello is ...

Add necessary information to current document

I have encountered a unique challenge with AJAX. Let me explain the situation at hand: There is a form with submit=javascript:function() This function triggers an AJAX call with certain values, and upon success, I aim to add some content with a &apo ...

Is the modal login overlay persisting even after successful authentication?

<?php require_once('authenticate.php');?> <body> <div id="login-modal" class="modal show"tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;"> <div class="modal-dialog"> ...

Efficiently incorporating multiple properties into one in Angular

Within my Angular service, I have defined variables in the following manner: export class MyService { someVariableA = 1; someParams = { someVariableB, otherVariable: this.someVariableA }; } In a component, I update 'someVariableA&a ...

JavaScript's "for of" loop is not supported in IE 11, causing it to fail

Here is a piece of code I'm using to select and remove a d3.js node: if (d.children) { for (var child of d.children) { if (child == node) { d.children = _.without(d.children, child); update(root); ...

Exploring the functionality differences between mouse wheel tilt and scroll in JavaScript with scrollTop

Did you know that some computer mice come equipped with a scroll wheel that can tilt both left and right? This feature allows users to navigate through Google's piano roll app, which can be accessed here (source code available here). I am working to ...

Give a style attribute to every other child element within a class. Each class should function independently, including their final child which contains an if statement

UPDATE: I kindly request a review of the answers provided. Utilizing JavaScript for this task may not be the most efficient method. I will keep this question as it is, as it could potentially benefit someone else. The aim is for every ".ulInCollapse li" t ...

Retrieve data from the MySQL database based on the search input field and dynamically update the options in the

I'm facing a programming challenge that I perceive to be at an advanced level for me. Currently, I have a custom search field in my registration form. However, I am looking to transform it into a dropdown menu that pulls user values from MySQL databas ...

PHP child categories causing jQuery menu loading issue

Our custom jQuery menu has been functioning perfectly on our OpenCart store. However, we are facing an issue where the 2nd level child categories are not being displayed. It seems that there is an error in the PHP code for both the modified and original me ...

Displaying Database Content on Screen Instead of index.html in Node/Express

I'm currently working with Node.js, however, when I try to access my localhost all I see is the database data in JSON format instead of my index.html page. This issue doesn't happen when using localhost, so I'm not sure why it's not dis ...

Center your wordpress site title next to your logo

My logo and website title are not aligned correctly on my site. The current setup looks like this: https://i.sstatic.net/3dUiN.png After examining the HTML generated by WordPress, I found the following snippet: <div id="logo" class="clearfix"> ...

Pattern to prevent consecutive hyphens and identical digits next to one another in a series

Here is a regular expression that can validate all numbers not being the same even after a hyphen: ^(\d)(?!\1+$)\d{3}-\d{1}$ For example, in the pattern: 0000-0 would not be allowed (all digits are the same) 0000-1 would be allowed 111 ...

The Expressjs router prioritizes resolving before retrieving data from Firestore

UPDATE: Upon further investigation, I discovered that by adding "async" to the function signature, the output now displays in the intended order. However, I am still encountering an error regarding setting headers after they have already been sent, even th ...

Unveil as you scroll - ScrollMagic

I am working on a skill chart that dynamically fills when the document loads. I am exploring the possibility of using ScrollMagic to animate the chart filling as the user scrolls down. After trying various combinations of classes and pseudo-classes in the ...

tips for decreasing box size diagonally while scrolling down

$(function(){ var navIsBig = true; var $nav = $('#header_nav'); $(document).scroll( function() { var value = $(this).scrollTop(); if ( value > 50 && navIsBig ){ $nav.animate({height:45},"medium"); $('.box ...

"Enhance Your Smart Contract Interaction with Algrand's Method Calling

Currently, I am working on integrating an Algorand smart contract with a Next.js application. To sign transactions, I am utilizing Pera wallet. However, I have encountered an issue when attempting to call methods from the contract on the front end using th ...