Get ready to showcase a stunning array of 6 items within the viewport using JQuery and Bootstrap 4's Carousel feature

I came across this code snippet that shows how to display multiple items in a carousel simultaneously: https://www.codeply.com/go/YbRfxfTDN1 .

However, my requirement is to show 6 items at once instead of 4. I made adjustments to the Bootstrap grid to have 6 columns and modified the CSS from transform: translateX(25%); to transform: translateX(16.67%);, but it's still not functioning as intended.

I've been experimenting with the jQuery code as well because I believe I need to clone additional items, but I'm facing challenges in making it work.

Is there anyone who can provide assistance?

Note: The number of items will vary as the carousel will be dynamically populated.

   $('#recipeCarousel').carousel({
      interval: 10000
    })
    
    $('.carousel .carousel-item').each(function(){
        var next = $(this).next();
        if (!next.length) {
        next = $(this).siblings(':first');
        }
        next.children(':first-child').clone().appendTo($(this));
        
        for (var i=0;i<2;i++) {
            next=next.next();
            if (!next.length) {
            next = $(this).siblings(':first');
          }
            
            next.children(':first-child').clone().appendTo($(this));
            //some code to display 6 items
          }
    });
   .carousel-inner .carousel-item.active,
    .carousel-inner .carousel-item-next,
    .carousel-inner .carousel-item-prev {
      display: flex;
    }
    
    .carousel-inner .carousel-item-right.active,
    .carousel-inner .carousel-item-next {
      transform: translateX(16.67%);
    }
    
    .carousel-inner .carousel-item-left.active, 
    .carousel-inner .carousel-item-prev {
      transform: translateX(-16.67%)
    }
      
    .carousel-inner .carousel-item-right,
    .carousel-inner .carousel-item-left{ 
      transform: translateX(0);
    }
    
    
    <div class="container text-center my-3">
        <h2>Bootstrap 4 Multiple Item Carousel</h2>
        <div class="row mx-auto my-auto">
            <div id="recipeCarousel" class="carousel slide w-100" data-ride="carousel">
                <div class="carousel-inner" role="listbox">
                    <div class="carousel-item active">
                        <img class="d-block col-2 img-fluid" src="http://placehold.it/350x180?text=1">
                    </div>
                    <div class="carousel-item">
                        <img class="d-block col-2 img-fluid" src="http://placehold.it/350x180?text=2">
                    </div>
                    <div class="carousel-item">
                        <img class="d-block col-2 img-fluid" src="http://placehold.it/350x180?text=3">
                    </div>
                    <div class="carousel-item">
                        <img class="d-block col-2 img-fluid" src="http://placehold.it/350x180?text=4">
                    </div>
                    <div class="carousel-item">
                        <img class="d-block col-2 img-fluid" src="http://placehold.it/350x180?text=5">
                    </div>
                    <div class="carousel-item">
                        <img class="d-block col-2 img-fluid" src="http://placehold.it/350x180?text=6">
                    </div>
                    <div class="carousel-item">
                        <img class="d-block col-2 img-fluid" src="http://placehold.it/350x180?text=7">
                    </div>
                    <div class="carousel-item">
                        <img class="d-block col-2 img-fluid" src="http://placehold.it/350x180?text=8">
                    </div>
                    <div class="carousel-item">
                        <img class="d-block col-2 img-fluid" src="http://placehold.it/350x180?text=9">
                    </div>
                    <div class="carousel-item">
                        <img class="d-block col-2 img-fluid" src="http://placehold.it/350x180?text=10">
                    </div>
                </div>
                <a class="carousel-control-prev" href="#recipeCarousel" role="button" data-slide="prev">
                    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="carousel-control-next" href="#recipeCarousel" role="button" data-slide="next">
                    <span class="carousel-control-next-icon" aria-hidden="true"></span>
                    <span class="sr-only">Next</span>
                </a>
            </div>
        </div>
        <h4>Advances one slide at a time</h4>
    </div>
    
    
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" ></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    

Answer №1

It seems like the key is duplication of more objects. Take note of the condition i<5:

https://www.example.com/code-sample

for (var i=0;i<5;i++) {
        next=next.next();
        if (!next.length) {
            next = $(this).siblings(':first');
        }

        next.children(':first-child').clone().appendTo($(this));
}

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 could be causing the gulp-clean-css task to output an empty file after minifying CSS?

When it comes to minimizing CSS, I rely on gulp-clean-css within a gulp.task. The file path I'm working with is css/mehr.css, and after minification, the new path becomes css/mehr.min.css However, upon running the task : gulp minify_css_task An is ...

Django compressor throwing minification hiccup

I'm currently utilizing a tool for automatic minification known as django compressor. Unfortunately, it seems that the minification process with django compressor is causing errors to be introduced. Updated script with semicolons: Before: var ap ...

