Last active
December 17, 2015 04:39
-
-
Save andybluntish/5552016 to your computer and use it in GitHub Desktop.
Trigger GalleryCSS changes via JavaScript, eg from swipe events on touch devices. Requires GalleryCSS (obviously) - https://github.com/benschwarz/gallery-css.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* GalleryCss JS | |
* Trigger GalleryCSS changes via JavaScript, eg from swipe events on touch devices. | |
* Requires GalleryCSS (obviously) - https://github.com/benschwarz/gallery-css | |
* | |
* GalleryCss.next(); // move to the next item, or loop back to the start | |
* GalleryCss.prev(); // move to the previous item, or loop back to the end | |
* GalleryCss.goTo(); // jump to a specific item (zero-based index) | |
* | |
*/ | |
var GalleryCss = { | |
_base: '.controls a', | |
_first: ':first-child', | |
_last: ':last-child', | |
_current: function() { | |
var target = window.location.hash, | |
selector = target === '' ? this._first : '[href="' + target + '"]', | |
current = document.querySelector(this._base + selector); | |
// if the URL Hash was not from Gallery CSS, fall back to the first link. | |
if (!current) current = document.querySelector(this._base + this._first); | |
return current; | |
}, | |
_next: function() { | |
var current = this._current(), | |
next = current.nextElementSibling || document.querySelector(this._base + this._first); | |
return next; | |
}, | |
_prev: function() { | |
var current = this._current(), | |
prev = current.previousElementSibling || document.querySelector(this._base + this._last); | |
return prev; | |
}, | |
_at: function(idx) { | |
var list = document.querySelectorAll( this._base ), | |
item = list.item(idx); | |
return item; | |
}, | |
_switchTo: function(item) { | |
var el, href; | |
switch(item) { | |
case 'next': | |
el = this._next(); | |
break; | |
case 'prev': | |
el = this._prev(); | |
break; | |
default: | |
item = parseInt(item, 10); | |
el = this._at(item); | |
} | |
if ( el ) { | |
window.location.hash = el.getAttribute('href'); | |
} | |
}, | |
// Public | |
next: function() { | |
this._switchTo('next'); | |
}, | |
prev: function() { | |
this._switchTo('prev'); | |
}, | |
goTo: function(idx) { | |
this._switchTo(idx); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment