Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync auto play behaviour #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions flickity-sync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Flickity sync v2.0.0
* Flickity sync v3.0.0
* enable sync for Flickity
*/

Expand Down Expand Up @@ -58,19 +58,41 @@ proto.sync = function( elem ) {
*/
proto._syncCompanion = function( companion ) {
let _this = this;
function syncListener() {
function syncListenerSelect() {
let index = _this.selectedIndex;
// do not select if already selected, prevent infinite loop
if ( companion.selectedIndex !== index ) {
companion.select( index );
}
}
this.on( 'select', syncListener );
this.on( 'select', syncListenerSelect );

function syncListenerPointerDown() {
if ( !companion.options.autoPlay ) {
return;
}
companion.stopPlayer();
}
this.on( 'pointerDown', syncListenerPointerDown );

// pause auto play on hover of companion
if ( companion.options.autoPlay && companion.options.pauseAutoPlayOnHover ) {
this.element.addEventListener( 'mouseenter', function() {
companion.pausePlayer();

_this.element.addEventListener( 'mouseleave', function _mouseleave() {
companion.unpausePlayer();
_this.element.removeEventListener( 'mouseleave', _mouseleave );
});
});
}

// keep track of all synced flickities
// hold on to listener to unsync
this.syncers[ companion.guid ] = {
flickity: companion,
listener: syncListener,
listenerSelect: syncListenerSelect,
listenerPointerDown: syncListenerPointerDown
};
};

Expand Down Expand Up @@ -100,7 +122,8 @@ proto._unsync = function( companion ) {
proto._unsyncCompanion = function( companion ) {
let id = companion.guid;
let syncer = this.syncers[ id ];
this.off( 'select', syncer.listener );
this.off( 'select', syncer.listenerSelect );
this.off( 'pointerDown', syncer.listenerPointerDown );
delete this.syncers[ id ];
};

Expand Down