Adjust the position of an image to move it closer to the top using CSS

I have a fiddle where I am trying to adjust the position of an image (keyboard) to match a specific design. https://i.sstatic.net/flqCH.png In my fiddle, the keyboard image is slightly lower than the desired position shown in the design. I am looking for ...

Conflicting Angular components: Sorting tables and dragging/dropping table rows

In the current project I'm working on, I've integrated both angular table-sort and angular drag-drop. However, I ran into an issue where dragging a row and attempting to drop it onto another row causes the table sort to forcefully rearrange the r ...

Rails 4 application encountering issues with rendering views when making a $.ajax request

I am a beginner in Rails and I am in the process of sending model data to a controller method from JavaScript for rendering a list of results... function submitResults() { resultURL = "/result"; resultData = JSON.stringify(results); $.ajax({ typ ...

Is it possible to utilize an svg markup element like (g) as the primary element in a React JSX return statement?

I have a desire to accomplish the following: Element Customer / Offspring return ( <Limage> <g id="golf"> <path fill="none" stroke="#fff" stroke-width="4" stroke-linejoin="round" stroke-miterlimit= ...

creating dynamic column headers with JavaScript

I am looking for a way to make the column names dynamic so that I don't have to manually update them every time. Here is my code snippet: jqGrid11.prototype = { display : function() { $('body').append(this.html.join("")); $("#jqGrid").j ...

Tips for successfully passing a variable as props within the confines of a different function's component

Is there a way to pass the variable id to the onConfirm event in this code snippet? Currently, it seems like the function is being called by another function within setAlert. How can I achieve this? const confirmationPrompt = (id) => { setAlert( ...

What methods are most effective for showcasing Flash messages within Express.js?

I have a Node.js app in the EJS framework and I am new to JavaScript. Could someone please advise me on the correct way to set flash messages in Node.js? Below is my code which is throwing an error: C:\Users\sad\Desktop\Node Applica ...

The Power Duo: jQuery Form Plugin paired with jQuery Validate

Have you ever tried using the jQuery Form Plugin in combination with the jQuery Validate plugin? The issue I'm facing is that even when the required fields are empty, the form still submits via AJAX. Check out my code snippet below: // Making the ne ...

The value of the jQuery data on click event handler becomes 'undefined' when used within an AJAX handler

When I click on the Positive or Negative buttons, a jQuery event handler function is triggered. Each button passes different data objects to the handler. However, within the AJAX POST handler, I am unable to access the data from the click event. $('# ...

What specific style of CSS is utilized in Material UI's `sx` property feature?

I came across this code snippet in the Material UI documentation: <FormControl sx={{ "& > :not(style)": { m: 1 }, maxWidth: "100%", minWidth: "100%", }} > It appears that & is not a standard p ...

The three-dimensional object created using Three.js depicts a split torus design with a striking

I have a unique goal of splitting shapes such as a torus, circle, triangle, etc., using a custom plane. Once the shape is split into two parts, each part should be draggable separately. Currently, I am able to split the torus, but I'm facing some iss ...

Looking to access the layout details of an HTML element similar to what can be done in Firefox's debugger tool?

I am facing a challenge with a aspx.net grid control where I need to extract the first row's information and copy it into another table using JavaScript. To achieve this, I am attempting to calculate the width of each cell in the first row by accessin ...

Can you explain the distinction between the various markup languages?

I am confused about the difference between these two markup examples. <div class="pic"> <img src="http://www.lorempixel.com/333/333" alt=""> </div> css .pic { width: 600px; background: red; } Now I can see my background, but when ...

Using Jquery to append a secondary class to an element if it is absent with a class selector

I'm currently working on a website where clicking on an image opens up information about a person in a div. I'm achieving this using toggleClass in jQuery UI. My goal is to close any open div when another one is clicked. I tried using the :not se ...

place an image next to a div element

Currently, I have a table-like structure with two columns that I am struggling to make mobile responsive. Take a look at the code snippet below: setTimeout(function() { odometer.innerHTML = '1925' }) .fiftyFiftySection { back ...

Achieving an oval shape using HTML5 and CSS3: Step-by-step guide

<!DOCTYPE html> <head> <title> Oval shape</title> </head> <body> <div style="width:400px; height:400px; background-color:#ff0;">Oval</div> <p> Exploring the creation of an oval shape using HTML5 ...

Show a picture exclusively on mobile devices

I'm currently working on an email design where I want to display one image only when the email is viewed on a mobile device, and another image when viewed on a desktop. I've managed to hide the desktop image when viewed on mobile using CSS, but ...

jQuery Alert for Double-Checking Email Entry

Is it possible to create a simple alert pop-up using jQuery if the user does not enter the correct email address in the second email input form? Below is my current code: <div id="reservation-request"> <form method="post" action="test.php ...