A smoother Thickbox (with less code)
November 6th, 2006
- Category:
- CSS,
- JavaScript,
- Web Development,
- jQuery
Here’s an instruction on how to make the beautiful Thickbox even better. These things came to my mind while I was implementing Thickbox for Plazes. The result is a much smoother Thickbox with much less JavaScript code.
What bothered me was that when scrolling or resizing the browser window the Thickbox modal window gets moved to stay in the center, but that happens not smooth at all and with a little delay (after the resize/scroll events get fired that is).
So I changed the Thickbox CSS to use fixed positioning, starting with the modal window:
-
Change CSS to this:
html, body { height: 100%; } #TB_window { position: fixed; top: 50%; left: 50%; ... } -
Remove the scroll and resize event handler:
$(window).scroll(TB_position); $(window).resize(TB_position); -
Change the function
TB_positionto this:function TB_position() { $("#TB_window").css({marginLeft: '-' + parseInt(TB_WIDTH / 2) + 'px', width: TB_WIDTH + 'px'}); if ( !(jQuery.browser.msie && typeof XMLHttpRequest == 'function') ) { // take away IE6 $("#TB_window").css({marginTop: '-' + parseInt(TB_HEIGHT / 2) + 'px'}); } TB_overlaySize(); }It now pushes the modal window half of its width/height to the left/top from the middle so that it is exactly centered.
-
Fix our special friend IE 6 to emulate a fixed positioning:
* html #TB_window { position: absolute; margin-top: expression(0 - parseInt(this.offsetHeight / 2) + (TBWindowMargin = document.documentElement && document.documentElement.scrollTop || document.body.scrollTop) + 'px'); }(Based on http://www.howtocreate.co.uk/fixedPosition.html)
I use the Star Selector hack to hide these styles from IE 7, which supports fixed
positioning. Because this being invalid CSS, I recommend inclusion in an extra style sheet via Conditional Comments. - Smoooooth.
-
The same consideration applies to the overlay and the spinner. There is still some overhead to compute their dimensions and position depending on the state of the viewport.
Use fixed positioning here as well and make the overlay 100% wide and high:
#TB_overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; ... } #TB_load { position: fixed; top: 50%; left: 50%; margin: -50px 0 0 -50px; /* -height/2 0 0 -width/2 */ ... } -
Change
TB_load_position();to$('#TB_load').show();and throw awayfunction TB_overlaySize(){ ... }and all references to it. Also remove
function TB_load_position() { ... } function TB_getPageScrollTop(){ ... }Nearly done!
-
The only thing we still need to do is to accommodate IE 6 with a Dynamic Property again:
* html #TB_overlay { position: absolute; height: expression(document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + 'px'); }And don’t forget to add
* html #TB_load:* html #TB_window, * html #TB_load { ... }
Here’s a demo with all these modifications to the original. Try scrolling and resizing the window and you will see the difference. I’d really like to see that in a future version of Thickbox. Have fun!
37 Comments
Comments feed
Comment by Jörn, November 6th, 2006 at 9:48 am
Author Comment
Comment by Klaus, November 6th, 2006 at 11:03 am
Comment by Jörn, November 6th, 2006 at 11:44 am
Author Comment
Comment by Klaus, November 6th, 2006 at 12:49 pm
Comment by Paul McLanahan, November 6th, 2006 at 4:07 pm
Comment by Joel Birch, November 11th, 2006 at 4:33 am
Comment by Joe, December 8th, 2006 at 10:41 pm
Comment by Mike, December 13th, 2006 at 3:32 pm
Comment by Joel Birch, January 24th, 2007 at 6:49 am
Comment by Cody Lindley, January 26th, 2007 at 5:25 pm
Comment by Cody Lindley, January 26th, 2007 at 5:50 pm
Author Comment
Comment by Klaus, January 27th, 2007 at 1:01 pm
Comment by Shahin, January 31st, 2007 at 9:24 pm
Author Comment
Comment by Klaus, February 1st, 2007 at 12:06 pm
Comment by andy, February 2nd, 2007 at 10:39 pm
Comment by Kirk, February 19th, 2007 at 9:26 pm
Author Comment
Comment by Klaus, February 19th, 2007 at 10:19 pm
Comment by Kirk, February 19th, 2007 at 11:09 pm
Comment by Tobin, April 25th, 2007 at 7:58 pm
Comment by Mike Windham, May 8th, 2007 at 11:29 pm
Comment by Jay, June 19th, 2007 at 2:58 pm
Author Comment
Comment by Klaus, June 19th, 2007 at 3:38 pm
Comment by Gilles, June 22nd, 2007 at 9:50 am
Comment by Thomas, August 28th, 2007 at 9:21 am
Comment by alexa mancini, September 3rd, 2007 at 12:37 pm
Author Comment
Comment by Klaus, September 3rd, 2007 at 2:57 pm
Comment by Nate, December 3rd, 2007 at 10:42 pm
Comment by nate, December 13th, 2007 at 6:47 pm
Comment by Nate, December 13th, 2007 at 6:54 pm
Comment by Rob, January 26th, 2008 at 7:15 am
Comment by Rob, February 2nd, 2008 at 9:46 pm
Author Comment
Comment by Klaus, February 5th, 2008 at 11:29 am
Comment by Koen, September 2nd, 2008 at 1:11 pm
Comment by ben, September 4th, 2008 at 10:21 am
Author Comment
Comment by Klaus, September 4th, 2008 at 11:39 am
Comment by ben, September 4th, 2008 at 4:03 pm
Author Comment
Comment by Klaus, September 4th, 2008 at 4:13 pm