Control.Modal

Lightboxes, Modal Windows and Tooltips for Prototype.js

Introduction

By default, Control.Modal creates modal windows and lightboxes from any links/anchors elements on your pages. Since it attaches these behaviors to HTML that already has semantic meaning, it will degrade gracefully for browsers that do not support JavaScript, and is search engine friendly. It attaches in one line of code for simple use cases, but is highly customizable and can be used in a variety of edge cases.

Examples of Common Use Cases

NameDescription
Relative Modal Link HREF: #test_one_contents Parameters: opacity: 0.8, position: 'relative', width:300, height:50
Centered Modal Link HREF: #test_two_contents Parameters: containerClassName: 'test', overlayClassName: 'test', width: 300
AJAX Modal Link HREF: http://livepipe.net/projects/control_modal/modal_ajax Parameters: none
IFrame Modal Link HREF: http://google.com/ Parameters: iframe: true, width: 640, height: 480
Simple Lightbox Link HREF: /ryan/photos/photos/Plants/Banners_Closeup.jpg Parameters: none
Lightbox w/Effects Link HREF: /ryan/photos/photos/Plants/Banners_Closeup.jpg Parameters: fade: true
Hoverbox Link HREF: #hoverbox_one_contents Parameters: hover: true, position: 'relative', offsetLeft: 100
<span> Hoverbox Parameters: hover: true, position: 'relative', offsetLeft: 120, contents: 'Control.Modal can attach to any element.'
<span> Tooltip Parameters: position: 'mouse', offsetTop: 20, contents: 'Control.Modal can act as a tooltip too!'
I am the innerHTML of a div with the id "test_one_contents". You can make a modal window open relative to any element on the page.
I am the innerHTML of a div with the id "test_two_contents". By default, all modals open in the center of the screen. This modal window has and the overlay that dim the screen both have class names attached.
Modals can also act like tooltips / hoverboxes.

Sample Code

<a href="#test_one_contents" id="modal_link_one">Relative Modal</a>
<div id="test_one_contents">
    I am the innerHTML of a div with the id "test_one_contents". You can make a modal window open relative to any element on the page.
</div>
<script>
    new Control.Modal('modal_link_one',{
        opacity: 0.8,
        position: 'relative',
        width: 300,
        height: 50
    });
</script>

Note that all of the examples above use id attributes in the anchor tags. If you were to assign class="modal" to each link, you could instead do the following if all modals are going to have the same options:

<a href="#modal_window_one" class="modal">Modal One</a>
<a href="#modal_window_two" class="modal">Modal Two</a>
<script>
    document.getElementsByClassName('modal').each(function(link){
        new Control.Modal(link);
    });
</script>

HTML / CSS Containers & Names

This script automatically creates two nodes that are direct children of the body node of your document. They are #modal_overlay and #modal_container. The overlay is what makes the screen dim. The container contains the contents of the currently open modal window. You need to assign some default styles to these containers. This is more or less what is used on this page:

  1. #modal_container {  
  2.     padding:5px;  
  3.     background-color:#fff;  
  4.     border:1px solid #666;  
  5.     overflow:auto;  
  6.     font-family:"Lucida Grande",Verdana;  
  7.     font-size:12px;  
  8.     color:#333;  
  9.     text-align:left;  
  10. } 
  11. #modal_overlay {  
  12.     background-color:#000;  
  13. }  

Using Control.Modal as a Lightbox

If your link ends in a file name that has any of the following extensions jpg, jpeg, gif, png, tif, tiff, Control.Modal will automatically treat it as an image modal / lightbox. An image element with an id of #modal_image is automatically created and destoryed each time the modal opens and closes.

Using Control.Modal as a Hoverbox

You can easily repurpose any modal window to act as a hoverbox by specifying {hover: true} in the options hash. In the example used above notice that position was set to relative and an offsetLeft was specified. The modal can be in any mode (local, iframe, ajax, image), and the two differences in functionality are that the overlay never appears, and instead of a click opening and closing the window, the onmouseover and onmouseout events on the link trigger it. Note that if the modal completely overlaps the link that opened it, you won't be able to close the modal in some browsers since the onmouseout even will no longer fire. You can prevent this by offsetting the modal window from the link or attaching Control.Modal.close events to other elements on the page.

Using Control.Modal as a Tooltip

Pass in the option {position: 'mouse'} on any modal to make it act like a tooltip (the modal will follow the mouse). {hover: true} is automatically applied when this happens, so most of the functionality and caveats that apply to a hoverbox apply here. Note that Safari 1 & 2 both have CPU usage problems on complex pages with this option. Firefox and IE both behave as expected. This will be fixed in an upcoming release.

Local, AJAX or iframe Modals

You can open a modal window to content that is already on the page, so long as it is conatained in any element that has an id. You create this kind of link just as you would link to any other element id <a href="#my_container_name">. If you want to link to an AJAX call just specify the full URL in your link's href. To create an iframe, do the same and pass in the parameter {iframe: true}. The iframe element will have an id of #modal_iframe by default.

Linking to a Modal Window from Another Page

You can have a modal window pre opened on the page if is local / on page (not an AJAX, iframe or image modal), by adding the id of the container to the page url. For example load the following URL in another window: http://livepipe.net/projects/control_modal/#test_one_contents

Closing the Modal Window with a Link

You can close any modal window by calling Control.Modal.close(); You don't need a reference to the currently open modal window to do that. From inside an opened iframe you'll need to call parent.Control.Modal.close();