GrayOut Javascript


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function grayOut(vis, options) {
  2. // Pass true to gray out screen, false to ungray
  3. // options are optional. This is a JSON object with the following (optional) properties
  4. // opacity:0-100 // Lower number = less grayout higher = more of a blackout
  5. // zindex: # // HTML elements with a higher zindex appear on top of the gray out
  6. // bgcolor: (#xxxxxx) // Standard RGB Hex color code
  7. // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  8. // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  9. // in any order. Pass only the properties you need to set.
  10. var options = options || {};
  11. var zindex = options.zindex || 50;
  12. var opacity = options.opacity || 70;
  13. var opaque = (opacity / 100);
  14. var bgcolor = options.bgcolor || '#000000';
  15. var dark=document.getElementById('darkenScreenObject');
  16. if (!dark) {
  17. // The dark layer doesn't exist, it's never been created. So we'll
  18. // create it here and apply some basic styles.
  19. // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
  20. var tbody = document.getElementsByTagName("body")[0];
  21. var tnode = document.createElement('div'); // Create the layer.
  22. tnode.style.position='absolute'; // Position absolutely
  23. tnode.style.top='0px'; // In the top
  24. tnode.style.left='0px'; // Left corner of the page
  25. tnode.style.overflow='hidden'; // Try to avoid making scroll bars
  26. tnode.style.display='none'; // Start out Hidden
  27. tnode.id='darkenScreenObject'; // Name it so we can find it later
  28. tbody.appendChild(tnode); // Add it to the web page
  29. dark=document.getElementById('darkenScreenObject'); // Get the object.
  30. }
  31. if (vis) {
  32. // Calculate the page width and height
  33. if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
  34. var pageWidth = document.body.scrollWidth+'px';
  35. var pageHeight = document.body.scrollHeight+'px';
  36. } else if( document.body.offsetWidth ) {
  37. var pageWidth = document.body.offsetWidth+'px';
  38. var pageHeight = document.body.offsetHeight+'px';
  39. } else {
  40. var pageWidth='100%';
  41. var pageHeight='100%';
  42. }
  43. //set the shader to cover the entire page and make it visible.
  44. dark.style.opacity=opaque;
  45. dark.style.MozOpacity=opaque;
  46. dark.style.filter='alpha(opacity='+opacity+')';
  47. dark.style.zIndex=zindex;
  48. dark.style.backgroundColor=bgcolor;
  49. dark.style.width= pageWidth;
  50. dark.style.height= pageHeight;
  51. dark.style.display='block';
  52. } else {
  53. dark.style.display='none';
  54. }
  55. }

URL: http://www.hunlock.com/blogs/Snippets:_Howto_Grey-Out_The_Screen

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.