The slider is experiencing a transition error

I am having issues with the CSS slider that I created. It works fine on auto play, but when I try to manually navigate using the dots, the transition does not work smoothly. I believe it should take only 5 minutes to solve this issue for someone who is experienced in this area. Any help would be greatly appreciated!

If you have an alternative method to create a beautiful slider, please share your knowledge.

Below are the code snippets provided, please take a look and run them to see the error in the transition:

// Javascript code
var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}
// More JavaScript functions...

/* CSS Styles */
*{
    margin: 0;
    padding: 0;
}
.psr_slider{
    position: absolute;
    top: 0;
    left: 0;
    overflow: hidden;
}
 /* More CSS Styles... */


<!DOCTYPE html>
<html lang="en">
<head>
    <!-- HTML code goes here -->
</head>
<body>
   <!-- Body content here -->
</body>
</html>

Answer №1

After some investigation, it turns out I found the solution to my question on my own. I identified where I went wrong and made necessary adjustments. Instead of relying on the width property for transitions, I now utilize the transform property. This change required only minor modifications in both the CSS and JS code without affecting the HTML structure. Hopefully, this insight proves helpful to you as well.

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("psr_slides");
  var dots = document.getElementsByClassName("psr_slider_dot");
  if (n > slides.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = slides.length
  }
  for (i = 0; i < slides.length; i++) {
    var j = 100*i-(slideIndex-1)*100+"%";
    slides[i].style.transform = "translateX("+j+")";
  }
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  dots[slideIndex - 1].className += " active";
}

function autoInce(){
    for (let i = 1; i <= 10000; i++) {
        setTimeout(function(){ 
            plusSlides(1);
         }, i*5000);    
    }
}
autoInce();
*{
    margin: 0;
    padding: 0;
}
.psr_slider{
    position: absolute;
    top: 0;
    left: 0;
    overflow: hidden;
    width: 100vw;
    height: 40vw;
}
.psr_slide{
    display: flex;
    flex-direction: column-reverse;
}
.psr_slide_1,.psr_slide_2,.psr_slide_3,.psr_slide_4{
    position: absolute;
    top: 0;
    width: 100vw;
    height: 40vw;
    /* overflow: hidden; */
    transition: all 1s;
}
.psr_slide_1{
    background: red;
}
.psr_slide_2{
    background: pink;
}
.psr_slide_3{
    background: yellow;
}
.psr_slide_4{
    background: green;
}
.psr_slider_right{
    position: absolute;
    top: 50%;
    right: 1rem;
    transform: translateY(-50%);
}
.psr_slider_right svg{
    height: 64px;
}
.psr_slider_left{
    position: absolute;
    top: 50%;
    left: 1rem;
    transform: translateY(-50%);
    z-index: 100;
}

.psr_slider_dots{
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
}

