/* jquery.poptrox.js v2.5.1 | (c) n33 | n33.co | mit licensed */ (function($) { // disables selection $.fn.poptrox_disableselection = function() { return $(this).css('user-select', 'none').css('-khtml-user-select', 'none').css('-moz-user-select', 'none').css('-o-user-select', 'none').css('-webkit-user-select', 'none'); } // poptrox prototype method $.fn.poptrox = function(options) { // handle no elements. if (this.length == 0) return $(this); // handle multiple elements. if (this.length > 1) { for (var i=0; i < this.length; i++) $(this[i]).poptrox(options); return $(this); } // settings var settings = $.extend({ preload: false, // if true, preload fullsize images in the background basezindex: 1000, // base z-index fadespeed: 300, // global fade speed overlaycolor: '#000000', // overlay color overlayopacity: 0.6, // overlay opacity overlayclass: 'poptrox-overlay', // overlay class windowmargin: 50, // window margin size (in pixels; only comes into play when an image is larger than the viewport) windowheightpad: 0, // window height pad selector: 'a', // anchor tag selector caption: null, // caption settings (see docs) parent: 'body', // parent selector (ie. where all the popup/overlay stuff gets added). popupspeed: 300, // popup (resize) speed popupwidth: 200, // popup width popupheight: 100, // popup height popupisfixed: false, // if true, popup won't resize to fit images usebodyoverflow: false, // if true, the body tag is set to overflow: hidden when the popup is visible usepopupeasyclose: true, // if true, popup can be closed by clicking on it anywhere usepopupforceclose: false, // if true, popup can be closed even while content is loading usepopuploader: true, // if true, show the popup loader usepopupcloser: true, // if true, show the popup closer button/link usepopupcaption: false, // if true, show the popup image caption usepopupnav: false, // if true, show (and use) popup navigation usepopupdefaultstyling: true, // if true, default popup styling will be applied (background color, text color, etc) popupbackgroundcolor: '#ffffff', // (default style) popup background color (when usepopupstyling = true) popuptextcolor: '#000000', // (default style) popup text color (when usepopupstyling = true) popuploadertextsize: '2em', // (default style) popup loader text size popupcloserbackgroundcolor: '#000000', // (default style) popup closer background color (when usepopupstyling = true) popupclosertextcolor: '#ffffff', // (default style) popup closer text color (when usepopupstyling = true) popupclosertextsize: '20px', // (default style) popup closer text size popuppadding: 10, // (default style) popup padding (when usepopupstyling = true) popupcaptionheight: 60, // (default style) popup height of caption area popupcaptiontextsize: null, // (default style) popup caption text size popupblankcaptiontext: '(untitled)', // applied to images that don't have captions (when captions are enabled) popupclosertext: '×', // popup closer text popuploadertext: '••••', // popup loader text popupclass: 'poptrox-popup', // popup class popupselector: null, // (advanced) popup selector (use this if you want to replace the built-in popup) popuploaderselector: '.loader', // (advanced) popup loader selector popupcloserselector: '.closer', // (advanced) popup closer selector popupcaptionselector: '.caption', // (advanced) popup caption selector popupnavpreviousselector: '.nav-previous', // (advanced) popup nav previous selector popupnavnextselector: '.nav-next', // (advanced) popup nav next selector onpopupclose: null, // called when popup closes onpopupopen: null // called when popup opens }, options); // variables var $this = $(this), $body = $('body'), $overlay = $('
'), $window = $(window); var windowwidth, windowheight, queue = [], navpos = 0, islocked = false, cache = new array(); function updatewh() { windowwidth = $(window).width(); windowheight = $(window).height() + settings.windowheightpad; var dw = math.abs($popup.width() - $popup.outerwidth()), dh = math.abs($popup.height() - $popup.outerheight()); var nw = $x.width(), nh = $x.height(); var maxw = windowwidth - (settings.windowmargin * 2) - dw, maxh = windowheight - (settings.windowmargin * 2) - dh; $popup .css('min-width', settings.popupwidth) .css('min-height', settings.popupheight); $pic.children() .css('max-width', maxw) .css('max-height', maxh); } // disable unused features if (!settings.usepopuploader) settings.popuploaderselector = null; if (!settings.usepopupcloser) settings.popupcloserselector = null; if (!settings.usepopupcaption) settings.popupcaptionselector = null; if (!settings.usepopupnav) { settings.popupnavpreviousselector = null; settings.popupnavnextselector = null; } // get popup var $popup; if (settings.popupselector) $popup = $(settings.popupselector); else $popup = $('
' + (settings.popuploaderselector ? '
' + settings.popuploadertext + '
' : '') + '
' + (settings.popupcaptionselector ? '
' : '') + (settings.popupcloserselector ? '' + settings.popupclosertext + '' : '') + (settings.popupnavpreviousselector ? '' : '') + (settings.popupnavnextselector ? '' : '') + '
'); // get popup components var $pic = $popup.find('.pic'), $x = $(), $loader = $popup.find(settings.popuploaderselector), $caption = $popup.find(settings.popupcaptionselector), $closer = $popup.find(settings.popupcloserselector), $nav_next = $popup.find(settings.popupnavnextselector), $nav_previous = $popup.find(settings.popupnavpreviousselector), $nav = $nav_next.add($nav_previous); // apply default styling? if (settings.usepopupdefaultstyling) { $popup .css('background', settings.popupbackgroundcolor) .css('color', settings.popuptextcolor) .css('padding', settings.popuppadding + 'px'); if ($caption.length > 0) { $popup .css('padding-bottom', settings.popupcaptionheight + 'px'); $caption .css('position', 'absolute') .css('left', '0') .css('bottom', '0') .css('width', '100%') .css('text-align', 'center') .css('height', settings.popupcaptionheight + 'px') .css('line-height', settings.popupcaptionheight + 'px'); if (settings.popupcaptiontextsize) $caption.css('font-size', popupcaptiontextsize); } if ($closer.length > 0) $closer .html(settings.popupclosertext) .css('font-size', settings.popupclosertextsize) .css('background', settings.popupcloserbackgroundcolor) .css('color', settings.popupclosertextcolor) .css('display', 'block') .css('width', '40px') .css('height', '40px') .css('line-height', '40px') .css('text-align', 'center') .css('position', 'absolute') .css('text-decoration', 'none') .css('outline', '0') .css('top', '0') .css('right', '-40px'); if ($loader.length > 0) { $loader .html('') .css('position', 'relative') .css('font-size', settings.popuploadertextsize) .on('startspinning', function(e) { var x = $('
' + settings.popuploadertext + '
'); x .css('height', math.floor(settings.popupheight / 2) + 'px') .css('overflow', 'hidden') .css('line-height', math.floor(settings.popupheight / 2) + 'px') .css('text-align', 'center') .css('margin-top', math.floor(($popup.height() - x.height() + ($caption.length > 0 ? $caption.height() : 0)) / 2)) .css('color', (settings.popuptextcolor ? settings.popuptextcolor : '')) .on('xfin', function() { x.fadeto(300, 0.5, function() { x.trigger('xfout'); }); }) .on('xfout', function() { x.fadeto(300, 0.05, function() { x.trigger('xfin'); }); }) .trigger('xfin'); $loader.append(x); }) .on('stopspinning', function(e) { var x = $loader.find('div'); x.remove(); }); } if ($nav.length == 2) { $nav .css('font-size', '75px') .css('text-align', 'center') .css('color', '#fff') .css('text-shadow', 'none') .css('height', '100%') .css('position', 'absolute') .css('top', '0') .css('opacity', '0.35') .css('cursor', 'pointer') .css('box-shadow', 'inset 0px 0px 10px 0px rgba(0,0,0,0)') .poptrox_disableselection(); var wn, wp; if (settings.usepopupeasyclose) { wn = '100px'; wp = '100px'; } else { wn = '75%'; wp = '25%'; } $nav_next .css('right', '0') .css('width', wn) .html('
>
'); $nav_previous .css('left', '0') .css('width', wp) .html('
<
'); } } // main $window .on('resize orientationchange', function() { updatewh(); }); $caption .on('update', function(e, s) { if (!s || s.length == 0) s = settings.popupblankcaptiontext; $caption.html(s); }); $closer .css('cursor', 'pointer') .on('click', function(e) { e.preventdefault(); e.stoppropagation(); $popup.trigger('poptrox_close'); return true; }); $nav_next .on('click', function(e) { e.stoppropagation(); e.preventdefault(); $popup.trigger('poptrox_next'); }); $nav_previous .on('click', function(e) { e.stoppropagation(); e.preventdefault(); $popup.trigger('poptrox_previous'); }); $overlay .css('position', 'fixed') .css('left', 0) .css('top', 0) .css('z-index', settings.basezindex) .css('width', '100%') .css('height', '100%') .css('text-align', 'center') .css('cursor', 'pointer') .appendto(settings.parent) .prepend('
') .append('
') .hide() .on('touchmove', function(e) { return false; }) .on('click', function(e) { e.preventdefault(); e.stoppropagation(); $popup.trigger('poptrox_close'); }); $popup .css('display', 'inline-block') .css('vertical-align', 'middle') .css('position', 'relative') .css('z-index', 1) .css('cursor', 'auto') .appendto($overlay) .hide() .on('poptrox_next', function() { var x = navpos + 1; if (x >= queue.length) x = 0; $popup.trigger('poptrox_switch', [x]); }) .on('poptrox_previous', function() { var x = navpos - 1; if (x < 0) x = queue.length - 1; $popup.trigger('poptrox_switch', [x]); }) .on('poptrox_reset', function() { updatewh(); $popup .data('width', settings.popupwidth) .data('height', settings.popupheight); $loader.hide().trigger('stopspinning'); $caption.hide(); $closer.hide(); $nav.hide(); $pic.hide(); $x .attr('src', '') .detach(); }) .on('poptrox_open', function(e, index) { if (islocked) return true; islocked = true; if (settings.usebodyoverflow) $body.css('overflow', 'hidden'); if (settings.onpopupopen) (settings.onpopupopen)(); $overlay .fadeto(settings.fadespeed, 1.0, function() { $popup.trigger('poptrox_switch', [index, true]); }); }) .on('poptrox_switch', function(e, index, ignorelock) { var x, img; if (!ignorelock && islocked) return true; islocked = true; $popup .css('width', $popup.data('width')) .css('height', $popup.data('height')); // cleanup from previous $caption.hide(); if ($x.attr('src')) $x.attr('src', ''); $x.detach(); // activate new object x = queue[index]; $x = x.object; $x.off('load'); $pic .css('text-indent', '-9999px') .show() .append($x); if (x.type == 'ajax') $.get(x.src, function(data) { $x.html(data); $x.trigger('load'); }); else $x.attr('src', x.src); if (x.type != 'image') { var xwidth, xheight; xwidth = x.width; xheight = x.height; if (xwidth.slice(-1) == '%') xwidth = (parseint(xwidth.substring(0, xwidth.length - 1)) / 100.00) * $window.width(); if (xheight.slice(-1) == '%') xheight = (parseint(xheight.substring(0, xheight.length - 1)) / 100.00) * $window.height(); $x .css('position', 'relative') .css('outline', '0') .css('z-index', settings.basezindex + 100) .width(xwidth) .height(xheight); } // initialize $loader.trigger('startspinning').fadein(300); $popup.show(); if (settings.popupisfixed) { $popup .width(settings.popupwidth) .height(settings.popupheight); $x.load(function() { $x.off('load'); $loader.hide().trigger('stopspinning'); $caption.trigger('update', [x.captiontext]).fadein(settings.fadespeed); $closer.fadein(settings.fadespeed); $pic.css('text-indent', 0).hide().fadein(settings.fadespeed, function() { islocked = false; }); navpos = index; $nav.fadein(settings.fadespeed); }); } else { $x.load(function() { updatewh(); $x.off('load'); $loader.hide().trigger('stopspinning'); var nw = $x.width(), nh = $x.height(), f = function() { $caption.trigger('update', [x.captiontext]).fadein(settings.fadespeed); $closer.fadein(settings.fadespeed); $pic.css('text-indent', 0).hide().fadein(settings.fadespeed, function() { islocked = false; }); navpos = index; $nav.fadein(settings.fadespeed); $popup .data('width', nw) .data('height', nh) .css('width', 'auto') .css('height', 'auto'); }; if (nw == $popup.data('width') && nh == $popup.data('height')) (f)(); else $popup.animate({ width: nw, height: nh }, settings.popupspeed, 'swing', f); }); } if (x.type != 'image') $x.trigger('load'); }) .on('poptrox_close', function() { if (islocked && !settings.usepopupforceclose) return true; islocked = true; $popup .hide() .trigger('poptrox_reset'); if (settings.onpopupclose) (settings.onpopupclose)(); $overlay .fadeout(settings.fadespeed, function() { if (settings.usebodyoverflow) $body.css('overflow', 'auto'); islocked = false; }); }) .trigger('poptrox_reset'); // easy close. if (settings.usepopupeasyclose) { $caption.on('click', 'a', function(e) { e.stoppropagation(); }); $popup .css('cursor', 'pointer') .on('click', function(e) { e.stoppropagation(); e.preventdefault(); $popup.trigger('poptrox_close'); }); } else $popup .on('click', function(e) { e.stoppropagation(); }); $window .keydown(function(e) { if ($popup.is(':visible')) { switch (e.keycode) { case 37: case 32: if (settings.usepopupnav) { $popup.trigger('poptrox_previous'); return false; } break; case 39: if (settings.usepopupnav) { $popup.trigger('poptrox_next'); return false; } break; case 27: $popup.trigger('poptrox_close'); return false; break; } } }); $this.find(settings.selector).each(function(index) { var x, tmp, a = $(this), i = a.find('img'), data = a.data('poptrox'); // ignore? skip. if (data == 'ignore') return; // no href? bail. if (!a.attr('href')) return; x = { src: a.attr('href'), captiontext: i.attr('title'), width: null, height: null, type: null, object: null, options: null }; // determine caption. // no caption setting? use default (title attribute of image). if (!settings.caption) c = i.attr('title'); // function? else if (typeof(settings.caption) == 'function') c = (settings.caption)(a); // selector? else if ('selector' in settings.caption) { var s; s = a.find(settings.caption.selector); if ('attribute' in settings.caption) c = s.attr(settings.caption.attribute); else { c = s.html(); if (settings.caption.remove === true) s.remove(); } } x.captiontext = c; // if a data attribute exists, use it if (data) { var b = data.split(','); // type. if (0 in b) x.type = b[0]; // size. if (1 in b) { tmp = b[1].match(/([0-9%]+)x([0-9%]+)/); if (tmp && tmp.length == 3) { x.width = tmp[1]; x.height = tmp[2]; } } // options. if (2 in b) x.options = b[2]; } // no type? attempt to guess it based on the href's domain if (!x.type) { tmp = x.src.match(/\/\/([a-z0-9\.]+)\/.*/); if (!tmp || tmp.length < 2) tmp = [false]; switch (tmp[1]) { case 'api.soundcloud.com': x.type = 'soundcloud'; break; case 'youtu.be': x.type = 'youtube'; break; case 'vimeo.com': x.type = 'vimeo'; break; case 'wistia.net': x.type = 'wistia'; break; case 'bcove.me': x.type = 'bcove'; break; default: x.type = 'image'; break; } } // create object (based on type) tmp = x.src.match(/\/\/[a-z0-9\.]+\/(.*)/); switch (x.type) { case 'iframe': x.object = $(''); x.object .on('click', function(e) { e.stoppropagation(); }) .css('cursor', 'auto'); if (!x.width || !x.height) { x.width = "600"; x.height = "400"; } break; case 'ajax': x.object = $('
'); x.object .on('click', function(e) { e.stoppropagation(); }) .css('cursor', 'auto') .css('overflow', 'auto'); if (!x.width || !x.height) { x.width = "600"; x.height = "400"; } break; case 'soundcloud': x.object = $(''); x.src = '//w.soundcloud.com/player/?url=' + escape(x.src) + (x.options ? '&' + x.options : ''); x.width = '600'; x.height = "166"; break; case 'youtube': x.object = $(''); x.src = '//www.youtube.com/embed/' + tmp[1] + (x.options ? '?' + x.options : ''); if (!x.width || !x.height) { x.width = "800"; x.height = "480"; } break; case 'vimeo': x.object = $(''); x.src = '//player.vimeo.com/video/' + tmp[1] + (x.options ? '?' + x.options : ''); if (!x.width || !x.height) { x.width = "800"; x.height = "480"; } break; case 'wistia': x.object = $(''); x.src = '//fast.wistia.net/' + tmp[1] + (x.options ? '?' + x.options : ''); if (!x.width || !x.height) { x.width = "800"; x.height = "480"; } break; case 'bcove': x.object = $(''); x.src = '//bcove.me/' + tmp[1] + (x.options ? '?' + x.options : ''); if (!x.width || !x.height) { x.width = "640"; x.height = "360"; } break; default: x.object = $(''); if (settings.preload) { var tmp = document.createelement('img'); tmp.src = x.src; cache.push(tmp); } x.width = a.attr('width'); x.height = a.attr('height'); break; } // fix src if protocol is 'file'. if (window.location.protocol == 'file:' && x.src.match(/^\/\//)) x.src = 'http:' + x.src; queue.push(x); i.attr('title', ''); a .attr('href', '') .css('outline', 0) .on('click', function(e) { e.preventdefault(); e.stoppropagation(); $popup.trigger('poptrox_open', [index]); }); }); return $(this); }; })(jquery);