The Carousel Slider malfunctions when attempting to set up multiple carousels

I recently created a carousel slider that seems to be behaving incorrectly when multiple carousels are added. It should slide by one item at a time. For instance, with only one carousel, it slides by one item; with two carousels shown, it slides by two items.

I've been unable to pinpoint the issue in my code where the sliding is based on the number of carousels shown. Below is a snippet of the code I'm currently troubleshooting. My goal is to have the carousel slide only by one item at a time.

<div class="faculty-carousel">
    <ul class="faculty-items">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        <li>Item 6</li>
    </ul>

    <div class="controls">
        <div class="prev">
        prev
        </div>

        <div class="next">
        next
        </div>
    </div>
</div>

... (repeated sections) ...


<script type="text/javascript">


var activeSlide = 0;
$('.faculty-carousel').attr('data-slide', '0');



$('.prev').on('click', function(e) {
    event.stopPropagation();

    var carouselWrapper     = $(this).closest('.faculty-carousel'),
        facultyProfilePanel = carouselWrapper.find('.faculty-items li'),
        facultyProfileCount = facultyProfilePanel.length,
        viewPortSize        = $(window).width(),
        carousel            = carouselWrapper.find('.faculty-items'),
        position            = 0,
        currentSlide        = parseInt(carouselWrapper.attr('data-slide'));

    // Check if data-slide attribute is greater than 0
    if (currentSlide > 0) {
        // Decrement current slide
        currentSlide--;
        // Assign CSS position to clicked slider
        var transformPercentage = -1 * currentSlide / facultyProfileCount * 100;
        carousel.css('transform', 'translateX(' + transformPercentage + '% )');

        // Update data-slide attribute
        carouselWrapper.attr('data-slide', currentSlide);
        activeSlide = currentSlide;
    }

    console.log('prev');

});

... (other script sections) ...

</script>

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}

... (other CSS reset rules) ...

.faculty-items li {
    height : 100px;
}

... (other CSS styles) ...

Answer №1

One major issue is within the fCarousel function, specifically where profileCount ends up tallying every element from each individual carousel.

facultyPanel = $('.faculty-carousel .faculty-items li'),
profileCount = facultyPanel.length,

This results in all carousels being set to a width equivalent to the total number of items across all carousels on the page.

carousel.outerWidth( profilePanelSize * profileCount );
facultyPanel.outerWidth(profilePanelSize);

The problem arises when attempting to implement a percentage-based shift on the carousel using the prev and next click events (due to the width being calculated based on the total number of items including those from other carousels).

carousel.css('transform', 'translateX(' + transformPercentage + '% )');

A better approach would be to iterate through each carousel (carouselWrapper) individually and adjust its width based solely on the items it contains.

var viewPortSize        = $(window).width(),
    carouselWrapper = $('.faculty-carousel')
;

...

carouselWrapper.each(function(){
    var wrapper = $(this);

    var facultyPanel = wrapper.find('.faculty-items li'),
    profileCount = facultyPanel.length,
    carousel            = wrapper.find('.faculty-items');

    carousel.outerWidth( profilePanelSize * profileCount );
    facultyPanel.outerWidth(profilePanelSize);
    carousel.css('transform', 'translateX(' + 0 + '% )');
});

Additionally, certain CSS styles have been applied to the carousel elements for visual enhancement and responsiveness.

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

When you initiate a prevent default, both the Angular and Bootstrap UI tabs will become active simultaneously

I am facing a CSS issue while utilizing Angular UI-Tab. The problem arises when I have two tabs and need to stop tab switching based on a specific condition. To achieve this, I implemented prevent default. However, the CSS displays both tabs as active bec ...

Learn how to easily incorporate a drop-down list into the material-UI Search component within the navbar to enhance the search results

I integrated a Material UI search bar into my React app's navbar following the instructions from the official documentation on MUI. However, the article does not provide any guidance on how to add a dropdown list when selecting the search input field. ...

Using HTML and CSS to create a Contact Form

