Multi-Column List

Introduction

This script will take an html list (ul or ol) and reformat it after a page loads so that the list spans multiple columns. The list and list items are converted to block display (see notes).

The script uses DOM-compliant coding and as a result older browsers (such as Netscape Navigator 4.x) will not perform any of the operations. Also, the functionality of the script was design so that if javascript were disabled or the page accessed by an older browser the user would still be able to view the content of the page.

Requirements

A W3C DOM-compliant browser with JavaScript and CSS support enabled. Tested in both standards mode and quirks mode.

Tested and functional:

Tested and non-functional:

Usage

Attach the JS file (formatted or size-optimized) to a document using <script src...></script> or some other mechanism.

Include in the html document a stylesheet that contains the basic style information. Additional styling can be defined as desired, but the basic style information must be included for the script to function as expected.

The menu uses a list (ul or ol) with a class attribute containing "multiColumnList" as it's source.

Modify user parameters as desired. The following parameters can be set on the web page referencing this script after the script reference.

mcl_columnCount
[integer] Indicates the (max) number of columns to create from the list. While the script will attempt to fill in all columns specified, the actual number of columns in which content is placed will be based on the calculation of the height of the content in each column.
Default: 2

Sample

  1. <html>
  2. <head>
  3. <title>Multi-Column List</title>
  4. <link rel="stylesheet" type="text/css" href="multiColumnList.css" />
  5. <script type="text/javascript" src="multiColumnList.js"></script>
  6. </head>
  7. <body>
  8. <ul class="multiColumnList">
  9. <li><p><a href="http://www.google.com">Google</a></p></li>
  10. <li><p><a href="http://www.google.com">Google</a></p></li>
  11. <li><p><a href="http://www.google.com">Google</a></p></li>
  12. <li><p><a href="http://www.google.com">Google</a></p></li>
  13. <li><p><a href="http://www.google.com">Google</a></p></li>
  14. <li><p><a href="http://www.google.com">Google</a></p></li>
  15. </ul>
  16. </body>
  17. </html>

Comments on the sample:

See the demo page for a demonstration of the features of this script.

Functions

The following functions are included in the show/hide code library. Additional line-item documentation is provided in the code itself. Source information for public, externally developed functions is provided; documentation for these functions is available from the source.

Function mcl_bwcheck
Source DHTML Central
http://www.dhtmlcentral.com/projects/lib/reference.asp?m=58
Creates an object that stores information about the user agent
Parameters None
Notes

2006-05-05 (bsweeney@aaas.org)

  • added a check for IE7

2005-09-20 (bsweeney@aaas.org)

  • added a check for Safari

 

Function mcl_addEvent
Source Scott Andrew LePera
http://www.scottandrew.com/weblog/articles/cbs-events
Cross-browser code to add an event handler to a DOM object
Parameters
obj
[object] the DOM object to which an event is being attached
evType
[string] the event handler being added ('click', 'mouseover', etc.)
fn
[function] the function called by the event handler
useCapture
[boolean] determines if the event fires during capture or bubble
Notes

2005-09-20 (bsweeney@aaas.org)

  • removed alert for unsupported browsers

 

Function mcl_init
Details This function initializes the multi-column list. The page is parsed for lists that have a class of "multiColumnList" which are then pushed into an array. These lists are assigned another class of "mclList". Next the list items are given a percentage width consistent with the number of columns and processed to determine the total height. The height is divided by the number of columns and the list items are processed again for the first column. Once the first column is set, subsequent columns are determined based on its height. Subsequent columns are created by providing a negative top margin of the first item in each column and giving all items in each column a left margin. The last item in the list is given a height to make up space between it and the max column height so that content following the list does not overlap it. The height of the list is determined and stored in a temporary variable. Lastly, the mcl_correct function is called.
Parameters None (used as event handler)
Notes

None

 

Function mcl_correct
Details This function checks the height of each list with the previously stored height. If there is a discrepancy the list is reprocessed to determine the height of the first column so that the top margin of the first list item in each subsequent column, and the height of the last list item, can be set. This process is needed because these values are set in pixels. If these values are next set as the list changes dimensions the columns will not be aligned correctly.
Parameters None (parses array of list DOM nodes, called recursively)
Notes

None

To Do

Notes

2006-05-18 (bsweeney@aaas.org)

2006-05-17 (bsweeney@aaas.org)

2005-09-20 (bsweeney@aaas.org)