Creating a popup menu on mouse over

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Apr 2007
Posts: 3
Reputation: asull100 is an unknown quantity at this point 
Solved Threads: 0
asull100 asull100 is offline Offline
Newbie Poster

Creating a popup menu on mouse over

 
0
  #1
Apr 30th, 2007
I'm creating a website and I want to have a popup menu with additional links come up when a text image is moused over.

I'm alright with HTML, but I know that this can't be done in HTML and I assume it's Javascript that I would need to use. Can anyone help me or point me in the direction of some resources that could help?

I'm pretty rusty with web developement and I know almost nothing about javascript.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 164
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Creating a popup menu on mouse over

 
0
  #2
May 1st, 2007
I'd get a book on JavaScript and read it. I used "JavaScript Demystified" by Jim Keogh.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 24
Reputation: obscured47 is an unknown quantity at this point 
Solved Threads: 0
obscured47 obscured47 is offline Offline
Newbie Poster

Re: Creating a popup menu on mouse over

 
0
  #3
May 1st, 2007
You can create a new window using the window.open() funtion. This will help http://www.w3schools.com/htmldom/met_win_open.asp
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 25
Reputation: JTroopSoldier is an unknown quantity at this point 
Solved Threads: 1
JTroopSoldier JTroopSoldier is offline Offline
Light Poster

Re: Creating a popup menu on mouse over

 
0
  #4
May 1st, 2007
Just go to the library and pick up a Javascript book.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 3
Reputation: seabob is an unknown quantity at this point 
Solved Threads: 0
seabob seabob is offline Offline
Newbie Poster

Re: Creating a popup menu on mouse over

 
0
  #5
May 10th, 2007
You can use the dreamweaver from macromedia. In there you can find premade java-scripts that will do your job as well.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Creating a popup menu on mouse over

 
0
  #6
May 10th, 2007
Originally Posted by obscured47 View Post
You can create a new window using the window.open() funtion. This will help http://www.w3schools.com/htmldom/met_win_open.asp
Using spawned windows as popup menus is a naff approach. Alot of 'modern' browsers block out popups, or put them behind the active window. Also, you'd have to be careful to avoid multiple window.opens every time the user moves the mouse...

You can make popup menus without any Javascript (using CSS)... but they only work well in those 'modern' browsers already mentioned.

If using Javascript, look at moving and hiding/showing some 'menu HTML', or if appropriate, creating such a menu dynamically..

