Pointer sensitive menus for Web pages
Thursday, November 1st, 2007Often there is a need for a specifc style of menus on Web pages. These menus show up after a specific event (for example: click or hover over) and they stay visible as long as the cursor is over them.
Developing such menus is not trivial at least to say. I have managed to find two solutions to this.
One solution is based on CSS (Cascading Style Sheets) described in the excellent article: Pure CSS Menus.
The solution I have worked out for the menu is based on JavaScript, goes like this:
- Retrieve the top HTML element for the menu or create the HTML elements for the menu. This can be a result of a mouse-click or a mouse-over event.
- Register the mouseout event for every HTML element under the menu, the event should invoke a function checking if the menu should be closed.
- Use the event.target (or event.srcElement on some browsers) and event.relatedTarget (or event.toElement on some browsers) properties from the event to check if the menu should be closed.
- The condition for closing the menu is: the mouse has not entered (event.relatedTarget) the top menu item and it has not entered any other element that is a child of the top menu item.
This second part may need a supporting function to determine whether an element is a child of another element, example (see isAChildOf function): Mouseenter and mouseleave events for Firefox (and other non-IE browsers) - (Optional) To get a funky effect, hook up the close action to a timer and only close it on expiry. In the meantime any mouseover event to any menu item should disable the timer.
Another example of the pointer sensitive menus can be found on the TDT 3D Web site. Unfortunately the code is scrambled, but it seems that it combines CSS and JavaScript to achieve a pointer sensitive, roll-down, delayed hiding menu effect.
















