| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
(function($) { |
| 8 |
|
| 9 |
|
| 10 |
var win = $(window), options, images, activeImage = -1, activeURL, prevImage, nextImage, compatibleOverlay, middle, centerWidth, centerHeight, |
| 11 |
ie6 = !window.XMLHttpRequest, hiddenElements = [], documentElement = document.documentElement, |
| 12 |
|
| 13 |
|
| 14 |
preload = {}, preloadPrev = new Image(), preloadNext = new Image(), |
| 15 |
|
| 16 |
|
| 17 |
overlay, center, image, sizer, prevLink, nextLink, bottomContainer, bottom, caption, number; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
$(function() { |
| 24 |
|
| 25 |
$("body").append( |
| 26 |
$([ |
| 27 |
overlay = $('<div id="lbOverlay" />')[0], |
| 28 |
center = $('<div id="lbCenter" />')[0], |
| 29 |
bottomContainer = $('<div id="lbBottomContainer" />')[0] |
| 30 |
]).css("display", "none") |
| 31 |
); |
| 32 |
|
| 33 |
image = $('<div id="lbImage" />').appendTo(center).append( |
| 34 |
sizer = $('<div style="position: relative;" />').append([ |
| 35 |
prevLink = $('<a id="lbPrevLink" href="#" />').click(previous)[0], |
| 36 |
nextLink = $('<a id="lbNextLink" href="#" />').click(next)[0] |
| 37 |
])[0] |
| 38 |
)[0]; |
| 39 |
|
| 40 |
bottom = $('<div id="lbBottom" />').appendTo(bottomContainer).append([ |
| 41 |
$('<a id="lbCloseLink" href="#" />').add(overlay).click(close)[0], |
| 42 |
caption = $('<div id="lbCaption" />')[0], |
| 43 |
number = $('<div id="lbNumber" />')[0], |
| 44 |
$('<div style="clear: both;" />')[0] |
| 45 |
])[0]; |
| 46 |
}); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
$.slimbox = function(_images, startImage, _options) { |
| 55 |
options = $.extend({ |
| 56 |
loop: false, |
| 57 |
overlayOpacity: 0.8, |
| 58 |
overlayFadeDuration: 400, |
| 59 |
resizeDuration: 400, |
| 60 |
resizeEasing: "swing", |
| 61 |
initialWidth: 250, |
| 62 |
initialHeight: 250, |
| 63 |
imageFadeDuration: 400, |
| 64 |
captionAnimationDuration: 400, |
| 65 |
counterText: "Image {x} of {y}", |
| 66 |
closeKeys: [27, 88, 67], |
| 67 |
previousKeys: [37, 80], |
| 68 |
nextKeys: [39, 78] |
| 69 |
}, _options); |
| 70 |
|
| 71 |
|
| 72 |
if (typeof _images == "string") { |
| 73 |
_images = [[_images, startImage]]; |
| 74 |
startImage = 0; |
| 75 |
} |
| 76 |
|
| 77 |
middle = win.scrollTop() + (win.height() / 2); |
| 78 |
centerWidth = options.initialWidth; |
| 79 |
centerHeight = options.initialHeight; |
| 80 |
$(center).css({top: Math.max(0, middle - (centerHeight / 2)), width: centerWidth, height: centerHeight, marginLeft: -centerWidth/2}).show(); |
| 81 |
compatibleOverlay = ie6 || (overlay.currentStyle && (overlay.currentStyle.position != "fixed")); |
| 82 |
if (compatibleOverlay) overlay.style.position = "absolute"; |
| 83 |
$(overlay).css("opacity", options.overlayOpacity).fadeIn(options.overlayFadeDuration); |
| 84 |
position(); |
| 85 |
setup(1); |
| 86 |
|
| 87 |
images = _images; |
| 88 |
options.loop = options.loop && (images.length > 1); |
| 89 |
return changeImage(startImage); |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
$.fn.slimbox = function(_options, linkMapper, linksFilter) { |
| 101 |
linkMapper = linkMapper || function(el) { |
| 102 |
return [el.href, el.title]; |
| 103 |
}; |
| 104 |
|
| 105 |
linksFilter = linksFilter || function() { |
| 106 |
return true; |
| 107 |
}; |
| 108 |
|
| 109 |
var links = this; |
| 110 |
|
| 111 |
return links.unbind("click").click(function() { |
| 112 |
|
| 113 |
var link = this, startIndex = 0, filteredLinks, i = 0, length; |
| 114 |
filteredLinks = $.grep(links, function(el, i) { |
| 115 |
return linksFilter.call(link, el, i); |
| 116 |
}); |
| 117 |
|
| 118 |
|
| 119 |
for (length = filteredLinks.length; i < length; ++i) { |
| 120 |
if (filteredLinks[i] == link) startIndex = i; |
| 121 |
filteredLinks[i] = linkMapper(filteredLinks[i], i); |
| 122 |
} |
| 123 |
|
| 124 |
return $.slimbox(filteredLinks, startIndex, _options); |
| 125 |
}); |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
function position() { |
| 134 |
var l = win.scrollLeft(), w = win.width(); |
| 135 |
$([center, bottomContainer]).css("left", l + (w / 2)); |
| 136 |
if (compatibleOverlay) $(overlay).css({left: l, top: win.scrollTop(), width: w, height: win.height()}); |
| 137 |
} |
| 138 |
|
| 139 |
function setup(open) { |
| 140 |
if (open) { |
| 141 |
$("object").add(ie6 ? "select" : "embed").each(function(index, el) { |
| 142 |
hiddenElements[index] = [el, el.style.visibility]; |
| 143 |
el.style.visibility = "hidden"; |
| 144 |
}); |
| 145 |
} else { |
| 146 |
$.each(hiddenElements, function(index, el) { |
| 147 |
el[0].style.visibility = el[1]; |
| 148 |
}); |
| 149 |
hiddenElements = []; |
| 150 |
} |
| 151 |
var fn = open ? "bind" : "unbind"; |
| 152 |
win[fn]("scroll resize", position); |
| 153 |
$(document)[fn]("keydown", keyDown); |
| 154 |
} |
| 155 |
|
| 156 |
function keyDown(event) { |
| 157 |
var code = event.keyCode, fn = $.inArray; |
| 158 |
|
| 159 |
return (fn(code, options.closeKeys) >= 0) ? close() |
| 160 |
: (fn(code, options.nextKeys) >= 0) ? next() |
| 161 |
: (fn(code, options.previousKeys) >= 0) ? previous() |
| 162 |
: false; |
| 163 |
} |
| 164 |
|
| 165 |
function previous() { |
| 166 |
return changeImage(prevImage); |
| 167 |
} |
| 168 |
|
| 169 |
function next() { |
| 170 |
return changeImage(nextImage); |
| 171 |
} |
| 172 |
|
| 173 |
function changeImage(imageIndex) { |
| 174 |
if (imageIndex >= 0) { |
| 175 |
activeImage = imageIndex; |
| 176 |
activeURL = images[activeImage][0]; |
| 177 |
prevImage = (activeImage || (options.loop ? images.length : 0)) - 1; |
| 178 |
nextImage = ((activeImage + 1) % images.length) || (options.loop ? 0 : -1); |
| 179 |
|
| 180 |
stop(); |
| 181 |
center.className = "lbLoading"; |
| 182 |
|
| 183 |
preload = new Image(); |
| 184 |
preload.onload = animateBox; |
| 185 |
preload.src = activeURL; |
| 186 |
} |
| 187 |
|
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
function animateBox() { |
| 192 |
center.className = ""; |
| 193 |
$(image).css({backgroundImage: "url(" + activeURL + ")", visibility: "hidden", display: ""}); |
| 194 |
$(sizer).width(preload.width); |
| 195 |
$([sizer, prevLink, nextLink]).height(preload.height); |
| 196 |
|
| 197 |
$(caption).html(images[activeImage][1] || ""); |
| 198 |
$(number).html((((images.length > 1) && options.counterText) || "").replace(/{x}/, activeImage + 1).replace(/{y}/, images.length)); |
| 199 |
|
| 200 |
if (prevImage >= 0) preloadPrev.src = images[prevImage][0]; |
| 201 |
if (nextImage >= 0) preloadNext.src = images[nextImage][0]; |
| 202 |
|
| 203 |
centerWidth = image.offsetWidth; |
| 204 |
centerHeight = image.offsetHeight; |
| 205 |
var top = Math.max(0, middle - (centerHeight / 2)); |
| 206 |
if (center.offsetHeight != centerHeight) { |
| 207 |
$(center).animate({height: centerHeight, top: top}, options.resizeDuration, options.resizeEasing); |
| 208 |
} |
| 209 |
if (center.offsetWidth != centerWidth) { |
| 210 |
$(center).animate({width: centerWidth, marginLeft: -centerWidth/2}, options.resizeDuration, options.resizeEasing); |
| 211 |
} |
| 212 |
$(center).queue(function() { |
| 213 |
$(bottomContainer).css({width: centerWidth, top: top + centerHeight, marginLeft: -centerWidth/2, visibility: "hidden", display: ""}); |
| 214 |
$(image).css({display: "none", visibility: "", opacity: ""}).fadeIn(options.imageFadeDuration, animateCaption); |
| 215 |
}); |
| 216 |
} |
| 217 |
|
| 218 |
function animateCaption() { |
| 219 |
if (prevImage >= 0) $(prevLink).show(); |
| 220 |
if (nextImage >= 0) $(nextLink).show(); |
| 221 |
$(bottom).css("marginTop", -bottom.offsetHeight).animate({marginTop: 0}, options.captionAnimationDuration); |
| 222 |
bottomContainer.style.visibility = ""; |
| 223 |
} |
| 224 |
|
| 225 |
function stop() { |
| 226 |
preload.onload = null; |
| 227 |
preload.src = preloadPrev.src = preloadNext.src = activeURL; |
| 228 |
$([center, image, bottom]).stop(true); |
| 229 |
$([prevLink, nextLink, image, bottomContainer]).hide(); |
| 230 |
} |
| 231 |
|
| 232 |
function close() { |
| 233 |
if (activeImage >= 0) { |
| 234 |
stop(); |
| 235 |
activeImage = prevImage = nextImage = -1; |
| 236 |
$(center).hide(); |
| 237 |
$(overlay).stop().fadeOut(options.overlayFadeDuration, setup); |
| 238 |
} |
| 239 |
|
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
})(jQuery); |