Skip to content

Instantly share code, notes, and snippets.

@neilalbrock
Created March 22, 2011 14:32
Show Gist options
  • Save neilalbrock/881293 to your computer and use it in GitHub Desktop.
Save neilalbrock/881293 to your computer and use it in GitHub Desktop.
JQuery gallery plugin written for http://www.atomised.coop
(function($,undefined){
$.fn.atomGallery = function(options) {
//Plugin settings
var settings = $.extend(
{},
{
slideOutTime:1200,
slideOutCSS:{"opacity":"toggle","top":"+=30em"},
slideOutEasing:"easeOutSine",
slideInTime:1000,
slideInCSS:{"opacity":"toggle"},
slideInEasing:"easeOutSine",
autoTrans:true,
autoDelay:5000
},
options
);
//Cache container
var $atomGallery = $(this);
//Cache text and images blocks
var $allText = $("div.gallery-text",$atomGallery);
var $allImages = $("ul.gallery-images",$atomGallery);
//Hide all list items except the first
$("div:gt(0)",$allText).hide();
$("li:gt(0)",$allImages).hide();
//Bind click functions
$("ul.gallery-nav li",$atomGallery).click(function(e) {
var navDir = $(this).attr('id');
runTransition(navDir);
});
//Set timed transition interval
if (settings.autoTrans) {
setInterval(
function(){
runTransition();
},
(settings.autoDelay + (settings.slideOutTime + settings.slideInTime))
);
}
function runTransition(navDir) {
//Current image and text
var $thisImage = $("li:visible",$allImages);
var $thisText = $("div:visible",$allText);
var $nextImage = null;
var $nextText = null;
//Handle the correct image and text to show
if (navDir === 'prev') {
if ($thisImage.prev().length) {
$nextImage = $thisImage.prev();
} else {
$nextImage = $thisImage.nextAll(":last");
}
if ($thisText.prev().length) {
$nextText = $thisText.prev();
} else {
$nextText = $thisText.nextAll(":last");
}
} else {
if ($thisImage.next().length) {
$nextImage = $thisImage.next();
} else {
$nextImage = $thisImage.prevAll(":last");
}
if ($thisText.next().length) {
$nextText = $thisText.next();
} else {
$nextText = $thisText.prevAll(":last");
}
}
//Transition image and text
elemTransition($thisImage,$nextImage);
elemTransition($thisText,$nextText);
}
function elemTransition($thisElem,$nextElem) {
if (!$thisElem.queue("fx").length) {
$thisElem.animate(
settings.slideOutCSS,
settings.slideOutTime,
settings.slideOutEasing,
function() {
$thisElem.css("top","0em");
$nextElem.animate(
settings.slideInCSS,
settings.slideInTime,
settings.slideInEasing
);
}
);
}
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment