Created
December 1, 2010 10:26
-
-
Save joecritch/723300 to your computer and use it in GitHub Desktop.
Just a super simple jQuery closure-style gallery that's not a plugin...
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
var productGallery = $('.productGallery'); | |
if(productGallery.length) { | |
(function() { | |
// Define some dynamics and cache selectors | |
var largeContainer = productGallery.find('.caraLarge').eq(0), | |
thumbContainer = productGallery.find('.caraThumbs').eq(0), | |
currentIndex = 0, | |
prev = productGallery.find('.prev').eq(0), | |
next = productGallery.find('.next').eq(0) | |
disabled = false, | |
currentClass = 'current', | |
thumbs = thumbContainer.find('a'), | |
largeImages = largeContainer.find('li'), | |
largeWidth = largeImages.eq(0).width(); | |
// Click the thumbnails to change the main image... | |
thumbs.click(function(e) { | |
// Event selectors | |
var thumb = $(this); | |
var index = thumb.parent('li').index(); | |
e.preventDefault(); | |
// Can it be clicked? | |
if(disabled === true) return; | |
else disabled = true; | |
// Current class | |
thumbs.parent('li').removeClass(currentClass); | |
thumb.parent('li').addClass(currentClass); | |
// Animate the container | |
largeContainer.animate({ | |
left: ((index * largeWidth) * -1) | |
}, { | |
duration: 600, | |
easing: 'easeInOutCirc', | |
complete: function() { | |
disabled = false; | |
currentIndex = index; | |
} | |
}); | |
}).eq(0).click(); | |
// Previous and next wrappers. | |
var adjacentClick = function(e, difference) { | |
var self = $(this); | |
if(!self.hasClass(disabledClass)) { | |
thumbs.eq(difference).trigger('click'); | |
} | |
}; | |
prev.click(function(e) { e.preventDefault(); adjacentClick(e, (currentIndex - 1)); }); | |
next.click(function(e) { e.preventDefault(); adjacentClick(e, (currentIndex + 1)); }); | |
})(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment