imagebox.js 15.2 KB
Newer Older
christian's avatar
christian committed
1 2 3 4 5 6 7
/*
    jQuery ImageBox  - Crazy image gallery with included Slimbox
    Version: 0.1.4
    (c) 2014 Christian Weiss <http://monkeybiz.at>
    MIT-style license.
*/
(function ($, window, document, Math, undefined) {
christian's avatar
christian committed
8
  'use strict';
christian's avatar
christian committed
9
  var ver = '0.1.4';
christian's avatar
christian committed
10
  $('head').append(' <link rel="stylesheet" href="./css/slimbox.css" type="text/css" media="all">');
christian's avatar
christian committed
11 12

  //Feature tests
christian's avatar
christian committed
13
  if (!$.cssHooks) {
christian's avatar
christian committed
14
    throw (new Error('jQuery 1.4.3+ is needed for this plugin to work'));
christian's avatar
christian committed
15
  }
christian's avatar
christian committed
16 17 18 19 20

  function doStyle(prop) {
    var vP, sP, cP = prop.charAt(0).toUpperCase() + prop.slice(1),
            prefix = ['Moz', 'Webkit', 'O', 'ms'], div = document.createElement('div');

christian's avatar
christian committed
21
    if (prop in div.style) {
christian's avatar
christian committed
22
      sP = prop;
christian's avatar
christian committed
23
    }
christian's avatar
christian committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    else {
      for (var i = 0; i < prefix.length; i++) {
        vP = prefix[i] + cP;
        if (vP in div.style) {
          sP = vP;
          break;
        }
      }
    }
    div = null;
    $.support[prop] = sP;
    return sP;
  }

  var transform = doStyle("transform");

  if (transform && transform !== "transform") {
    $.cssHooks.transform = {
      get: function (elem, computed, extra) {
        return $.css(elem, transform);
      },
      set: function (elem, value) {
        elem.style[transform] = value;
      }
    };
  }
  var backfaceVisibility = doStyle("backfaceVisibility");

  if (backfaceVisibility && backfaceVisibility !== "backfaceVisibility") {
    $.cssHooks.backfaceVisibility = {
      get: function (elem, computed, extra) {
        return $.css(elem, backfaceVisibility);
      },
      set: function (elem, value) {
        elem.style[backfaceVisibility] = value;
      }
    };
  }

  $.fn.imagebox = function (options) {

    return this.each(function () {
      options = options || {};
      var $cont = $(this);
      var $el = $cont.children();
      var elArr = $el.get();

christian's avatar
christian committed
71
      if ($cont.css('position') == 'static') {
christian's avatar
christian committed
72
        $cont.css('position', 'relative');
christian's avatar
christian committed
73
      }
christian's avatar
christian committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

      //extend config
      var opts = $.extend({}, $.fn.imagebox.defaults, options || {});

      //create imageBox wrapper
      var $imageBox = $('<div />');
      var $imageBoxContainer = $('<div id="imageBox-container" />');

      $imageBox.css({
        position: 'absolute',
        border: opts.border,
        width: opts.width,
        height: opts.height,
        boxShadow: opts.boxShadow
      });

      var maxRowLen = Math.floor(parseInt($cont.width() / $imageBox.width()));
      var maxColLen = Math.ceil(elArr.length / maxRowLen);

      var n = 1, nLeft, nTop, rPM, mRot, nRpm = 0, bC = opts.boxChaos, mR = opts.maxRotate, nX = 0, nY = 0, elId, s, iBgallery = opts.iBgallery;

      $.each($el, function (i) {
        rPM = Math.cos(Math.PI * Math.round(Math.random()));
        nRpm = Math.floor(Math.random() * (bC)) * rPM;
        mRot = Math.floor(Math.random() * (mR)) * rPM;
        nLeft = parseInt(opts.width) * nX;
        nTop = parseInt(opts.height) * nY;

christian's avatar
christian committed
102 103 104 105 106 107 108
        if (nX < maxRowLen - n) {
          nX++;
        }
        else {
          nX = 0;
          nY += 1;
        }
christian's avatar
christian committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

        $(elArr[i]).css('display', 'block');
        elId = 'imageBox-' + (i);
        $(this).wrap($imageBox.attr('id', elId));
        $(elArr[i]).attr('data-rot', mRot);

        $(this).parent().css({
          left: nLeft + nRpm,
          top: nTop + nRpm,
          zIndex: 90 + nRpm,
          transform: 'scale(1.0,1.0) rotate(' + mRot + 'deg)'
        });

        $(this).find('img').css({
          backfaceVisibility: 'hidden',
          transformOriginX: '0',
          transformOriginY: '0',
          perspective: 1000,
          transform: 'translateZ(0) scale(1.0, 1.0)',
          position: 'relative',
          maxWidth: '100%',
          display: 'block'
        });

        $(this).css({
          width: opts.width,
          height: opts.height
        });

christian's avatar
christian committed
138
        if (opts.slimbox) {
christian's avatar
christian committed
139
          $(this).attr('rel', 'imageBox-' + iBgallery + '');
christian's avatar
christian committed
140
        }
christian's avatar
christian committed
141 142 143 144 145
      });

      $imageBoxContainer.css({
        paddingTop: parseInt($imageBox.height() / 2),
        paddingBottom: parseInt($imageBox.height() / 2)
christian's avatar
christian committed
146
      });
christian's avatar
christian committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

      $cont.css({
        width: parseInt($imageBox.width() * maxRowLen),
        height: parseInt($imageBox.height() * maxColLen)

      }).wrap($imageBoxContainer);

      $el.hover(function () {
        s = opts.scale;
        $(this).parent().css({
          boxShadow: opts.boxShadowHover,
          zIndex: parseInt($(this)[0].parentNode.style.zIndex) + 1000,
          transition: 'all, .2s',
          transform: 'scale(' + s[0] + ',' + s[1] + ') rotate(0deg)'
        });
      }, function () {
        $(this).parent().css({
          boxShadow: opts.boxShadow,
          zIndex: parseInt($(this)[0].parentNode.style.zIndex) - 1000,
          transition: 'all, .1s',
          transform: 'scale(1,1) rotate(' + $(this).attr('data-rot') + 'deg)'
        });
      });

      if (jQuery().slimbox) {
christian's avatar
christian committed
172 173
        console.info('KAY');
        if (opts.slimbox) {
christian's avatar
christian committed
174
          $.fn.imagebox.slimbox(opts.slimOptions);
christian's avatar
christian committed
175 176 177 178
        }
      }
      else {
        console.error('TROUBLE!!!');
christian's avatar
christian committed
179 180
      }
    }); 
christian's avatar
christian committed
181
  };
christian's avatar
christian committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

  $.fn.imagebox.ver = function () { return ver; };

  $.fn.imagebox.defaults = {
    width: "50px",
    height: "75px",
    boxShadow: "0 0 10px rgba(0,0,0,.5)",
    boxShadowHover: "0 0 10px rgba(0,0,0,1)",
    border: 0,
    maxRotate: 10,
    scale: [1.1, 1.1],
    boxChaos: 20,
    slimbox: false,
    slimOptions: {
      loop: false,
      overlayOpacity: 0.8,
      overlayFadeDuration: 400,
      resizeDuration: 400,
      resizeEasing: "swing",
      initialWidth: 250,
      initialHeight: 250,
      imageFadeDuration: 400,
      captionAnimationDuration: 400,
      counterText: "Image {x} of {y}",
      closeKeys: [27, 88, 67],
      previousKeys: [37, 80],
      nextKeys: [39, 78]
    },
    iBgallery: ''
christian's avatar
christian committed
211
  };
christian's avatar
christian committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

  $.fn.imagebox.slimbox = function (obj) {
    var obj = obj || {};
    $('a[rel^="imageBox"]').slimbox({
      loop: obj.loop,
      overlayOpacity: obj.overlayOpacity,
      overlayFadeDuration: obj.overlayFadeDuration,
      resizeDuration: obj.resizeDuration,
      resizeEasing: obj.resizeEasing,
      initialWidth: obj.initialWidth,
      initialHeight: obj.initialHeight,
      imageFadeDuration: obj.imageFadeDuration,
      captionAnimationDuration: obj.captionAnimationDuration,
      counterText: obj.counterText,
      closeKeys: obj.closeKeys,
      previousKeys: obj.previousKeys,
      nextKeys: obj.nextKeys
    }, null, function (el) {
      return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    });
  }

} (jQuery, window, document, Math));

/*!
	Slimbox v2.05 - The ultimate lightweight Lightbox clone for jQuery
	(c) 2007-2013 Christophe Beyls <http://www.digitalia.be>
	MIT-style license.
*/
(function($) {
	var win = $(window), options, images, activeImage = -1, activeURL, prevImage, nextImage, compatibleOverlay, middle, centerWidth, centerHeight,
		ie6 = !window.XMLHttpRequest, hiddenElements = [], documentElement = document.documentElement,
  	preload = {}, preloadPrev = new Image(), preloadNext = new Image(),
  	overlay, center, image, sizer, prevLink, nextLink, bottomContainer, bottom, caption, number;

	$(function() {
		$("body").append(
			$([
				overlay = $('<div id="lbOverlay" />').click(close)[0],
				center = $('<div id="lbCenter" />')[0],
				bottomContainer = $('<div id="lbBottomContainer" />')[0]
			]).css("display", "none")
		);

		image = $('<div id="lbImage" />').appendTo(center).append(
			sizer = $('<div style="position: relative;" />').append([
				prevLink = $('<a id="lbPrevLink" href="#" />').click(previous)[0],
				nextLink = $('<a id="lbNextLink" href="#" />').click(next)[0]
			])[0]
		)[0];

		bottom = $('<div id="lbBottom" />').appendTo(bottomContainer).append([
			$('<a id="lbCloseLink" href="#" />').click(close)[0],
			caption = $('<div id="lbCaption" />')[0],
			number = $('<div id="lbNumber" />')[0],
			$('<div style="clear: both;" />')[0]
		])[0];
	});

	// Open Slimbox with the specified parameters
	$.slimbox = function(_images, startImage, _options) {
		options = $.extend({
			loop: false,				// Allows to navigate between first and last images
			overlayOpacity: 0.8,			// 1 is opaque, 0 is completely transparent (change the color in the CSS file)
			overlayFadeDuration: 400,		// Duration of the overlay fade-in and fade-out animations (in milliseconds)
			resizeDuration: 400,			// Duration of each of the box resize animations (in milliseconds)
			resizeEasing: "swing",			// "swing" is jQuery's default easing
			initialWidth: 250,			// Initial width of the box (in pixels)
			initialHeight: 250,			// Initial height of the box (in pixels)
			imageFadeDuration: 400,			// Duration of the image fade-in animation (in milliseconds)
			captionAnimationDuration: 400,		// Duration of the caption animation (in milliseconds)
			counterText: "Image {x} of {y}",	// Translate or change as you wish, or set it to false to disable counter text for image groups
			closeKeys: [27, 88, 67],		// Array of keycodes to close Slimbox, default: Esc (27), 'x' (88), 'c' (67)
			previousKeys: [37, 80],			// Array of keycodes to navigate to the previous image, default: Left arrow (37), 'p' (80)
			nextKeys: [39, 78]			// Array of keycodes to navigate to the next image, default: Right arrow (39), 'n' (78)
		}, _options);

		// The function is called for a single image, with URL and Title as first two arguments
		if (typeof _images == "string") {
			_images = [[_images, startImage]];
			startImage = 0;
		}

		middle = win.scrollTop() + (win.height() / 2);
		centerWidth = options.initialWidth;
		centerHeight = options.initialHeight;
		$(center).css({top: Math.max(0, middle - (centerHeight / 2)), width: centerWidth, height: centerHeight, marginLeft: -centerWidth/2}).show();
		compatibleOverlay = ie6 || (overlay.currentStyle && (overlay.currentStyle.position != "fixed"));
		if (compatibleOverlay) overlay.style.position = "absolute";
		$(overlay).css("opacity", options.overlayOpacity).fadeIn(options.overlayFadeDuration);
		position();
		setup(1);

		images = _images;
		options.loop = options.loop && (images.length > 1);
		return changeImage(startImage);
	};

	/*
		options:	Optional options object, see jQuery.slimbox()
		linkMapper:	Optional function taking a link DOM element and an index as arguments and returning an array containing 2 elements:
				the image URL and the image caption (may contain HTML)
		linksFilter:	Optional function taking a link DOM element and an index as arguments and returning true if the element is part of
				the image collection that will be shown on click, false if not. "this" refers to the element that was clicked.
				This function must always return true when the DOM element argument is "this".
	*/
	$.fn.slimbox = function(_options, linkMapper, linksFilter) {
		linkMapper = linkMapper || function(el) {
			return [el.href, el.title];
		};

		linksFilter = linksFilter || function() {
			return true;
		};

		var links = this;

		return links.unbind("click").click(function() {
			// Build the list of images that will be displayed
			var link = this, startIndex = 0, filteredLinks, i = 0, length;
			filteredLinks = $.grep(links, function(el, i) {
				return linksFilter.call(link, el, i);
			});

			// We cannot use jQuery.map() because it flattens the returned array
			for (length = filteredLinks.length; i < length; ++i) {
				if (filteredLinks[i] == link) startIndex = i;
				filteredLinks[i] = linkMapper(filteredLinks[i], i);
			}

			return $.slimbox(filteredLinks, startIndex, _options);
		});
	};

	function position() {
		var l = win.scrollLeft(), w = win.width();
		$([center, bottomContainer]).css("left", l + (w / 2));
		if (compatibleOverlay) $(overlay).css({left: l, top: win.scrollTop(), width: w, height: win.height()});
	}

	function setup(open) {
		if (open) {
			$("object").add(ie6 ? "select" : "embed").each(function(index, el) {
				hiddenElements[index] = [el, el.style.visibility];
				el.style.visibility = "hidden";
			});
		} else {
			$.each(hiddenElements, function(index, el) {
				el[0].style.visibility = el[1];
			});
			hiddenElements = [];
		}
		var fn = open ? "bind" : "unbind";
		win[fn]("scroll resize", position);
		$(document)[fn]("keydown", keyDown);
	}

	function keyDown(event) {
		var code = event.which, fn = $.inArray;
		// Prevent default keyboard action (like navigating inside the page)
		return (fn(code, options.closeKeys) >= 0) ? close()
			: (fn(code, options.nextKeys) >= 0) ? next()
			: (fn(code, options.previousKeys) >= 0) ? previous()
			: null;
	}

	function previous() {
		return changeImage(prevImage);
	}

	function next() {
		return changeImage(nextImage);
	}

	function changeImage(imageIndex) {
		if (imageIndex >= 0) {
			activeImage = imageIndex;
			activeURL = images[activeImage][0];
			prevImage = (activeImage || (options.loop ? images.length : 0)) - 1;
			nextImage = ((activeImage + 1) % images.length) || (options.loop ? 0 : -1);

			stop();
			center.className = "lbLoading";

			preload = new Image();
			preload.onload = animateBox;
			preload.src = activeURL;
		}

		return false;
	}

	function animateBox() {
		center.className = "";
		$(image).css({backgroundImage: "url(" + activeURL + ")", visibility: "hidden", display: ""});
		$(sizer).width(preload.width);
		$([sizer, prevLink, nextLink]).height(preload.height);

		$(caption).html(images[activeImage][1] || "");
		$(number).html((((images.length > 1) && options.counterText) || "").replace(/{x}/, activeImage + 1).replace(/{y}/, images.length));

		if (prevImage >= 0) preloadPrev.src = images[prevImage][0];
		if (nextImage >= 0) preloadNext.src = images[nextImage][0];

		centerWidth = image.offsetWidth;
		centerHeight = image.offsetHeight;
		var top = Math.max(0, middle - (centerHeight / 2));
		if (center.offsetHeight != centerHeight) {
			$(center).animate({height: centerHeight, top: top}, options.resizeDuration, options.resizeEasing);
		}
		if (center.offsetWidth != centerWidth) {
			$(center).animate({width: centerWidth, marginLeft: -centerWidth/2}, options.resizeDuration, options.resizeEasing);
		}
		$(center).queue(function() {
			$(bottomContainer).css({width: centerWidth, top: top + centerHeight, marginLeft: -centerWidth/2, visibility: "hidden", display: ""});
			$(image).css({display: "none", visibility: "", opacity: ""}).fadeIn(options.imageFadeDuration, animateCaption);
		});
	}

	function animateCaption() {
		if (prevImage >= 0) $(prevLink).show();
		if (nextImage >= 0) $(nextLink).show();
		$(bottom).css("marginTop", -bottom.offsetHeight).animate({marginTop: 0}, options.captionAnimationDuration);
		bottomContainer.style.visibility = "";
	}

	function stop() {
		preload.onload = null;
		preload.src = preloadPrev.src = preloadNext.src = activeURL;
		$([center, image, bottom]).stop(true);
		$([prevLink, nextLink, image, bottomContainer]).hide();
	}

	function close() {
		if (activeImage >= 0) {
			stop();
			activeImage = prevImage = nextImage = -1;
			$(center).hide();
			$(overlay).stop().fadeOut(options.overlayFadeDuration, setup);
		}

		return false;
	}

})(jQuery);