Current location: Hot Scripts Forums » General Web Coding » JavaScript » Switch Menu - Default Close


Switch Menu - Default Close

Reply
  #1 (permalink)  
Old 02-03-10, 02:39 PM
Frogger Frogger is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 149
Thanks: 5
Thanked 0 Times in 0 Posts
Switch Menu - Default Close

Hi all,

Can someone please help. I found this code (can't remember where now) for a very simple switch menu. I would like to modify the code some how so that the switch menu is closed when you first see it. At the moment the default is open. Any ideas how I can change the code for this to happen?

And also how could I modify the script so that rather onmouse click it would run the same with onmouse over.

Thanks again

Code:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
<style type="text/css">
<!--
.menuCategory {
	width: 200px;   
   	background-color: green;
   	color: #036;
   	padding: 2px;
   	margin-top: 1px;
}
-->
</style>

<script language="JavaScript" type="text/javascript">
function toggle(obj) {
  with (document.getElementById(obj).style) { display = (display == 'none') ? 'block' : 'none'; }
}
</script>

</head>

<body>

<a href="javascript:toggle('menu_1')"><div class="menuCategory">menu item 1</div></a>
<div id='menu_1'>
<div class='menuTopic'><a href="switch.htm">content to menu item 1</a></div>
<div class='menuTopic'><a href="switch.htm">content to menu item 2</a></div>
<div class='menuTopic'><a href="switch.htm">content to menu item 3</a></div>
</div>

<a href="javascript:toggle('menu_2')"><div class="menuCategory">menu item 2</div></a>
<div id='menu_2'>
<div class='menuTopic'>content to menu item 4</div>
<div class='menuTopic'>content to menu item 5</div>
</div>

<a href="switch.htm">switch</a>

</body>
</html>
Reply With Quote
  #2 (permalink)  
Old 02-03-10, 05:35 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
HTML Code:
<div onmouseover="toggle('menu_1')">
<div id='menu_1' style="display:none">
Not tested
Reply With Quote
The Following User Says Thank You to wirehopper For This Useful Post:
Frogger (02-03-10)
  #3 (permalink)  
Old 02-03-10, 05:52 PM
Frogger Frogger is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 149
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks again wirehopper. That was exactly what i was looking for.

perfect
Reply With Quote
  #4 (permalink)  
Old 02-03-10, 06:22 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Just using onmouseover on the main menu items will just display the submenu items and they will stay displayed.

What you might want to consider is clicking on the main menu items to display the submenu items
and use onmouseout to close the submenu items.

Here I use onclick and onmouseout in the main menu items.
This causes the submenu items to display when a main menu item is clicked.
And when you mouseout of the main menu item the submenu item will hide.
Now when you traverse from the main menu item to the submenu item you do not want the submenu item to hide until you mouseout of the submenu.
So to make this happen correctly I use onmouseover and onmouseout in the submenu items.

Example:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
<!--
#menu_1, #menu_2 {
      display:none;
      width:200px;
      padding: 2px;
      background:#ff0;
}
.menuCategory {
       width:200px;
      background-color: green;
       color: #036;
       padding: 2px;
       margin-top: 1px;
       cursor:pointer;
       text-decoration:underline;
}
-->
</style>
<script language="JavaScript" type="text/javascript">
function open_menu(obj) {
  document.getElementById(obj).style.display = 'block';
}
function close_menu(obj) {
  document.getElementById(obj).style.display = 'none';
}
</script>
</head>
<body>
 <div class="menuCategory" onclick="open_menu('menu_1');" onmouseout="close_menu('menu_1');">menu item 1</div>
  <div id='menu_1' onmouseover="open_menu(this.id);" onmouseout="close_menu(this.id);">
   <div class='menuTopic'><a href="switch.htm">content to menu item 1</a></div>
   <div class='menuTopic'><a href="switch.htm">content to menu item 2</a></div>
   <div class='menuTopic'><a href="switch.htm">content to menu item 3</a></div>
  </div>
 <div class="menuCategory" onclick="open_menu('menu_2');" onmouseout="close_menu('menu_2');">menu item 2</div>
  <div id='menu_2' onmouseover="open_menu(this.id);" onmouseout="close_menu(this.id);">
   <div class='menuTopic'>content to menu item 4</div>
   <div class='menuTopic'>content to menu item 5</div>
  </div>
 <a href="switch.htm">switch</a>
</body>
</html>
__________________
Jerry Broughton
Reply With Quote
  #5 (permalink)  
Old 02-03-10, 06:38 PM
Frogger Frogger is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 149
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks job0107. That too is another great idea.

Now I have 2 great scripts to use. Thanks again
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Keypad Login smithygotlost Script Requests 26 07-11-07 12:29 PM
MYSQL database countll Database 2 06-19-07 04:20 PM
What do you think of my database structure? Oskare100 Database 5 12-27-06 07:43 AM
Left Join with Where & Count is eluding me CutAndPaste PHP 8 12-13-05 03:46 AM
Newbie MySQL fccolon PHP 2 03-16-04 10:54 AM


All times are GMT -5. The time now is 01:59 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.