/* * $ lightbox_me * by: buck wilson * version : 2.4 * * licensed under the apache license, version 2.0 (the "license"); * you may not use this file except in compliance with the license. * you may obtain a copy of the license at * * http://www.apache.org/licenses/license-2.0 * * unless required by applicable law or agreed to in writing, software * distributed under the license is distributed on an "as is" basis, * without warranties or conditions of any kind, either express or implied. * see the license for the specific language governing permissions and * limitations under the license. */ (function($) { $.fn.lightbox_me = function(options) { return this.each(function() { var opts = $.extend({}, $.fn.lightbox_me.defaults, options), $overlay = $(), $self = $(this), $iframe = $(''); if (opts.showoverlay) { //check if there's an existing overlay, if so, make subequent ones clear var $currentoverlays = $(".js_lb_overlay:visible"); if ($currentoverlays.length > 0){ $overlay = $('
'); } else { $overlay = $(''); } } /*---------------------------------------------------- dom building ---------------------------------------------------- */ $('body').append($self.hide()).append($overlay); /*---------------------------------------------------- overlay css stuffs ---------------------------------------------------- */ // set css of the overlay if (opts.showoverlay) { setoverlayheight(); // pulled this into a function because it is called on window resize. $overlay.css({ position: 'absolute', width: '100%', top: 0, left: 0, right: 0, bottom: 0, zindex: (opts.zindex + 2), display: 'none' }); if (!$overlay.hasclass('lb_overlay_clear')){ $overlay.css(opts.overlaycss); } } /*---------------------------------------------------- animate it in. ---------------------------------------------------- */ // if (opts.showoverlay) { $overlay.fadein(opts.overlayspeed, function() { setselfposition(); $self[opts.appeareffect](opts.lightboxspeed, function() { setoverlayheight(); setselfposition(); opts.onload()}); }); } else { setselfposition(); $self[opts.appeareffect](opts.lightboxspeed, function() { opts.onload()}); } /*---------------------------------------------------- hide parent if parent specified (parentlightbox should be jquery reference to any parent lightbox) ---------------------------------------------------- */ if (opts.parentlightbox) { opts.parentlightbox.fadeout(200); } /*---------------------------------------------------- bind events ---------------------------------------------------- */ $(window).resize(setoverlayheight) .resize(setselfposition) .scroll(setselfposition); $(window).bind('keyup.lightbox_me', observekeypress); if (opts.closeclick) { $overlay.click(function(e) { closelightbox(); e.preventdefault; }); } $self.delegate(opts.closeselector, "click", function(e) { closelightbox(); e.preventdefault(); }); $self.bind('close', closelightbox); $self.bind('reposition', setselfposition); /*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */ /*---------------------------------------------------- private functions ---------------------------------------------------- */ /* remove or hide all elements */ function closelightbox() { var s = $self[0].style; if (opts.destroyonclose) { $self.add($overlay).remove(); } else { $self.add($overlay).hide(); } //show the hidden parent lightbox if (opts.parentlightbox) { opts.parentlightbox.fadein(200); } if (opts.preventscroll) { $('body').css('overflow', ''); } $iframe.remove(); // clean up events. $self.undelegate(opts.closeselector, "click"); $self.unbind('close', closelightbox); $self.unbind('repositon', setselfposition); $(window).unbind('resize', setoverlayheight); $(window).unbind('resize', setselfposition); $(window).unbind('scroll', setselfposition); $(window).unbind('keyup.lightbox_me'); opts.onclose(); } /* function to bind to the window to observe the escape/enter key press */ function observekeypress(e) { if((e.keycode == 27 || (e.dom_vk_escape == 27 && e.which==0)) && opts.closeesc) closelightbox(); } /* set the height of the overlay : if the document height is taller than the window, then set the overlay height to the document height. : otherwise, just set overlay height: 100% */ function setoverlayheight() { if ($(window).height() < $(document).height()) { $overlay.css({height: $(document).height() + 'px'}); $iframe.css({height: $(document).height() + 'px'}); } else { $overlay.css({height: '100%'}); } } /* set the position of the modal'd window ($self) : if $self is taller than the window, then make it absolutely positioned : otherwise fixed */ function setselfposition() { var s = $self[0].style; // reset css so width is re-calculated for margin-left css $self.css({left: '50%', marginleft: ($self.outerwidth() / 2) * -1, zindex: (opts.zindex + 3) }); /* we have to get a little fancy when dealing with height, because lightbox_me is just so fancy. */ // if the height of $self is bigger than the window and self isn't already position absolute if (($self.height() + 80 >= $(window).height()) && ($self.css('position') != 'absolute')) { // we are going to make it positioned where the user can see it, but they can still scroll // so the top offset is based on the user's scroll position. var topoffset = $(document).scrolltop() + 40; $self.css({position: 'absolute', top: topoffset + 'px', margintop: 0}) } else if ($self.height()+ 80 < $(window).height()) { //if the height is less than the window height, then we're gonna make this thing position: fixed. if (opts.centered) { $self.css({ position: 'fixed', top: '50%', margintop: ($self.outerheight() / 2) * -1}) } else { $self.css({ position: 'fixed'}).css(opts.modalcss); } if (opts.preventscroll) { $('body').css('overflow', 'hidden'); } } } }); }; $.fn.lightbox_me.defaults = { // animation appeareffect: "fadein", appearease: "", overlayspeed: 250, lightboxspeed: 300, // close closeselector: ".close", closeclick: true, closeesc: true, // behavior destroyonclose: false, showoverlay: true, parentlightbox: false, preventscroll: false, // callbacks onload: function() {}, onclose: function() {}, // style classprefix: 'lb', zindex: 999, centered: false, modalcss: {top: '40px'}, overlaycss: {background: 'black', opacity: .3} } })(jquery);