Jump to content

JS Drop Menu


unsider

Recommended Posts

Alright, currently I'm in need of a drop down menu.

 

Allthough unlike the googled standard, I'm not looking for onMouseHover effect, I'm looking to click on the object, and it remain in it's pullDown state until I click either on the object or elsewhere within the browser.

 

I am very tired, and normally would provide some code for us to work with, but for now I'm going to ask and see if I get some responses, and I might post some tomorrow depending where this goes.

 

If there is a tutorial out there that you could point me towards I'd appreciate that as well.

 

 

Link to comment
https://forums.phpfreaks.com/topic/123223-js-drop-menu/
Share on other sites

Keep in mind that the principle of the dynamic menu is really the same whether you use hover or click for the event. I would recommend you write up a JavaScript class for your menu element to handle all the nuances of the control. Here is a base that should get you started on your way. Keep in mind that the mouseover and mouseout events are handled a little differently in browsers, but what I have here works well in both FF and IE:

 

CSS:

body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
color: #454545;
}

#popup {
border: 1px solid #aaaaaa;
padding: 2px;
display: none;
position: absolute;
top: 50%;
left: 50%;
height: 100px;
width: 300px;
margin-top: -50px;
margin-left: -150px;
background-color: #ffffff;
}

#popup p {
border: 1px solid #ffffff;
background-color: #aaaaaa;
padding: 42px 100px;
color: #ffffff;
text-align: center;
margin: 0;
}

 

HTML:

<div id="popup"><p>Hi there!</p></div>
<p><a href="#" id="menu-trigger">Click me!</a></p>

 

JavaScript:

function popup(id, t_id)
{
this.ele     = document.getElementById(id);
this.trigger = document.getElementById(t_id);
this.is_over = false;

var o = this;
this.hide = function()
{
	if (this.is_over == false)
	{
		this.ele.style.display = 'none';
	}
};

this.show = function()
{
	o.ele.style.display = 'block';
};

this.setOver = function()
{
	o.is_over = true;
};

this.setOut = function()
{
	o.is_over = false;
};

this.ele.onmouseover = this.setOver;
this.ele.onmouseout  = this.setOut;
this.trigger.onmouseover = this.setOver;
this.trigger.onmouseout  = this.setOut;
this.trigger.onclick     = this.show;

// Set up to handle any clicks outside our bounds
var x = (document.onclick) ? document.onclick : function() {};
document.onclick = function()
{
	x(); // Any previously assigned click handles -- allows for multiple objects, too
	o.hide();
};
}

var mypopup;
window.onload = function()
{
mypopup = new popup('popup', 'menu-trigger');
};

 

With a little tweaking, you would easily be able to assign this type of object to your menu structure. The nice thing about handling your JavaScript in objects like this is that you would easily be able to assign multiple menu style occurrences within your window.onload assignment simply by adding another object for each.

 

Good luck!

Link to comment
https://forums.phpfreaks.com/topic/123223-js-drop-menu/#findComment-636620
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.