3 * version: 1.2 (05/05/2008)
4 * @requires jQuery v1.2 or later
6 * Examples at http://famspam.com/facebox/
8 * Licensed under the MIT:
9 * http://www.opensource.org/licenses/mit-license.php
11 * Copyright 2007, 2008 Chris Wanstrath [ chris@ozmm.org ]
15 * jQuery(document).ready(function() {
16 * jQuery('a[rel*=facebox]').facebox()
19 * <a href="#terms" rel="facebox">Terms</a>
20 * Loads the #terms div in the box
22 * <a href="terms.html" rel="facebox">Terms</a>
23 * Loads the terms.html page in the box
25 * <a href="terms.png" rel="facebox">Terms</a>
26 * Loads the terms.png image in the box
29 * You can also use it programmatically:
31 * jQuery.facebox('some html')
33 * The above will open a facebox with "some html" as the facebox_content.
35 * jQuery.facebox(function($) {
36 * $.get('blah.html', function(data) { $.facebox(data) })
39 * The above will show a loading screen before the passed function is called,
40 * allowing for a better ajaxy experience.
42 * The facebox function can also display an ajax page or image:
44 * jQuery.facebox({ ajax: 'remote.html' })
45 * jQuery.facebox({ image: 'dude.jpg' })
47 * Want to close the facebox? Trigger the 'close.facebox' document event:
49 * jQuery(document).trigger('close.facebox')
51 * Facebox also has a bunch of other hooks:
54 * beforeReveal.facebox
55 * reveal.facebox (aliased as 'afterReveal.facebox')
58 * Simply bind a function to any of these hooks:
60 * $(document).bind('reveal.facebox', function() { ...stuff to do after the facebox and facebox_contents are revealed... })
64 $.facebox = function(data, klass) {
67 if (data.ajax) fillFaceboxFromAjax(data.ajax)
68 else if (data.image) fillFaceboxFromImage(data.image)
69 else if (data.div) fillFaceboxFromHref(data.div)
70 else if ($.isFunction(data)) data.call($)
71 else $.facebox.reveal(data, klass)
75 * Public, $.facebox methods
82 loadingImage : 'images/facebox/loading.gif',
83 closeImage : 'images/facebox/closelabel.gif',
84 imageTypes : [ 'png', 'jpg', 'jpeg', 'gif' ],
86 <div id="facebox" style="display:none;"> \
91 <td class="tl"/><td class="b"/><td class="tr"/> \
96 <div class="facebox_content"> \
98 <div class="facebox_footer"> \
99 <a href="#" class="close"> \
100 <img src="images/facebox/closelabel.gif" title="close" class="close_image" /> \
107 <td class="bl"/><td class="b"/><td class="br"/> \
115 loading: function() {
117 if ($('#facebox .loading').length == 1) return true
120 $('#facebox .facebox_content').empty()
121 $('#facebox .body').children().hide().end().
122 append('<div class="loading"><img src="'+$.facebox.settings.loadingImage+'"/></div>')
125 top: getPageScroll()[1] + (getPageHeight() / 10),
129 $(document).bind('keydown.facebox', function(e) {
130 if (e.keyCode == 27) $.facebox.close()
133 $(document).trigger('loading.facebox')
136 reveal: function(data, klass) {
137 $(document).trigger('beforeReveal.facebox')
138 if (klass) $('#facebox .facebox_content').addClass(klass)
139 $('#facebox .facebox_content').append(data)
140 $('#facebox .loading').remove()
141 $('#facebox .body').children().fadeIn('normal')
142 $('#facebox').css('left', $(window).width() / 2 - ($('#facebox table').width() / 2))
143 $(document).trigger('reveal.facebox').trigger('afterReveal.facebox')
147 $(document).trigger('close.facebox')
153 * Public, $.fn methods
156 $.fn.facebox = function(settings) {
159 function clickHandler() {
160 $.facebox.loading(true)
162 // support for rel="facebox.inline_popup" syntax, to add a class
163 // also supports deprecated "facebox[.inline_popup]" syntax
164 var klass = this.rel.match(/facebox\[?\.(\w+)\]?/)
165 if (klass) klass = klass[1]
167 fillFaceboxFromHref(this.href, klass)
171 return this.click(clickHandler)
178 // called one time to setup facebox on this page
179 function init(settings) {
180 if ($.facebox.settings.inited) return true
181 else $.facebox.settings.inited = true
183 $(document).trigger('init.facebox')
186 var imageTypes = $.facebox.settings.imageTypes.join('|')
187 $.facebox.settings.imageTypesRegexp = new RegExp('\.' + imageTypes + '$', 'i')
189 if (settings) $.extend($.facebox.settings, settings)
190 $('body').append($.facebox.settings.faceboxHtml)
192 var preload = [ new Image(), new Image() ]
193 preload[0].src = $.facebox.settings.closeImage
194 preload[1].src = $.facebox.settings.loadingImage
196 $('#facebox').find('.b:first, .bl, .br, .tl, .tr').each(function() {
197 preload.push(new Image())
198 preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
201 $('#facebox .close').click($.facebox.close)
202 $('#facebox .close_image').attr('src', $.facebox.settings.closeImage)
205 // getPageScroll() by quirksmode.com
206 function getPageScroll() {
207 var xScroll, yScroll;
208 if (self.pageYOffset) {
209 yScroll = self.pageYOffset;
210 xScroll = self.pageXOffset;
211 } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
212 yScroll = document.documentElement.scrollTop;
213 xScroll = document.documentElement.scrollLeft;
214 } else if (document.body) {// all other Explorers
215 yScroll = document.body.scrollTop;
216 xScroll = document.body.scrollLeft;
218 return new Array(xScroll,yScroll)
221 // Adapted from getPageSize() by quirksmode.com
222 function getPageHeight() {
224 if (self.innerHeight) { // all except Explorer
225 windowHeight = self.innerHeight;
226 } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
227 windowHeight = document.documentElement.clientHeight;
228 } else if (document.body) { // other Explorers
229 windowHeight = document.body.clientHeight;
234 // Backwards compatibility
235 function makeCompatible() {
236 var $s = $.facebox.settings
238 $s.loadingImage = $s.loading_image || $s.loadingImage
239 $s.closeImage = $s.close_image || $s.closeImage
240 $s.imageTypes = $s.image_types || $s.imageTypes
241 $s.faceboxHtml = $s.facebox_html || $s.faceboxHtml
244 // Figures out what you want to display and displays it
247 // image: blah.extension
248 // ajax: anything else
249 function fillFaceboxFromHref(href, klass) {
251 if (href.match(/#/)) {
252 var url = window.location.href.split('#')[0]
253 var target = href.replace(url,'')
254 $.facebox.reveal($(target).clone().show(), klass)
257 } else if (href.match($.facebox.settings.imageTypesRegexp)) {
258 fillFaceboxFromImage(href, klass)
261 fillFaceboxFromAjax(href, klass)
265 function fillFaceboxFromImage(href, klass) {
266 var image = new Image()
267 image.onload = function() {
268 $.facebox.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass)
273 function fillFaceboxFromAjax(href, klass) {
274 $.get(href, function(data) { $.facebox.reveal(data, klass) })
277 function skipOverlay() {
278 return $.facebox.settings.overlay == false || $.facebox.settings.opacity === null
281 function showOverlay() {
282 if (skipOverlay()) return
284 if ($('facebox_overlay').length == 0)
285 $("body").append('<div id="facebox_overlay" class="facebox_hide"></div>')
287 $('#facebox_overlay').hide().addClass("facebox_overlayBG")
288 .css('opacity', $.facebox.settings.opacity)
289 .click(function() { $(document).trigger('close.facebox') })
294 function hideOverlay() {
295 if (skipOverlay()) return
297 $('#facebox_overlay').fadeOut(200, function(){
298 $("#facebox_overlay").removeClass("facebox_overlayBG")
299 $("#facebox_overlay").addClass("facebox_hide")
300 $("#facebox_overlay").remove()
310 $(document).bind('close.facebox', function() {
311 $(document).unbind('keydown.facebox')
312 $('#facebox').fadeOut(function() {
313 $('#facebox .facebox_content').removeClass().addClass('facebox_content')
315 $('#facebox .loading').remove()