oncontextmenu placement at mouse click

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johkar

    oncontextmenu placement at mouse click

    I am having trouble figuring out how I can get a custom context menu to
    appear at the point of the mouse click. When the user right clicks
    inside a text box, I want my custom menu to appear at that point. The
    below kind of works unless the page scrolls and is scrolled down the
    page a ways. I need the menu to be placed correctly regardless of
    scrollbar position.

    function setMenu(elm){
    document.getEle mentById('myMen u').style.visib ility='visible' ;
    var x=document.body .scrollLeft + event.screenX;
    var y=document.body .scrollTop + event.screenY;
    document.getEle mentById('myMen u').style.left= x;
    document.getEle mentById('myMen u').style.top=y ;
    }

    <input type="text" name="textfield " oncontextmenu=" setMenu(this);r eturn
    false" />

    <div id="myMenu" style="position :absolute;visib ility:hidden">
    Some links here
    </div>
    Solutions or pointers for the major browsers appreciated.

    John

  • Ivo

    #2
    Re: oncontextmenu placement at mouse click

    "johkar" wrote[color=blue]
    > below kind of works unless the page scrolls and is scrolled down the
    > page a ways. I need the menu to be placed correctly regardless of
    > scrollbar position.
    >
    > function setMenu(elm){
    > document.getEle mentById('myMen u').style.visib ility='visible' ;
    > var x=document.body .scrollLeft + event.screenX;
    > var y=document.body .scrollTop + event.screenY;[/color]

    Make that:
    var y=document.body .scrollTop + event.screenY + getscrolltop();

    And, depending on your doctype, perhaps even: + 'px'

    Then add a function like:

    function getscrolltop(){
    var d=document;
    return window.pageYOff set ? window.pageYOff set : d.documentEleme nt &&
    typeof d.documentEleme nt.scrollTop=== 'number' ? d.documentEleme nt.scrollTop
    : d.body.scrollTo p;
    }
    [color=blue]
    > Solutions or pointers for the major browsers appreciated.[/color]

    You realize that basically all of your code sofar, oncontextmenu, the global
    event, is very much IE. Here 's a link to the FAQ of this newsgroup for a
    thorough read up and lots of pointers:


    Note that the above getscrolltop() caters for Gecko as well as IE browsers
    in both quircks and standards mode. You may wish to translate it into a
    getscrollleft() function too.
    HTH
    --
    Ivo



    Comment

    • johkar

      #3
      Re: oncontextmenu placement at mouse click

      Thank you for all the info.

      Comment

      Working...