drop down at mouse location
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
drop down at mouse location
Need menu in js or jquery that when you click the mouse it drops down at mouse location. Basically it can be a click event on a div.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: drop down at mouse location
That is kinda' sort of what I needed for my PBBG map making program I made some time back. What you can do is create an invisible div that follows the mouse around provided that a variable, maybe called something like isSelectingOption, is false. Then when the player clicks, you set the isSelectingOption variable to true and add a drop-down element inside that div. Once an option is selected, turn isSelectingOption back to false and hide the drop-down so that the div will start following the mouse again. In this way, it will appear that the drop-down appears when the player clicks the disappears after an item is selected.
EDIT: I am not sure if this might be kind of hacky, and there may be a better way of doing it.
EDIT: I am not sure if this might be kind of hacky, and there may be a better way of doing it.
The indelible lord of tl;dr
Re: drop down at mouse location
A real dropdown for in a form, or more like a menu?
Read the title.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Written while high :D</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script type="text/javascript">
$(document).ready( function()
{
$(document).mousedown( function(e)
{
$('#dropdown').css({ top : e.pageY, left : e.pageX });
$('#dropdown').slideDown();
});
});
</script>
<style type="text/css">
#dropdown {
display: none;
position: absolute;
background: #F5F5F5;
border: #000 1px solid;
}
</style>
</head>
<body>
<ul id="dropdown">
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</body>
</html>
Fighting for peace is declaring war on war. If you want peace be peaceful.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: drop down at mouse location
Ahh, yes, that would probably be more efficient than having the JS engine handle a div tracing around with the mouse. That method worked better for me in my project, but in my project the div always had to be there since it was an aiming reticle for dropping props on a map. Since in Halls' case the menu probably will not be shown most of the time it would be a waste to keep it always tracking the mouse and it would be better to just move the menu to the mouse when needed.
The indelible lord of tl;dr
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: drop down at mouse location
Awesome Thanks!!!
Re: drop down at mouse location
Stepping over to the dark side halls?hallsofvallhalla wrote:Awesome Thanks!!!
Fighting for peace is declaring war on war. If you want peace be peaceful.
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: drop down at mouse location
your rock! it works great!
of course now when I click on a drop down item it just moves the box
will have to find a way to freeze mousedown if menu open or something.
of course now when I click on a drop down item it just moves the box
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: drop down at mouse location
k nevermind, it works but I need it to be an event of a div.
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: drop down at mouse location
k got it working, figured I would share
Basically I dynamically build the div tags then create the mouse function off of each one of them.
Code: Select all
for (i=7;i<=62;i++)
{
var divTag = document.createElement("div");
divTag.id = "div" + i;
divTag.className ="dynamicDiv";
document.getElementById("maparea").appendChild(divTag);
$('#div' + i).mousedown( function(e)
{
$('#dropdown').css({ top : e.pageY, left : e.pageX });
$('#dropdown').slideDown();
});
}