Lights Out - Dimming/Covering Background Content with jQuery

11 January 2010 Monday

This application was viewed 764 times




This is a quick and easy approach that tackles a pretty common technique. By the end of this tutorial you will be able to add pop up message boxes complete with dimmed background to your existing site. Feel free to take a look at the demo to scope out exactly what we’ll be creating here today.

You may already be familiar with Lightbox – which does a number of the things we are aiming to accomplish today, so as an added disclaimer – The purpose of this tutorial is simply to show you how to dim/cover the background while displaying a message box.

View the DEMO

The Concept

Here’s the gist: When a link is clicked a div that covers the entire page becomes visible and lays on top of the content. On top of that we have our message box which hides everything again when it gets closed. There are a number of different customizations we can make to this process, but it’s important to have the overall picture before tweaking too much.

Step 1 – The HTML

  1. <body>  
  2.   
  3. <!--The overlay and message box-->  
  4.  <div id="fuzz">  
  5.     <div class="msgbox">  
  6.        <a class="close" href="#" ><img src="close.jpg"/></a>  
  7.     </div>  
  8.  </div>  
  9.   
  10. <!--The content to be hidden-->  
  11.  <div id="content">  
  12.        <a class="alert" href="#" >Alert Me!</a>  
  13.  </div>  
  14.   
  15. </body>  

When applying this to your own site, you will only need to include the top overlay and message box section, as the rest is just a placeholder for this example. The one element from the  “content to be hidden” section you will want/need to grab is the “.alert” link which will be the trigger for the message box when clicked.

I have named the overlay layer “fuzz” as it will be TV-style  static, this is easily adjustable in case that’s not what you want – more on that in Step 2.

Step 2 – The CSS

Next up let’s take care of the styling for our overlay and message box:

  1. /*Styles for fuzz overlay & message box*/  
  2.  #fuzzposition:absolutetop:0; left:0; width:100%; z-index:100; backgroundurl(´fuzz.gif´); display:nonetext-align:left; }   
  3.   
  4.    /*Message box, positioned in dead center of browser*/  
  5.    .msgbox{ position:absolutewidth:300pxheight:200pxz-index:200; border:5px solid #222background#FFFtop: 50%; left: 50%; margin-top: -100pxmargin-left: -150px; }   
  6.       .msgbox img {border:nonemargin:5px;}   
  7.   
  8.       /*The "X" in the upper right corner of msgbox*/  
  9.       .close{ top:0pxfloat:rightright; }  

If you want to change the overlay (#fuzz) to anything other than static, it is a simple matter of changing the background image – I have also included a semi-transparent black png that can be used for the dim effect in the attached files.

Step 3 – The jQuery

I have commented each item below to help guide you along:

  1. $(document).ready(function(){   
  2.   
  3.    //Adjust height of overlay to fill screen when page loads   
  4.    $("#fuzz").css("height", $(document).height());   
  5.   
  6.    //When the link that triggers the message is clicked fade in overlay/msgbox   
  7.    $(".alert").click(function(){   
  8.       $("#fuzz").fadeIn();   
  9.       return false;   
  10.    });   
  11.   
  12.    //When the message box is closed, fade out   
  13.    $(".close").click(function(){   
  14.       $("#fuzz").fadeOut();   
  15.       return false;   
  16.    });   
  17.   
  18. });   
  19.   
  20. //Adjust height of overlay to fill screen when browser gets resized   
  21. $(window).bind("resize"function(){   
  22.    $("#fuzz").css("height", $(window).height());   
  23. });  

As far as customization at this stage, you could simply choose to replace fadeIn/Out with show/hide for instant results. You could also go as far as having the message box slide in from off screen. Quite a few possibilities, get creative with it.

Tags: jquery, effects, modal,


Lights Out - Dimming/Covering Background Content with jQueryBookmark and Share

You write your thoughts

Please login to post comments here from the right corner.

your add
icon free icon icon set vector icon free catalog catalog template jquery news ticker widget tools tutorial php slider video youtube effects menu thumbnail forms facebook zoom image combo multiselect modal polaroid gallery expand code feedback contact navigation slider tagcloud Skateboard psd templates files badges subscribe panel login temlate file free office holland free template tabs prototype css scriptaculous opacity moo autocomplete wordpress themes design theme button images photoshop fonts font nav pdf logo style high javascript pretty photo jqery lightbox Themes Blog popular blog desing joomla xhtml brushes hair footer admin mootols slide show blackbox new download class first plugin upload browse Page effect modalbox web application urlitooltips sliding boxes captions flatchat chat ajax messaging browser hoverlightbox CSS Dev General Javascript PHP SQL search avatar vector Drupal Joomla Oscommerce javaScript phototype manager ajaxplorer flash xml rotator bookmarking pageflip slideshow poll text component script mediaplayer imagegallery pictograms car poster player mp3 radio onlineradio navigations drop-down collection brush tree textures paper pop-up bumpbox web2.0 iphone film frame twitter page fan mail delete dynamic calender form handbook city social paint timeline clock connect bar sharing accordion quotes ticker photo-effects background 3D media player graphic rss leaf green glossy bubble icon comic clean cool web 2.0 png actionscript ietter rotating Menu animated textured site masking pasword app layer sponge bob popup bubble tooltips horizontal prototip tooltip pack charachters comics blog action icon.psd flavour navi tango gradients istanbul mixed css3 sleek animations program html5 website Layer styles ultimate candy Media icons dropdown snow icon innerfade hover sub tag