Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have got a hover menu(using javascript) that is getting displayed when ever i mouseover on any of the tree node.

But the position of the menu is not correct...

Can any body tell me how do i pop up the hover menu at the position where my mouse cursor is.... Provided I have the X & Y co-ordinates of the cursor position.

So specifically, i want to know how to position a hover menu at some particular co-cordinates[x,y].

Please help me out. It's urgent!!

Snigdharani Behera
Posted

Hi All,

Just now found the correct answer.
It goes like this:

C#
function ShowPopup(hoveritem, arg)
        {
            hp = document.getElementById("hoverpopup");
            // Set position of hover-over popup
            hp.style.position = "absolute";
            hp.style.top = getPosition(event).y - 5;
            hp.style.left = getPosition(event).x - 5;
            // Set popup to visible
            hp.style.visibility = "Visible";
        }

        function HidePopup()
        {
            hp = document.getElementById("hoverpopup");
            hp.style.visibility = "Hidden";
        }

        function getPosition(e)
        {
            e = e || window.event;
            var cursor = {x:0, y:0};
            if (e.pageX || e.pageY)
            {
                cursor.x = e.pageX;
                cursor.y = e.pageY;
            }
            else
            {
                var de = document.documentElement;
                var b = document.body;
                cursor.x = e.clientX +
                    (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
                cursor.y = e.clientY +
                    (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
            }
            return cursor;
        }



But now i have one problem, that is I am not able to select a menu item.
Because as soon as i try to select a menu item, the mouseout even fires, n the menu dissapears.

Can somebody help!!

Snigdharani Behera
 
Share this answer
 
Hi,

Though I am not getting any response, still i would like to post more answers, so that others can be benefited.

I found a solution to my problem, stated earlier.

Try this code:

C#
//#############New Code###############
        var menuArray = new Array();
        menuArray[0] = 'hoverpopup';
        //menuArray[1] = 'submenu2';
        var globalObject = '';
        var isActive = false;
        var ourTimer;

        function showhide(obj,onlink,arg)
        {
            targetObject = document.getElementById(obj).style;

            globalObject = targetObject;
            if(onlink)
            {
                clearTimeout(ourTimer);

                for (i=0; i < menuArray.length; i++)
                {
                    var tempObject = document.getElementById(menuArray[i]).style;
                    tempObject.visibility = "hidden";
                }

                targetObject.visibility = 'visible';
                targetObject.position = "absolute";
                targetObject.top = getPosition(event).y;
                targetObject.left = getPosition(event).x;
                isActive = true;
            }
            else
            {
                isActive = false;
                layerTimer();
            }
        }

        function layerTimer()
        {
            ourTimer = setTimeout("hideMenu()",0750);
        }

        function hideMenu()
        {
            if( !isActive )
            {
                globalObject.visibility = 'hidden';
            }
        }

        function layerCheck(flag)
        {
            if(flag)
            {
                isActive = true;
                clearTimeout(ourTimer);
            }
            else
            {
                isActive = false;
                layerTimer();
            }
        }
        window.onload = function()
        {
            for(var i=0;i<menuArray.length;i++)
            {
                var id = menuArray[i];
                var e = document.getElementById(id);
                e.onmouseover = function(e)
                {
                    layerCheck(true);
                }
                e.onmouseout = function(e)
                {
                    layerCheck(false);
                }
            }
        }
        //############################



XML
<div id="hoverpopup" style="visibility:hidden; position:absolute; top:0; left:0;">
        <table bgcolor="#0000FF">
            <tr><td><font color="#FFFFFF">Option 1</font></td></tr>
            <tr><td bgcolor="#8888FF">Option 2</td></tr>
            <tr><td><font color="#FFFFFF">Option 3</font></td></tr>
            <tr><td bgcolor="#8888FF">Option 4</td></tr>
            <tr><td><font color="#FFFFFF">Option 5</font></td></tr>
            <tr><td bgcolor="#8888FF">Option 6</td></tr>
        </table>
    </div>


Hope it helps others!!

Snigdharani Behera
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900