.psr_slider_dot{
    height: 16px;
    width: 16px;
    border-radius: 8px;
    background: #bbb;
    display: inline-block;
    opacity: 0.5;
    transition: 1s;
}
.active{
    opacity: 1 !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="psr_slider">
        <div class="psr_slide">
        <div class="psr_slides psr_slide_1"></div>
        <div class="psr_slides psr_slide_2"></div>
        <div class="psr_slides psr_slide_3"></div>
        <div class="psr_slides psr_slide_4"></div>
    </div>
        <div class="psr_slider_right" onclick="plusSlides(1)">
            <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="#fff" class="bi bi-chevron-right" viewBox="0 0 16 16">
                <path fill-rule="evenodd" d="M4.646 1.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1 0 .708l-6 6a.5.5 0 0 1-.708-.708L10.293 8 4.646 2.354a.5.5 0 0 1 0-.708z"/>
              </svg>
        </div>
        <div class="psr_slider_left" onclick="plusSlides(-1)">
            <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="#fff" class="bi bi-chevron-left" viewBox="0 0 16 16">
                <path fill-rule="evenodd" d="M11.354 1.646a.5.5 0 0 1 0 .708L5.707 8l5.647 5.646a.5.5 0 0 1-.708.708l-6-6a.5.5 0 0 1 0-.708l6-6a.5.5 0 0 1 .708 0z"/>
              </svg>
        </div>
        <div class="psr_slider_dots">
            <div class="psr_slider_dot" onclick="currentSlide(1)"></div>
            <div class="psr_slider_dot" onclick="currentSlide(2)"></div>
            <div class="psr_slider_dot" onclick="currentSlide(3)"></div>
            <div class="psr_slider_dot" onclick="currentSlide(4)"></div>
        </div>
    </div>
    <script src="main.js"></script>
</body>
</html>

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

Keeping an Rxjs observable alive despite encountering errors by simply ignoring them

I am passing some values to an rxjs pipe and then subscribing to them. If there are any errors, I want to skip them and proceed with the remaining inputs. of('foo', 'bar', 'error', 'bazz', 'nar', 'erro ...

Unable to achieve desired result when trying to combine styled components props with next adjacent selector

After transitioning from active classes to passing props to styled components, I encountered a minor issue. The prop passing is functioning correctly, but there seems to be an issue with the CSS. Prior to switching to props, this section worked flawlessly ...

The ng-repeat directive fails to acknowledge changes in the model

I'm having trouble iterating over the properties of a model that I am updating. Below is the code snippet from my controller: app.controller('VariantsController', ['$http', function($http){ var ctrl = this; this.cars = []; ...

How come the action doesn't trigger in my React/Redux application?

Currently, I am in the process of developing a small application that incorporates modals. Instead of relying on pre-made packages like react-modal, I have taken the initiative to build the modal functionality from scratch. 1) Defining a reducer located i ...

Implement Next.js deployment on NGINX server with a 403 forbidden error

I am currently utilizing Next.js for the frontend and Django for the backend. During development, everything is functioning properly. However, when transitioning to production, I am encountering a 403 Forbidden Error related to /_next/static/chunks. It app ...

Creating intentional blank space before the <Span> element using CSS in HTML

Currently working on a table with some spanned elements for customization. One issue I am facing is the persistent blank space above the blue spanned element in grid 6. Even when adding more spans to grid 6, they just jump below the blue one. How can I mak ...

Tips for Validating Radio Buttons in Angular Reactive Forms and Displaying Error Messages upon Submission

Objective: Ensure that the radio buttons are mandatory. Challenge: The element mat-error and its content are being displayed immediately, even before the form is submitted. It should only show up when the user tries to submit the form. I attempted to use ...

Bootstraps paves the way for the destruction of a dropdown element

https://getbootstrap.com/docs/4.0/components/dropdowns/ $().dropdown('dispose') Erases the dropdown functionality of an element. What exactly does this mean? Does it remove the tag entirely? Does it render the tag unusable as a dropdown lis ...

Tips for saving checkbox values in an array in the sequence they are selected

I need users to select items based on priorities, with a total of four items available. <input type="checkbox" value="1">Carrot <input type="checkbox" value="2">Apple <input type="checkbox" value="3">Banana <input ...

Pressing the button will allow you to select and copy the text within the

I am looking to incorporate a mock-chat feature into my website. The concept is to type something on the website, then click a button next to it which will move the text to a frame above. I attempted this using a textarea and even found a code for selectin ...

What is the best way to adjust CSS vw and vh values after resizing your website?

Utilizing vw and vh values in my CSS transform has resulted in the cloud image moving smoothly across the screen, from left to right, based on the actual window width. While this works well for various window sizes initially, I have encountered an issue w ...

Alert: An invalid value of `false` was received for the non-boolean attribute `className`. To properly write this to the DOM, please provide a string instead: className="false" or use a different

While many have tried to address this issue before, none of their solutions seem to work for me... The error I am encountering is: If you want to write it to the DOM, pass a string instead: className="false" or className={value.toString()}. If ...

Using MDBootstrap for reactjs, incorporating a modal into a table for enhanced functionality

As a newcomer to the world of React.js and Material Design Bootstrap, I am attempting to load a dataset onto a table using a mock JSON file. After some trial and error, I managed to achieve a certain level of success with this task. My goal is to include a ...

When hosted, OpenCart encounters a JavaScript error stating that the property "document" cannot be read because it is null

After successfully running opencart on my local machine, I encountered some errors upon uploading it to the hosting/server. The specific error message is as follows: Uncaught TypeError: Cannot read property 'document' of null f.each.contents @ j ...

Performing a series of synchronous JavaScript calls in Node.js by executing GET requests within a for loop

I'm utilizing super-agent in node to interact with a public API and I'm curious if there's a way to make synchronous calls using it. Although I appreciate its automatic caching feature, my research online suggests that synchronous calls are ...

Refreshing AJAX content with a dynamically adjusting time interval

I am facing a scenario where I have a webpage featuring a countdown alongside some dynamic data refreshed via AJAX. To optimize server load, I found a clever solution by adjusting the AJAX refresh interval based on the time remaining in the countdown, foll ...

Is it possible to dynamically import a Vue.js component based on a prop value?

Currently, I am utilizing the Material design Icons library specifically made for Vue.js. In this library, each icon is a standalone file component within its designated folder. Within my project, I have a separate component called ToolbarButton, which in ...

How can I modify my code to ensure that trs and th elements are not selected if their display property is set to none?

I am currently working on creating a filter for my pivot table, but I am unsure of how to dynamically calculate the sum of each row/column based on whether they are displayed or not. If you need any other part of my code, feel free to ask. To hide employee ...

Hide all elements in jQuery that do not have a class assigned

I've implemented a straightforward jQuery script that toggles the "active" class on an li element when it is clicked: $('li').click(function() { $(this).toggleClass('active'); }); My goal is to hide all other li elements in t ...

event handler in JavaScript that triggers upon a click

Encountering an issue with my code <tr> <td id="<?php echo; $id ?>" onclick="addrow(?php echo $id; ?>)">...</td> </tr> <tr id="<?php echo $id; ?>" class="ndisplay">...</tr> <tr id="<?php echo $i ...