943,186 Members | Top Members by Rank

Ad:
Apr 30th, 2007
0

Creating a popup menu on mouse over

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asull100 is offline Offline
3 posts
since Apr 2007
May 1st, 2007
0

Re: Creating a popup menu on mouse over

I'd get a book on JavaScript and read it. I used "JavaScript Demystified" by Jim Keogh.
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007
May 1st, 2007
0

Re: Creating a popup menu on mouse over

You can create a new window using the window.open() funtion. This will help http://www.w3schools.com/htmldom/met_win_open.asp
Reputation Points: 10
Solved Threads: 0
Light Poster
obscured47 is offline Offline
28 posts
since Apr 2007
May 1st, 2007
0

Re: Creating a popup menu on mouse over

Just go to the library and pick up a Javascript book.
Reputation Points: 10
Solved Threads: 1
Light Poster
JTroopSoldier is offline Offline
25 posts
since Mar 2005
May 10th, 2007
0

Re: Creating a popup menu on mouse over

You can use the dreamweaver from macromedia. In there you can find premade java-scripts that will do your job as well.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
seabob is offline Offline
3 posts
since May 2007
May 10th, 2007
0

Re: Creating a popup menu on mouse over

Click to Expand / Collapse  Quote originally posted by obscured47 ...
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..
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
May 14th, 2007
0

Re: Creating a popup menu on mouse over

cant exactly remember the code, but it would be in http://dynamicdrive.com
Reputation Points: 9
Solved Threads: 0
Newbie Poster
connor4312 is offline Offline
16 posts
since Apr 2007
Oct 12th, 2007
0

Re: Creating a popup menu on mouse over

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sameer_wanwey is offline Offline
1 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: require urgent help
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Window Scroll Position





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC