Last active
August 29, 2015 14:04
-
-
Save cgspicer/e39037508d95fe55360e to your computer and use it in GitHub Desktop.
SwipeJS Rotating Gallery Module
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
/** | |
* Rotating Galleries | |
* @author Ryan Eng - @reng-v1 | |
* @author Coran Spicer - @cgspicer | |
* Use data-gallery to initiate a rotating gallery (id of the gallery) | |
* Use data-gallery-type to set the type of gallery (slide or fade) | |
* Use data-navigability to set if the gallery can be navigate, default is false (true or false) | |
* Use data-speed to set the speed of the gallery | |
* Use data-offset to set the offset of the slides | |
* Example: | |
* data-gallery="slider" | |
* data-gallery-type="slide" | |
* data-navigability="true" | |
* data-speed="3000" | |
* data-offset="3" | |
*/ | |
/** PROOF NOTATION VERSION **/ | |
var RotateGallery = function($el) { | |
var self = this; // unused variable declared | |
var $el = $el; // redundant | |
// I would recommend placing these settings into an argument so that it | |
// can be instantiated without the markup data attr, as well. | |
var typeOfGallery = $el.data('gallery-type'); | |
var navigability = $el.data('navigability'); | |
var speed = $el.data('speed'); | |
var offset = $el.data('offset'); | |
// possible ternary operation to shorten code? | |
if(navigability == null) { // === operator recommended to compare null | |
navigability = false; | |
} | |
var init = function() { | |
switch(typeOfGallery){ | |
case 'fade': | |
if(speed == null) { | |
speed = 1; | |
} | |
if(offset == null) { | |
offset = 3; | |
} | |
var fadeGallery = new FadeGallery(navigability, speed, offset, $el); // you didn't really combine the modules | |
fadeGallery.init(); | |
break; | |
case 'slide': | |
if(speed == null) { | |
speed = 5000; | |
} | |
var gallerySlider = new GallerySlider(navigability, speed, $el); | |
gallerySlider.init(); | |
break; | |
} | |
} // semi-colons recommended at the end of a block | |
return { | |
init: function() { | |
init(); | |
} | |
}; | |
}; | |
/** | |
* Gallery Slider | |
*/ | |
var GallerySlider = function(navigability, speed, $el) { | |
var self = this; | |
var $el = $($el); | |
var slides = $el.find('.slide').toArray(); | |
var indicators = $el.find('.slide-indicator').toArray(); | |
var next = $el.find('.next-slide-arrow'); | |
var prev = $el.find('.prev-slide-arrow'); | |
var gallerySlider = $el.data('gallery'); | |
var slider; | |
//create a callback handler | |
function indexChanged(index,elem) { | |
for(var i=0;i<indicators.length;i++){ | |
$(indicators[i]).removeClass('active'); | |
} | |
$(indicators[index]).addClass('active'); | |
} | |
$(indicators[0]).addClass('active'); | |
// Init | |
var init = function(){ | |
if(navigability) { | |
$(indicators).each(function(i) { | |
$(this).on('click',function(e){ | |
e.preventDefault(); | |
slider.slide(i); | |
}) | |
}); | |
$(next).on('click', function(e) { | |
slider.next(); | |
}); | |
$(prev).on('click', function(e) { | |
slider.prev(); | |
}); | |
} | |
slider = new Swipe(document.getElementById(gallerySlider), { | |
auto: speed, | |
callback: indexChanged | |
}); | |
} | |
return { | |
init: function() { | |
init(); | |
} | |
}; | |
}; | |
/** | |
* Fade Gallery | |
*/ | |
var FadeGallery = function(navigability, speed, offset, $el) { | |
var self = this; | |
var $el = $el; | |
var slides = $el.find('.slide').toArray(); | |
var indicators = $el.find('.slide-indicator').toArray(); | |
var next = $el.find('.next-slide-arrow'); | |
var prev = $el.find('.prev-slide-arrow'); | |
var index = 0; | |
// Resets all the indicators and slides | |
var reset = function() { | |
for(var j=0;j<indicators.length;j++){ | |
$(indicators[j]).removeClass('active'); | |
} | |
TweenMax.to(slides, speed, {autoAlpha:0, delay:0.1}); | |
}; | |
var setSlides = function() { | |
for(var i=0; i<slides.length;i++) { | |
TweenMax.set(slides[i], {autoAlpha:0}); | |
} | |
TweenMax.set(slides[0], {autoAlpha:1}); | |
$(indicators[0]).addClass('active'); | |
} | |
// Indicators | |
function indexChanged() { | |
for(var i=0;i<indicators.length;i++){ | |
$(indicators[i]).removeClass('active'); | |
} | |
if(index < slides.length) { | |
$(indicators[index]).addClass('active'); | |
index++; | |
}else { | |
index=0; | |
$(indicators[index]).addClass('active'); | |
index++; | |
} | |
} | |
var changeSlides = function(i) { | |
TweenMax.killTweensOf(slides); | |
// Reset Everything | |
for(var j=0;j<indicators.length;j++){ | |
$(indicators[j]).removeClass('active'); | |
} | |
TweenMax.to(slides, speed, {autoAlpha:0, delay:0.1}); | |
// Fade Selected Slide | |
$(indicators[i]).addClass('active'); | |
TweenMax.to(slides[i], speed, {autoAlpha:1, delay:0.1}, offset); | |
}; | |
// Fade next slide | |
var nextFade = function() { | |
TweenMax.killTweensOf(slides); | |
for(var i=0;i<indicators.length;i++){ | |
if($(indicators[i]).hasClass('active')) { | |
index = i+1; | |
} | |
} | |
// Reset Everything | |
reset(); | |
// Checks if next slide is at the end, if so than it goes to the beginning slide in the array | |
if(index < slides.length){ | |
$(indicators[index]).addClass('active'); | |
TweenMax.to(slides[index], 1, {autoAlpha:1, delay:0.1}, offset); | |
}else { | |
$(indicators[0]).addClass('active'); | |
TweenMax.to(slides[0], speed, {autoAlpha:1, delay:0.1}, offset); | |
} | |
}; | |
// Fade prev slide | |
var prevFade = function() { | |
TweenMax.killTweensOf(slides); | |
for(var i=0;i<indicators.length;i++){ | |
if($(indicators[i]).hasClass('active')) { | |
index = i-1; | |
} | |
} | |
// Reset Everything | |
reset() | |
// Checks if prev slide is at the beginning, if so than it goes to the last slide in the array | |
if(index >= 0){ | |
$(indicators[index]).addClass('active'); | |
TweenMax.to(slides[index], speed, {autoAlpha:1, delay:0.1}, offset); | |
}else { | |
$(indicators[slides.length-1]).addClass('active'); | |
TweenMax.to(slides[slides.length-1], speed, {autoAlpha:1, delay:0.1}, offset); | |
} | |
}; | |
// Fade In and Out Background | |
var timeline = function() { | |
// Set all backgrounds to 0 opacity | |
setSlides(); | |
slideAnim = new TimelineMax({delay:0.1, repeat:-1}); | |
// Fade in | |
slideAnim.staggerTo(slides, speed, {autoAlpha:1, delay:0.1, onStart:indexChanged}, offset); | |
// Fade out | |
slideAnim.staggerTo(slides, speed, {autoAlpha:0}, offset, offset); | |
return slideAnim; | |
}; | |
// Init | |
var init = function() { | |
if(navigability) { | |
$(indicators).each(function(i) { | |
$(this).on('click',function(e){ | |
e.preventDefault(); | |
changeSlides(i); | |
}) | |
}); | |
$(next).on('click', function(e){ | |
e.preventDefault(); | |
nextFade(); | |
}); | |
$(prev).on('click', function(e){ | |
e.preventDefault(); | |
prevFade(); | |
}); | |
} | |
timeline(); | |
}; | |
return { | |
init: function() { | |
init(); | |
}, | |
fadeNext: function() { | |
nextFade(); | |
}, | |
fadePrev: function() { | |
prevFade(); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment