metafizzy / flickity

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

Cannot call jQuery .flickity method inside ready.flickity event #964

Open krjo opened 5 years ago

krjo commented 5 years ago

I need to remove a certain slide on mobile devices. I'm waiting for Flickity to initalize with the ready.flickity event, then using the remove method, and getting an error that Flickity is not initialized.

Console error:

flickity not initialized. Cannot call methods, i.e. $().flickity("remove")

Test case: https://codepen.io/krjo/pen/zVgaYY

aeblin commented 5 years ago

I have a similar issue. Attempting to get ahold of a data-attribute on the first slide of a slideshow, and can't utilize the slides property within the ready event. Moreover, attempting to access the flickity object within the ready event returns with undefined.

What would be the right way to call a method that requires the flickity object to exist immediately after instantiation?

svsdesign commented 4 years ago

I think I'm experiencing a similar issue here with scrolling to the first slide

https://codepen.io/svsdesign/pen/QWjowZp

Unless I'm misunderstanding the whole point of the ready.flickity function?

desandro commented 1 year ago

In both cases, you can resolve the issue by calling your method after initialization, without the ready function.

revised krjo's example:

$carousel.flickity();
$carousel.flickity('remove',$('.carousel-cell:first-child'));

revissed svsdesign example

  $carousel.flickity({
     prevNextButtons:  false,
     pageDots: false
   });
  $carousel.flickity("next");

What's going on When using Flickity as a jQuery plugin - the plugin instance is created as the last part of initialization. Unfortunately, that means you cannot use jQuery plugin methods like .flickity('remove') within the ready.flickity event.