metafizzy / flickity

:leaves: Touch, responsive, flickable carousels
https://flickity.metafizzy.co
7.52k stars 605 forks source link

Init in JS but set some options for different carousels #994

Closed cheeseytoastie closed 4 years ago

cheeseytoastie commented 4 years ago

Is is possible to do something like?

$carousel = $('.carousel').flickity({ autoPlay: $(this.element).data('carousel-speed'),

where I've put (by setting a value in a CMS) a data attribute on the markup.

There are multiple carousels on the page so though it this works (setting a single div with a value) it's not suitable, how can I get to the carousel element in the initialisation?!

$carousel = $('.carousel').flickity({
    autoPlay: $('.carousel-speed').data('carousel-speed'),
desandro commented 4 years ago

Yes, you can use each() to iterate though the carousel elements https://fizzy.school/un-repeat-with-functions

$('.carousel').each( function( i, carousel ) {
  let $carousel = $( carousel );
  $carousel.flickity({
    autoPlay: $carousel.data('carousel-speed');
  });
});
cheeseytoastie commented 4 years ago

That looks perfect. Thanks!