The code I'm posting is gratuitously commented, and has been tested on Opera 9, Firefox 2, and IE6. It will let you assign different menus to multiple objects; but the menus don't disapear very cleanly... You can hack the Javascript around to do things like, alter the position of the popup relatively, so it appears 'from' the parent element rather than over it; or even at the position of the mouse exactly. You also might want to make the popups disapear automatically when the user mouses out of the vicinity of the popup... I'll leave that to you to research:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4. <title>Simple PopupMenu</title>
  5.  
  6. <style type="text/css">
  7. /*Important style definition is 'position:absolute'. Means the element can
  8. be placed at an arbitrary position without affecting the flow of surrounding
  9. elements*/
  10. .popup
  11. {
  12. position:absolute;
  13. border:solid 1px black;
  14. background-color:white;
  15. padding:4px;
  16. }
  17. </style>
  18.  
  19. <script type="text/javascript">
  20. //findPos function is from http://www.quirksmode.org/js/findpos.html;
  21. //where its workings are explained in more detail.
  22. function findPos(obj) {
  23. var curleft = curtop = 0;
  24. if (obj.offsetParent) {
  25. curleft = obj.offsetLeft
  26. curtop = obj.offsetTop
  27. while (obj = obj.offsetParent) {
  28. curleft += obj.offsetLeft
  29. curtop += obj.offsetTop
  30. }
  31. }
  32. return [curleft,curtop];
  33. }
  34.  
  35. //Display a named menu, at the position of another object
  36. function display_menu(parent,named)
  37. {
  38. //get the named menu
  39. var menu_element = document.getElementById(named);
  40. //override the 'display:none;' style attribute
  41. menu_element.style.display = "";
  42. //get the placement of the element that invoked the menu...
  43. var placement = findPos(parent);
  44. //...and put the menu there
  45. menu_element.style.left = placement[0] + "px";
  46. menu_element.style.top = placement[1] + "px";
  47. }
  48.  
  49. //Hide a named menu
  50. function hide_menu(named)
  51. {
  52. //get the named menu
  53. var menu_element = document.getElementById(named);
  54. //hide it with a style attribute
  55. menu_element.style.display = "none";
  56. }
  57. </script>
  58. </head>
  59. <body>
  60.  
  61. <!--Define the menu's contents. It's important to put the display:none style attribute
  62. here rather than in a <style> block, because it can be overriden easily if it's assigned here-->
  63. <div class="popup" id="menu1" style="display:none;">
  64. <ul>
  65. <li><a href="#">Menu 1</a></li>
  66. <li><a href="#">Menu 2</a></li>
  67. <li><a href="#">Menu 3</a></li>
  68. </ul>
  69. <!--Hide the menu when the text (Close popup) is clicked-->
  70. <span onclick="hide_menu('menu1');">(Close popup)</span>
  71. </div>
  72.  
  73. <!--Mouseovering on this element will show the menu with id 'menu1', at the position of {this}, that is
  74. this element-->
  75. <a href="#" onmouseover="display_menu(this,'menu1');">Popup on hover</a>
  76. <p>
  77. Other text here, should not be obscured on pops.
  78. </p>
  79. <p>
  80. Other text here, should not be obscured on pops.
  81. </p>
  82. <p>
  83. Other text here, should not be obscured on pops.
  84. </p>
  85. <a href="#" onmouseover="display_menu(this,'menu1')">Popup on hover</a>
  86.  
  87. </body>
  88. </html>
  89.  

As said, a similar effect is afforded much more gracefully using only HTML and CSS. The following example is tested to work in Opera 9 and Firefox 2, but DOES NOT WORK IN INTERNET EXPLORER 6. It uses the CSS 'next element' selector; which doesn't work in IE6 (nor does parent:hover child, strangely enough). This menu WILL go away automagically when the user mouses away from it; but, as said, won't work with old browsers...

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4. <title>CSS PopupMenu</title>
  5. <style>
  6. div.popup
  7. {
  8. display:none;
  9. position:absolute;
  10. border:solid 1px black;
  11. padding:8px;
  12. background-color:white;
  13. }
  14. a.popup:hover + div.popup
  15. {
  16. display:block;
  17. }
  18. div.popup:hover
  19. {
  20. display:block;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25.  
  26. <a class="popup" href="#">Popup on hover</a>
  27.  
  28. <div class="popup">
  29. <ul>
  30. <li><a href="#">Menu 1</a></li>
  31. <li><a href="#">Menu 2</a></li>
  32. <li><a href="#">Menu 3</a></li>
  33. </ul>
  34. </div>
  35.  
  36. <p>
  37. Other text here, should not be obscured on pops.
  38. </p>
  39.  
  40. </body>
  41. </html>
Last edited by MattEvans; May 10th, 2007 at 7:28 pm. Reason: html codeblocks are well messy..
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 16
Reputation: connor4312 is an unknown quantity at this point 
Solved Threads: 0
connor4312's Avatar
connor4312 connor4312 is offline Offline
Newbie Poster

Re: Creating a popup menu on mouse over

 
0
  #7
May 14th, 2007
cant exactly remember the code, but it would be in http://dynamicdrive.com
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1
Reputation: sameer_wanwey is an unknown quantity at this point 
Solved Threads: 0
sameer_wanwey sameer_wanwey is offline Offline
Newbie Poster

Re: Creating a popup menu on mouse over

 
0
  #8
Oct 12th, 2007
Can u give me same functionality for gridview elements

Please its urgent!

I tryed but it shows allways first elmets data as popup and one more thing grid view have bounded coulumns.

Please Help
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC