Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

How to Create a Menu Using JavaScript

, 29 Jun 2012
Rate this:
Please Sign up or sign in to vote.
Create a menu using JavaScript on mouseover and hide this menu on mouse out event.

Introduction

In this article you learn how to create a menu using JavaScript on mouseover and hide this menu on mouse out event.

How to Create a Menu

First of all we need to add a style for a div which contains the menu and then for the table's column td.

<style>
.DivMenu {
      position:absolute;
      left:-200px;
      top:-1000px;
      width:180px;
      z-index:100;
      background-color: darkorange;
      border: 1px groove lightgrey;
   }
   .TDMenu {
      color:darkblue;
      font-family:verdana;
      font-size:70%;
      width:100%;
      cursor:default;
   }
</style>

In the ASPX page you can add any control on which you want to show a menu on mouseover event and hide on the mouseout event. Then you create a div and in this div use a table, and on both controls (div and table's td) apply the style class.

<div id="JsMenuDiv" class="DivMenu" onmouseout="return hideMenu(this)">
<TD id="MenuSaveAs" RollOver RollOut onclick="goPage('Default4.aspx')" class="TDMenu">

In td, we use Rollover RollOut. We use it in the JavaScript function; on the mouseover and mouseout events of this td, we change the color.

function document_onmouseover()
{
   var srcElement = event.srcElement;
   if (srcElement.tagName == "TD" && typeof(srcElement.RollOver) != "undefined")
   {
      srcElement.style.color = "white";
      srcElement.style.backgroundColor ="darkblue";
   }
}
function document_onmouseout()
{
   var srcElement = event.srcElement;
   if (srcElement.tagName == "TD" && typeof(srcElement.RollOut) != "undefined")
   {
      srcElement.style.color = "darkblue";
      srcElement.style.backgroundColor = "darkorange";
   }
}

document.onmouseover = document_onmouseover;
document.onmouseout = document_onmouseout;
Figure 1. When mouse over then backcolor change

Now we add two functions for showing and hiding menu on mouseover and mouseout events.

function showMenu(menuToShow)
{
   var srcElement = event.srcElement;
   var xPos = parseInt(srcElement.offsetLeft);
   var yPos = parseInt(srcElement.offsetTop);
   menuToShow.style.left = xPos + (srcElement.width)
   menuToShow.style.top = yPos+40;
}

function hideMenu(menuToHide)
{
   if (event.toElement != menuToHide &&
      menuToHide.contains(event.toElement) == false)
   {
      menuToHide.style.left = -200;
      menuToHide.style.top = -1000;
   }
}
Figure 2. When mouse over on Button then show menu

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Prosan
Software Developer (Senior)
India India
i work in ASP.Net,C#,Sql,VB6.0 etc. I want to enhance my skills by learning new technologies.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberProsan29-Jun-12 18:42 
Bug;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P PinmemberNarangGopal29-Jun-12 9:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 29 Jun 2012
Article Copyright 2012 by Prosan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid