Closed andrewscofield closed 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)
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.
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 );
}
}
});
Cool, that makes perfect sense. Thanks for the explanation.
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 aselect
orsettle
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 myselect
andsettle
listeners fire just fine.Using the latest version of flickity and flickity-hash and vanilla js.