Created
May 16, 2010 19:02
-
-
Save deepakjois/403093 to your computer and use it in GitHub Desktop.
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
function($){ | |
$.fn.gallery = function(settings) { | |
var config = { | |
activeelm: 'active', | |
nav: 'nav', | |
navnext: 'nxt' | |
}; | |
if (settings) $.extend(config, settings); | |
var $gallery = $(this); | |
// Hide the image elems | |
$gallery.children().addClass("hide"); | |
// Preload Images | |
preloadImages(); | |
// Setup Click Handler | |
$("." + config.nav).children("a").click(function(){ | |
$(this).hasClass(config.navnext) ? changeimage("next") : changeimage("prev"); | |
return false; | |
}); | |
// Setup Keyboard Handler | |
$(document).bind('keydown',function(event){ | |
if (event.keyCode == '37') { | |
changeimage("prev"); | |
} else if (event.keyCode == '39') { | |
changeimage("next"); | |
} | |
}); | |
// Finally call changeImage with empty arguments to do initial setting up | |
changeImage(); | |
function changeimage(action) { | |
var currentelm = $gallery.children("." + config.activeelm); | |
if(action === 'next') { | |
var nextelm = $(currentelm).is(':last-child') ? $(currentelm).siblings(':first-child') : $(currentelm).next(); | |
} else if(action === 'prev') { | |
var nextelm = $(currentelm).is(':first-child') ? $(currentelm).siblings(':last-child') : $(currentelm).prev(); | |
} | |
// TODO handle case of empty args | |
// Move history updating to a separate function, like updateLocationBar, and you will also need | |
// another function called readLocationBar | |
window.location = window.location.href.replace(/\#.*/,'') + '#' + $(nextelm).children("img").attr("src"); | |
$(currentelm).removeClass(config.activeelm).addClass("hide"); | |
$(nextelm).removeClass("hide").addClass(config.activeelm); | |
}; | |
function preloadImages(){ | |
// TODO show some sort of loader graphic | |
$gallery.find("img").each(function(){ | |
var imagesrc = $(this).attr("src"); | |
if ($.browser.msie) { | |
new Image().src = imagesrc; | |
return; | |
}; | |
o = document.createElement('object'); | |
o.data = imagesrc; | |
o.width = o.height = 0; | |
document.body.appendChild(o); | |
}); | |
}; | |
} | |
}(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment