metafizzy / flickity-hash

Select Flickity slides with links
11 stars 10 forks source link

Event fired for hash? #2

Closed andrewscofield closed 6 years ago

andrewscofield commented 6 years ago

I noticed that on page load with a hash on it (e.g. #slide1), flickity perfectly loads the correct cell on load. But I'm not getting a select or settle event fired for it. This is only an issue because I'm using a custom navigation UI. Is an event supposed to fire? Looking through the code, it looks like it should but I don't know. Otherwise my select and settle listeners fire just fine.

Using the latest version of flickity and flickity-hash and vanilla js.

desandro commented 6 years ago

If you're trying to capture the initial select event, you can do it with the on option (New in Flickity v2.1)

andrewscofield commented 6 years ago

Yep I am trying to capture that initial event, but it wasn't firing if I used the normal .on('select') method. Just tried the new on option like you suggest in this test case here:

https://s.codepen.io/andrewscofield/debug/erzmWG/mWkoNbndjDgA#slide3

Works as expected, I'll change my code to use the new option.

Whats the different between on('select') and on option? From the docs I saw the new on option as a convenience or different approach. But if look at that test, only one of them fires on initialization.

desandro commented 6 years ago

Good question! It's a synchronous execution thing.

// using on method
var flkty = new Flickity('.carousel', {
  // options
});

// Flickity initialized, initial select event triggered

// bind listener for subsequent events
flkty.on('select', function(index){
  console.log('Flickity select from method: '+ index)
});
// using on option
var flkty = new Flickity('.carousel', {
  // add event listener for Flickity to be initialized with
  // initial events not yet triggered
  on: {
    select: function(index){
      console.log('Flickity select from option: ' + index );
    }
  }
});
andrewscofield commented 6 years ago

Cool, that makes perfect sense. Thanks for the explanation.