Greetings! I have come across this contact form code: /* Custom Contact Form Styling */ input[type=text], [type=email], select, textarea { width: 100%; padding: 12px; border: 1px solid #555; margin-top: 6px; margin-bottom: 16px; resize: v ...

Issue with ngModelChange and change events not functioning properly in Internet Explorer 11

Within a text input field, I have implemented single-way binding in addition to utilizing a number formatter pipe. I have also set up an (ngModelChange) event handler to remove any commas that are added by the number formatter, and a (change) event to tri ...

I keep encountering the issue where I receive the message "Unable to access property 'innerText' of an undefined element" when running the Array forEach function. This problem seems to be happening within the HTMLInputElement section of the code

I am facing an issue where the error occurs because "cardTxt" is not recognized as a string. I verified this using typeof syntax, but I'm unable to understand why it can't be a string. This code snippet includes the use of bootstrap for styling. ...

Using jQuery to Drag: Creating a draggable effect on a div element

I recently posted a question, but I feel like I need to rephrase it to make it more general. Basically, I have an element that can be moved around and resized using jQuery. I used CSS to attach another div to the right of this element. Initially, when you ...

Solutions for retrieving the selected row in a bootstrap modal using knockout js

I'm currently working on developing a master-detail page using Knockout JS, as shown in the image below. I've encountered an issue with retrieving values from a Bootstrap modal in the details section. For instance, upon clicking the browse butto ...

Specify "Accept" headers using jQuery when fetching data within an iframe

I am working on a webpage where I am adding an iframe dynamically like this: $('<iframe id="testIframe" />').src('http://www.google.nl/').appendTo('body'); The accept headers being sent while loading the content in thi ...

Including HTML elements while cycling through a jQuery collection

Is there a more efficient method of integrating somewhat intricate HTML into a webpage than what I'm currently employing? function display(friends) { $(".row").empty(); $.each(friends, function(index, friend) { var html = '<d ...

Clearing up memory in ThreeJS application

I'm facing challenges with memory management in my ThreeJS application. I know there are already a few queries regarding this issue: freeing memory in three.js Freeing memory with threejs Three.js Collada - What's the proper way to dispose() a ...

Dynamic toggling of attributes in Rails

Is there a way to toggle user attributes dynamically without creating separate methods for each attribute? def toggle item = params[:item] id = params[:id] @user = User.find(id) respond_to do |format| if @user.update("#{item}": ! ...

Tips on positioning an element absolutely so that it stops right before another element as the screen width is adjusted

Imagine a scenario where there is a 'parent' block containing various elements, including a button. When the user clicks this button, a 'slide' element should appear and cover the parent block but only up to the button itself (as shown ...

Tips for automatically closing a drop-down menu by clicking anywhere on the page

Hey there, I'm currently working on developing a Single Page Application (SPA) in AngularJS. After a user successfully logs in, I am creating a dropdown menu in the upper right corner that expands when clicked. However, I'm trying to figure out h ...

Utilizing Jquery or Javascript to Establish a Fresh Perspective for CylinderGeometry with Three.js

I'm attempting to change the dimensions of a cylinder created using examples from Three.js at runtime, but my code doesn't seem to be working. Here is the code snippet I am using: HTML <script src="http://www.html5canvastutorials.com/librari ...

Guide on creating line breaks within a list in a Python email

I'm having trouble querying a list from my Flask database and then sending it out as an HTML email. The issue is that I can't seem to break the list items into different lines. For example, instead of: a b c I currently get 'abc' i ...

Alter the webpage's background color using setInterval while also implementing automatic scrolling based on active records

I've created a row of blinking div elements with a time interval, ensuring that only one div blinks at any given moment. To see it in action, please view the demo below: var arr = $(".boxes"); var current = 0; function bgChange() { if (a ...

Having trouble identifying the specific CSS property that is being used or inherited

I have incorporated components from Twitter Bootstrap CSS into a custom CSS file, focusing mainly on toolbar components. Specifically, I have set up a toolbar with various features. However, when applying additional CSS to the .signinbox_left:hover select ...

Two adjacent div tags spanning the full width of the page, with differing widths (40% and 60%) that resize to 100% width and stack on top of each

I've been experimenting with a layout challenge and running into some obstacles. Two div tags are at the heart of the issue. Div A features an image with a default size of 768 x 400, while Div B displays an image at 1152 x 400. These images are meant ...

Implementing a JavaScript Module Using JavaScript

I have encountered a simple problem. I am trying to use two JavaScript files: one following the module pattern and another one that calls the first one. I have tested all the code using Node.js and everything works fine when it is all in one file. However, ...

Modifying the dimensions of media:thumbnail in Blogger's RSS Feed

I came across this post, but it seems like there have been updates in the past 4 years regarding how thumbnails are generated for Blogger posts. I've attempted various methods, but so far none of them have been successful. If anyone could assist me in ...