diff --git a/facebox.css b/facebox.css index 0ba0089..97ebe3c 100644 --- a/facebox.css +++ b/facebox.css @@ -1,26 +1,25 @@ #facebox .b { - background:url(b.png); + background:url(/facebox/b.png); } #facebox .tl { - background:url(tl.png); + background:url(/facebox/tl.png); } #facebox .tr { - background:url(tr.png); + background:url(/facebox/tr.png); } #facebox .bl { - background:url(bl.png); + background:url(/facebox/bl.png); } #facebox .br { - background:url(br.png); + background:url(/facebox/br.png); } #facebox { position: absolute; - width: 100%; top: 0; left: 0; z-index: 100; @@ -32,14 +31,12 @@ } #facebox table { - margin: auto; border-collapse: collapse; - width:auto; } #facebox td { border-bottom: 0; - padding:0; + padding: 0; } #facebox .body { @@ -74,3 +71,25 @@ overflow: hidden; padding: 0; } + +#facebox_overlay { + position: fixed; + top: 0px; + left: 0px; + height:100%; + width:100%; +} + +.facebox_hide { + z-index:-100; +} + +.facebox_overlayBG { + background-color: #000; + z-index: 99; +} + +* html #facebox_overlay { /* ie6 hack */ + position: absolute; + height: expression(document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + 'px'); +} diff --git a/facebox.js b/facebox.js index dc5ffca..1804675 100644 --- a/facebox.js +++ b/facebox.js @@ -1,11 +1,9 @@ /* * Facebox (for jQuery) - * version: 1.2 (03/13/2008) + * version: 1.1 (03/13/2008) * @requires jQuery v1.2 or later * * Examples at http://famspam.com/facebox/ - * Code at http://github.com/defunkt/facebox - * List at http://groups.google.com/groups/facebox * * Licensed under the MIT: * http://www.opensource.org/licenses/mit-license.php @@ -32,167 +30,152 @@ * * jQuery.facebox('some html') * - * This will open a facebox with "some html" as the content. + * The above will open a facebox with "some html" as the content. * - * jQuery.facebox(function() { ajaxes }) + * jQuery.facebox(function($) { + * $.get('blah.html', function(data) { $.facebox(data) }) + * }) * - * This will show a loading screen before the passed function is called, - * allowing for a better ajax experience. + * The above will show a loading screen before the passed function is called, + * allowing for a better ajaxy experience. * - * Want to close the facebox? Trigger the 'close.facebox' document event: + * The facebox function can also display an ajax page or image: + * + * jQuery.facebox({ ajax: 'remote.html' }) + * jQuery.facebox({ image: 'dude.jpg' }) * - * jQuery(document).trigger('close.facebox') + * Want to close the facebox? Trigger the 'close.facebox' document event: * + * jQuery(document).trigger('close.facebox') */ (function($) { $.facebox = function(data, klass) { - $.facebox.init() $.facebox.loading() - $.isFunction(data) ? data.call() : $.facebox.reveal(data, klass) - } - $.facebox.settings = { - loading_image : '/facebox/loading.gif', - close_image : '/facebox/closelabel.gif', - window_hash : '#facebox', - image_types : [ 'png', 'jpg', 'jpeg', 'gif' ], - facebox_html : '\ - ' + if (data.ajax) fillFaceboxFromAjax(data.ajax) + else if (data.image) fillFaceboxFromImage(data.image) + else if ($.isFunction(data)) data.call($) + else $.facebox.reveal(data, klass) } - // Opening the facebox adds #facebox to the url. Clicking 'back' closes the facebox - // but keeps you on the page you were on. - function back_button_observer() { - if (window.location.hash != $.facebox.settings.window_hash) $(document).trigger('close.facebox') - } - - function observe_back_button() { - $.facebox.settings.old_hash = window.location.hash || '#' - window.location.hash = $.facebox.settings.window_hash - $.facebox.settings.back_button_observer = setInterval(back_button_observer, 200) - } + /* + * Public, $.facebox methods + */ - function stop_observing_back_button() { - if (window.location.hash != $.facebox.settings.old_hash) window.location.hash = $.facebox.settings.old_hash - $.facebox.settings.old_hash = null - clearInterval($.facebox.settings.back_button_observer) - } + $.extend($.facebox, { + settings: { + opacity : 0, + overlay : true, + loadingImage : '/facebox/loading.gif', + closeImage : '/facebox/closelabel.gif', + imageTypes : [ 'png', 'jpg', 'jpeg', 'gif' ], + faceboxHtml : '\ + ' + }, - $.facebox.loading = function() { - if ($('#facebox .loading').length == 1) return true - - observe_back_button() + loading: function() { + init() + if ($('#facebox .loading').length == 1) return true + showOverlay() - $('#facebox .content').empty() - $('#facebox .body').children().hide().end(). - append('
') + $('#facebox .content').empty() + $('#facebox .body').children().hide().end(). + append('
') - var pageScroll = $.facebox.getPageScroll() - $('#facebox').css({ - top: pageScroll[1] + ($.facebox.getPageHeight() / 10), - left: pageScroll[0] - }).show() + $('#facebox').css({ + top: getPageScroll()[1] + (getPageHeight() / 10), + left: 0 + }).show() - $(document).bind('keydown.facebox', function(e) { - if (e.keyCode == 27) $.facebox.close() - }) - $('#facebox .close').click($.facebox.close) - } + $('#facebox').css('left', $(window).width() / 2 - ($('#facebox table').width() / 2)) - $.facebox.reveal = function(data, klass) { - if (klass) $('#facebox .content').addClass(klass) - $('#facebox .content').append(data) - $('#facebox .loading').remove() - $('#facebox .body').children().fadeIn('normal') - } + $(document).bind('keydown.facebox', function(e) { + if (e.keyCode == 27) $.facebox.close() + return true + }) + }, - $.facebox.close = function() { - $(document).trigger('close.facebox') - return false - } + reveal: function(data, klass) { + if (klass) $('#facebox .content').addClass(klass) + $('#facebox .content').append(data) + $('#facebox .loading').remove() + $('#facebox .body').children().fadeIn('normal') + }, - $(document).bind('close.facebox', function() { - stop_observing_back_button() - $(document).unbind('keydown.facebox') - $('#facebox').fadeOut(function() { - $('#facebox .content').empty().removeClass().addClass('content') - }) + close: function() { + $(document).trigger('close.facebox') + return false + } }) - $.fn.facebox = function(settings) { - $.facebox.init(settings) + /* + * Public, $.fn methods + */ - var image_types = $.facebox.settings.image_types.join('|') - image_types = new RegExp('\.' + image_types + '$', 'i') + $.fn.facebox = function(settings) { + init(settings) - function click_handler() { + function clickHandler() { $.facebox.loading(true) - // support for rel="facebox[.inline_popup]" syntax, to add a class - var klass = this.rel.match(/facebox\[\.(\w+)\]/) + // support for rel="facebox.inline_popup" syntax, to add a class + // also supports deprecated "facebox[.inline_popup]" syntax + var klass = this.rel.match(/facebox\[?\.(\w+)\]?/) if (klass) klass = klass[1] - // div - if (this.href.match(/#/)) { - var url = window.location.href.split('#')[0] - var target = this.href.replace(url,'') - $.facebox.reveal($(target).clone().show(), klass) - - // image - } else if (this.href.match(image_types)) { - var image = new Image() - image.onload = function() { - $.facebox.reveal('
', klass) - } - image.src = this.href - - // ajax - } else { - $.get(this.href, function(data) { $.facebox.reveal(data, klass) }) - } - + fillFaceboxFromHref(this.href, klass) return false } - return this.click(click_handler) + return this.click(clickHandler) } - $.facebox.init = function(settings) { + /* + * Private methods + */ + + // called one time to setup facebox on this page + function init(settings) { if ($.facebox.settings.inited) return true else $.facebox.settings.inited = true + makeCompatible() + + var imageTypes = $.facebox.settings.imageTypes.join('|') + $.facebox.settings.imageTypesRegexp = new RegExp('\.' + imageTypes + '$', 'i') + if (settings) $.extend($.facebox.settings, settings) - $('body').append($.facebox.settings.facebox_html) + $('body').append($.facebox.settings.faceboxHtml) var preload = [ new Image(), new Image() ] - preload[0].src = $.facebox.settings.close_image - preload[1].src = $.facebox.settings.loading_image + preload[0].src = $.facebox.settings.closeImage + preload[1].src = $.facebox.settings.loadingImage $('#facebox').find('.b:first, .bl, .br, .tl, .tr').each(function() { preload.push(new Image()) @@ -200,11 +183,11 @@ }) $('#facebox .close').click($.facebox.close) - $('#facebox .close_image').attr('src', $.facebox.settings.close_image) + $('#facebox .close_image').attr('src', $.facebox.settings.closeImage) } - + // getPageScroll() by quirksmode.com - $.facebox.getPageScroll = function() { + function getPageScroll() { var xScroll, yScroll; if (self.pageYOffset) { yScroll = self.pageYOffset; @@ -219,8 +202,8 @@ return new Array(xScroll,yScroll) } - // adapter from getPageSize() by quirksmode.com - $.facebox.getPageHeight = function() { + // Adapted from getPageSize() by quirksmode.com + function getPageHeight() { var windowHeight if (self.innerHeight) { // all except Explorer windowHeight = self.innerHeight; @@ -231,4 +214,90 @@ } return windowHeight } + + // Backwards compatibility + function makeCompatible() { + var $s = $.facebox.settings + + $s.loadingImage = $s.loading_image || $s.loadingImage + $s.closeImage = $s.close_image || $s.closeImage + $s.imageTypes = $s.image_types || $s.imageTypes + $s.faceboxHtml = $s.facebox_html || $s.faceboxHtml + } + + // Figures out what you want to display and displays it + // formats are: + // div: #id + // image: blah.extension + // ajax: anything else + function fillFaceboxFromHref(href, klass) { + // div + if (href.match(/#/)) { + var url = window.location.href.split('#')[0] + var target = href.replace(url,'') + $.facebox.reveal($(target).clone().show(), klass) + + // image + } else if (href.match($.facebox.settings.imageTypesRegexp)) { + fillFaceboxFromImage(href, klass) + // ajax + } else { + fillFaceboxFromAjax(href, klass) + } + } + + function fillFaceboxFromImage(href, klass) { + var image = new Image() + image.onload = function() { + $.facebox.reveal('
', klass) + } + image.src = href + } + + function fillFaceboxFromAjax(href, klass) { + $.get(href, function(data) { $.facebox.reveal(data, klass) }) + } + + function skipOverlay() { + return $.facebox.settings.overlay == false || $.facebox.settings.opacity === null + } + + function showOverlay() { + if (skipOverlay()) return + + if ($('facebox_overlay').length == 0) + $("body").append('
') + + $('#facebox_overlay').hide().addClass("facebox_overlayBG") + .css('opacity', $.facebox.settings.opacity) + .click(function() { $(document).trigger('close.facebox') }) + .fadeIn(200) + return false + } + + function hideOverlay() { + if (skipOverlay()) return + + $('#facebox_overlay').fadeOut(200, function(){ + $("#facebox_overlay").removeClass("facebox_overlayBG") + $("#facebox_overlay").addClass("facebox_hide") + $("#facebox_overlay").remove() + }) + + return false + } + + /* + * Bindings + */ + + $(document).bind('close.facebox', function() { + $(document).unbind('keydown.facebox') + $('#facebox').fadeOut(function() { + $('#facebox .content').removeClass().addClass('content') + hideOverlay() + $('#facebox .loading').remove() + }) + }) + })(jQuery);