The Carousel feature functions properly with 3 or more slides, but malfunctions when there are only 2 slides present

After implementing a minimal carousel, I discovered that it functions perfectly with 3 or more slides. However, as soon as I remove some slides, issues start to arise. Some of the problems I encountered include:

  1. The sliding animation is removed on 'next'
  2. The divs begin to flicker
  3. The animation breaks entirely

I attempted some solutions involving .clone() on the initial slide and then using .append() to add it to the end of the container. While this approach somewhat worked, it usually only allowed for one rotation before getting stuck on slide2.

Below is the logic I developed for handling clicks on the prev/next buttons:

var before_clone = $(elm + ':first').clone();
var after_clone = $(elm + ':last').clone();
$('#buttons a').click(function(e) {
    //slide the item
    if (container.is(':animated')) {
        return false;
    }
    if (e.target.id == previous) {
        container.stop().animate({
            'left': 0
        }, 1500, function() {
            //container.find(elm + ':first').before(container.find(elm + ':last'));
            container.append(after_clone);
            container.find(elm + ':last');
            resetSlides();
        });
    }
    if (e.target.id == next) {
        container.stop().animate({
            'left': item_width * -1
        }, 1500, function() {
            //container.find(elm + ':last').after(container.find(elm + ':first'));
            container.append(before_clone);
            container.find(elm + ':first');
            resetSlides();
        });
    }
    //cancel the link behavior            
    return false;
});

And here is the logic I implemented for managing automatic animations:

function rotate() {
    $('#next').click();
      container.append(before_clone);
      container.append(after_clone)
}

Here are two Fiddles that may help diagnose my problem:

Link to current attempt

Link to original code (ensure to remove/comment out 2 of the lis)

If you have any insights or solutions to offer, I would greatly appreciate your assistance in resolving this issue! Thank you!

Answer №1

Check out this code snippet.

I made a tweak to your next as shown below:

if (e.target.id == next) {
    container.find(elm + ':last').after(container.find(elm + ':first'));
    container.css({
        'left': 0
    });
    container.stop().animate({
        'left': item_width * -1
    }, 1500, function () {
        resetSlides();
    });
}

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

unable to effectively test promises with stubs using nodejs mocha

Currently, I am facing a couple of challenges when attempting to perform unit testing on promises using Mocha. 1) The main issue I encounter is an Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Prom ...

MaxwidthLG in Material UI design

Hi there, I've encountered an issue with Material UI CSS. Even after attempting to override it, the desired effect is still not achieved. My goal is for the entire div to occupy the full page but this is the current result: https://i.stack.imgur.com/b ...

Master the Art of Animating Letters in the DOM by Navigating Through Any Array of Characters

I am attempting to implement a typewriter effect animation where a new message is displayed and animated as I type into an input box. Initially, I tried using a global char variable to iterate through each element of the array. However, whenever I passed ...

How do I create individual tables for each JSON array within my object using React and MaterialUI?

I have a unique challenge with a component that creates multiple tables, all within one <TableContainer>. The issue lies in the fact that every table is confined within the same container and I am unable to customize each table separately. Each tabl ...

AngularJs JSON endpoint modifier

I've been working on a simple weather app in Angular for practice, but I've hit a roadblock. Here's the Angular JSON feed I'm using: app.factory('forecast', ['$http', function($http) { return $http.get('http: ...

Exploring JqueryUI tab navigation with the help of the <a> tag

I have come across several articles mentioning the possibility of navigating JqueryUI tabs using a button or anchor tag. Here is the method I am currently using: $("#vtabs").tabs(); $("#tabsinner").tabs(); $(".changeTab").click(function() { alert("as ...

The VueJs input file @change event only triggers once in Chrome after the first call

Here is the code snippet I am currently working with: HTML: <input id="task-message-input-upload" type="file" ref="file" @change="handleFileUpload($event)" /> Javascript : data() { return { uploadedFiles: [], show ...

Is it possible to refactor this forwardRef so that it can be easily reused in a function?

Currently, I am in the process of transitioning my application to Material UI V4 and facing challenges with moving my react router Link components into forwardRef wrapped components when setting the 'to' prop programmatically. The code below doe ...

Troubleshooting CSS Carousel Navigation Problems

{% extends 'shop/basic.html' %} {% load static %} {% block css %} .col-md-3 { display: inline-block; margin-left:-4px; } .carousel-indicators .active { background-color: blue; } .col-md-3 img{ width: 227px; ...

Setting value in Angular's ng-repeat directive

Utilizing ng-repeat, I am creating a table with radio buttons. The main goal is to assign each radio button a value based on the position of the object in the original array (before any sorting takes place). However, using $index assigns the position in ...

npm WARNING: The package @angular-devkit/[email protected] is in need of a peer dependency xxxx, however no installation for this dependency has

Attempting to launch an angular project and encountering the following errors: $ npm install npm WARN @angular-devkit/[email protected] requires a peer of @angular/compiler-cli@^14.0.0 but none is installed. You must install peer dependencies yoursel ...

The persistent issue of window.history.pushstate repeatedly pushing the identical value

I need some assistance with my Vue application. I am trying to update the URL when a user clicks on an element: const updateURL = (id: string) => { window.history.pushState({}, '', `email/${id}`); }; The issue I'm facing is th ...

What is the best way to interweave my objects within this tree using recursion?

I am working on creating a new function called customAdd() that will build a nested tree structure like the one shown below: let obj = [] let obj1 = { key: "detail1Tests", id: "94d3d1a2c3d8c4e1d77011a7162a23576e7d8a30d6beeabfadcee5df0876bb0e" } ...

Options for validating data in the igGrid component within igniteui-angular

I am currently working with an igniteui-angluar <ig-grid> and I am interested in validating cells using the checkValue event within the validatorOptions framework. Below is my configuration for the features section: HTML: <features> < ...

Is there a way to create a JavaScript function that relies on a successful form submission?

After attempting to modify a post on Stack Overflow, I am facing issues with my JavaScript code. As a beginner, it's possible that I overlooked something in the code causing it not to work as expected. The project I'm working on involves creatin ...

Tips for successfully retrieving a boolean value from an ASP.Net JavaScript AJAX request using a C# method

Query: Is there a way to call a C# function from JavaScript code on an .aspx webpage to get authentication results based on a username and password? Here is the JavaScript AJAX POST request I am currently using: $.ajax({ type: "POST", ...

Guide on utilizing angularjs $q.all() method with a dynamically generated set of promises

I am currently facing an issue with using $q.all() to wait for multiple promises to be resolved. I have created an array of promises and passed it to the all() method, but it doesn't seem to be working as expected. The promises in the array are gener ...

What is the best way to retrieve a specific property or attribute of a specific div element after obtaining it from e.target.parentNode?

e.target.parentNode will return this element. <div class="quantity"> <span class="glyphicon glyphicon-minus-sign"></span> <span class="product_Count"> 4 </span> <spa ...

Requests in Node.js may unexpectedly stall and remain unresolved until the server is restarted

I've encountered a strange and seemingly sporadic issue with our web application that I can't seem to resolve through debugging. The app runs smoothly for varying durations, ranging from 10 minutes to 6 hours, but then all of a sudden, any remote ...

What is the best way to update or reload an embedded application/pdf?

I have implemented an embed code using application/pdf in order to display a pdf document on the website. To dynamically change the src attribute of the embed when a link is clicked, I utilized javascript. I confirmed this action by displaying alerts to e ...