metafizzy / flickity

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

Flickity HTML Initialisation #277

Closed aaronstezycki closed 9 years ago

aaronstezycki commented 9 years ago

Is there anyway to achieve a callback like this #269, but by initialising with html ?

desandro commented 9 years ago

Hmm, interesting request.

If you are using jQuery, you can listen to the initial cellSelect event. See demo http://codepen.io/desandro/pen/NGzNgP

// capture initial cellSelect
$('body').one( 'cellSelect', function( event ) {
  console.log( 'initial cell select on ', event.target )  
});

But this code is a race condition, and may happen after the initial event has been triggered.

A better solution is to initialize in HTML with your own code so you don't have to hack it. See demo http://codepen.io/desandro/pen/yYEOza

// init Flickity on all .js-custom-flickity elements
$('.js-custom-flickity').each( function( i, elem ) {
  var $elem = $(elem);
  var optionsAttr = $elem.attr('data-flickity-options');
  var options = optionsAttr ? JSON.parse( optionsAttr ) : {};
  // init Flickity
  $elem.flickity( options );
});
aaronstezycki commented 9 years ago

Using a json parse. Brilliant, I had thought of that but wasn't quite sure how to execute it. This worked perfectly! :)

Julix91 commented 5 years ago

Seems all the older ones point at non https versions of the link - wonder if that's fixable with a redirect or something?

Screen Shot 2019-03-29 at 1 58 15 PM

desandro commented 5 years ago

@Julix91 pen files updated.