Difficulty with the Testimonials Carousel Javascript

I'm currently attempting to create a Testimonials slider using JavaScript, but I'm facing an issue with the slider not transitioning to the next slide. It functions correctly when on the first or last slide, but fails to move otherwise. I've explored multiple solutions but none have proven successful. As a novice, I suspect the solution may be evident, yet I can't seem to identify it. Thank you for any assistance!


              var counter = 0;
              var testimonials = document.getElementsByClassName("container");
              testimonials = Array.from(testimonials);
              var arrowleft = document.getElementsByClassName("arrowleft");
              var arrowright = document.getElementsByClassName("arrowright");
              var move = 0;

              for (let i = 0; i < arrowleft.length; i++) {
                  arrowright[i].addEventListener("click", getNextTestimonial);
                  arrowleft[i].addEventListener("click", getPreviousTestimonial);
              };

              function getPreviousTestimonial(){
                  if (testimonials[0]){
                      counter = 3;
                  } else {
                      counter += 1;
                  };
                  for (var key in testimonials){
                      move = -135 * counter;
                      testimonials[key].style.transition = "transform 1.5s ease";
                      testimonials[key].style.transform = "translate(" + move + "%)";
                  };
              }
              function getNextTestimonial(){
                  if (testimonials[3]){
                      counter = 0;
                  } else {
                      counter += 1;
                  };
                  for (var key in testimonials){
                      move = 135 * counter;
                      testimonials[key].style.transition = "transform 1.5s ease";
                      testimonials[key].style.transform = "translate(" + move + "%)";
                  };  
              }
           
 .............CSS CODE.............

               <!DOCTYPE html>
               <html lang="en">
                  <head>
                     <meta charset="UTF-8">
                     <meta name="viewport" content="width=device-width, initial-scale=1.0">
                     <title>Testimonials</title>
                     <link rel="stylesheet" href="testimonials.css">
                  </head>
                  <body>
                     <h1>CLIENT TESTIMONIALS</h1>
                     <div id="maincontainer">
                          .............HTML CODE.............
                     </div>
                  </body>
               </html>
            

Answer №1

To enhance the functionality of your move formula, ensure that you adjust your `if` statements to incorporate the value `-135` in both scenarios. Additionally, remember to decrease the counter by `1` when navigating to the previous slide.

var counter = 0;
var testimonials = document.getElementsByClassName("container");
testimonials = Array.from(testimonials);
var arrowleft = document.getElementsByClassName("arrowleft");
var arrowright = document.getElementsByClassName("arrowright");
var move = 0;

for (let i = 0; i < arrowleft.length; i++) {
    arrowright[i].addEventListener("click", getNextTestimonial);
    arrowleft[i].addEventListener("click", getPreviousTestimonial);
};

function getPreviousTestimonial() {
    if (counter === 0) {
        counter = 3;
    } else {
        counter -= 1;
    };
    for (var key in testimonials){
        move = -135 * counter;
        testimonials[key].style.transition = "transform 1.5s ease";
        testimonials[key].style.transform = "translate(" + move + "%)";
    };
};
function getNextTestimonial(){
    if (counter === 3) {
        counter = 0;
    } else {
        counter += 1;
    };
    for (var key in testimonials){
        move = -135 * counter;
        testimonials[key].style.transition = "transform 1.5s ease";
        testimonials[key].style.transform = "translate(" + move + "%)";
    };  
};
...

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

Filtering an array of JSONs in Vue.js using a separate array of JSONs

