How to make a multi item carousel from materialize(css) cards when the cards data is provided by a loop?

The code here generates a list of cards using the for loop. These cards are to be put in a carousel with 4 cards visible each time and a next arrow button brings the next 4 cards. I have used materialize cards.

''' <div class="row">     {% for course in course_details %}             <div class="col s3">                 <div class="card medium">                     <div class="card-image">                         <a href="{{ course.0 }}"><img src="{{ course.0 }}" alt=""></a>                     </div>                     <div class="card-content">                         <p>{{ course.1 }}</p>                     </div>                     <div class="card-action">                         <a href="{{ course.1 }}">Price</a>                     </div>                 </div>             </div>     {%endfor%}     </div> ''' 
Add Comment
1 Answer(s)

One way to do this:

  1. Create a carousel-slider:

    <div class="carousel carousel-slider"></div>

  2. Create a carousel-item for every 4 cards. This is a single slide, and needs also a class of row so that we can use the grid on the cards:

    <div class="carousel-item row"></div>

  3. Place your cards:

<div class="col s3"><div class="card blue-grey darken-1"></div></div>

And then initialise:

document.addEventListener('DOMContentLoaded', function() {     var elems = document.querySelectorAll('.carousel-slider');     var instances = M.Carousel.init(elems);   }); 

https://codepen.io/doughballs/pen/RwrYBYx

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.