In my code, there are two arrays of JSONs: one named products and another called deletedProducts. The task is to filter out the products that are not present in the deletedProducts array. For instance: products = [ { id: 1, name: 'Box&apos ...

How can I extract the id of a clicked item and pass it to a different page with Jquery?

Facing an issue where the attribute value of a clicked href tag is not retained after browser redirection. Initially, when clicking on the href tag, the value is displayed correctly. However, upon being redirected to the peoplegallery_album, the id becomes ...

The dynamic Vue.js transitions and effects are like a magic

I am using a v-for to render multiple child components: <div v-for="(pass) in scoringPass" :key="pass.decision"> <Pass :pass="pass"/> </div> Each of these child components contains a transition tag: &l ...

I am currently using jQuery autocomplete in an attempt to display the value in the textbox as a table. The data is not being retrieved from a JSON source using Ajax directly in the text

Attempting ajax with json for autocomplete using Jquery for the first time, aiming for auto-complete appearance but table-like structure. Below is the jquery code snippet: $("document").ready(function (){ $(function () { $.ajax({ url: "dum ...

"Efficiently fetch data with an Express GET request without the

My goal is to send the client the HTML page (create.html) in response to a GET request triggered by a button click using fetch. I am intentionally avoiding the use of a form due to formatting and potential scalability issues. The code acknowledges that the ...

Instructions on creating an input field and a slider simultaneously

Currently, I am exploring the world of CSS 3D transforms and I am particularly interested in creating sliders to manage these transforms. I recently stumbled upon the Three.js editor and was impressed by how it handles the positioning, rotation, and scalin ...

What is the best way to transfer data from Vue.js to a PHP variable?

<section class="scans"> <h3>Scans</h3> <ul v-if="scans.length === 0"> <li class="empty">No scans yet</li> </ul> <transition-group name="scans" tag="ul"> <li v-for ...

Ensure a field is required if the integer field is not empty in Angular Schema Form

I am currently working on setting up a form using Angular JSON Schema Form. My goal is to make one field (dropdown1) required only when another field (number1) has been filled in. I have managed to successfully get the following form + schema to function p ...

The method this.$refs.myProperty.refresh cannot be invoked as it is not a valid

I have added a reference value 'callingTable' to a vue-data-table (using vuetify) like so: <v-data-table :headers="callingTableHeaders" :items="getCallingDetails" class="elevation-1" ref="callingTable&quo ...

What is the reason behind using AJAX to attempt sending a new URL request on

Having a strange issue with my product list. When trying to edit a product by clicking on it, I am redirected to the product form where an AJAX request is supposed to be sent to getFiltersGroup. However, on error, the AJAX request is somehow being sent to ...

Accessing PHP variables within the HTML portion of a PHP document

Setting up my own contact form for the first time using PHP has been a learning experience. While I am able to receive emails from the form, I'm struggling to display the user-inputted information in the email. The HTML <label for="name"> ...

What is the best way to retrieve the value of a selected button from a v-btn-toggle?

<v-btn-toggle v-model="toggle_one"> <v-btn flat> CAD50 </v-btn> <v-btn flat> CAD100 </v-btn> <v-btn flat> CAD1000 </v-btn> <v-btn flat> CAD10000 </v-btn> ...

showing the name in place of the identification number

Recently, I created a page that showcases all the truck plate numbers in a table, and upon clicking each plate number, it redirects to a page displaying the data associated with that specific plate number. However, there is an issue with displaying the tru ...

I am looking for a sample code for Tizen that allows scrolling of a <div> element using the bezel

Seeking a functional web application in Tizen that can scroll a div using the rotating bezel mechanism. I have dedicated several hours to getting it to work without success. I keep revisiting the same resources for the past three days: View Link 1 View Li ...

"Encountering a hiccup with the Firebase service worker in Messaging and Firebase

I am interested in developing a small web application to explore the capabilities of Firebase Cloud Messaging for web apps. My intention is to utilize Firebase Hosting as the hosting platform for my app. ISSUE: Upon allowing the notification pop-up on my ...

Combining classes in SCSS to create more versatile styles

Here is some code snippet: input { border:1px solid black; color:#FFF; } Another snippet: .input { padding:0px 5px 0px 3px; } How can we use SASS/SCSS to achieve the following: input.input This will apply the .input class directly to ...

Executing javascript code within the success function of the $ajax method in jQuery: A step-by-step guide

The code snippet below includes a comment before the actual code that is not running as expected. $(document).on('click', '#disable_url', function (e) { e.preventDefault(); var items = new Array(); $("input:checked:no ...

The Bootstrap grid feature is malfunctioning on both Codeply and devtools. The dimensions of the <div class="col-lg-3 col-md-4 col-sm-6"> element remain constant despite attempts to adjust them

After experimenting with the viewport meta tag and enclosing it in a div class="container", I found that the issue persists on Codeply and Chrome DevTools. However, when I adjust my browser size, the problem seems to disappear. I also tried using various v ...

The arrow icon that I included in my bootstrap project seems to have disappeared from my view

While trying to enhance my footer, I attempted to include an arrow-up-circle icon. However, after adding the code, the icon doesn't appear. Here is the html snippet I used: <html> <head> <link href="https://cd ...

Saving the id in a variable after triggering an AJAX request by clicking

I attempted to utilize an onClick event within the <a> element but it is not functioning as expected. I am aiming to capture the value 777 into a variable so that I can access it later in the code. My intention was to click on the image and have an ...