Popup above cursor

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

    #1

    Popup above cursor

    Hi!

    I want to show a popup table just left-above my corsor. The following
    code works fine (on IE), but if there are scrollbars at the browser
    the popup is not positionend correctly:

    *************** *************** *************** *************** *****
    ....

    function ShowPopup() {
    document.getEle mentById(ShowPo pup.arguments[0]).style.visibil ity =
    'visible'
    document.getEle mentById(ShowPo pup.arguments[0]).style.top =
    event.y - document.getEle mentById(ShowPo pup.arguments[0]).offsetHeight
    document.getEle mentById(ShowPo pup.arguments[0]).style.left=
    event.x - document.getEle mentById(ShowPo pup.arguments[0]).offsetWidth
    }
    function HidePopup() {
    document.getEle mentById(HidePo pup.arguments[0]).style.visibil ity =
    'hidden'
    }

    ....

    <a href="www.googl e.com" onmouseover="Sh owPopup('ID_01' )"
    onmouseout="Hid ePopup('ID_01') ">PopItUp</a>
    ....

    <table id="ID_01" style="position :absolute; visibility:hidd en">
    <tr><td>This is a popup</td></tr>
    </table>

    ....

    *************** *************** *************** *************** *****

    I can't figure out what i have to do if there are scrollbars.
    As a second question: what do i have to change to let it work on NN?

    Thanks Helmut
  • Lasse Reichstein Nielsen

    #2
    Re: Popup above cursor

    muggelkid@yahoo .de (Helmut) writes:
    [color=blue]
    > function ShowPopup() {
    > document.getEle mentById(ShowPo pup.arguments[0]).style.visibil ity =
    > 'visible'
    > document.getEle mentById(ShowPo pup.arguments[0]).style.top =
    > event.y - document.getEle mentById(ShowPo pup.arguments[0]).offsetHeight
    > document.getEle mentById(ShowPo pup.arguments[0]).style.left=
    > event.x - document.getEle mentById(ShowPo pup.arguments[0]).offsetWidth
    > }[/color]

    I can do that shorter (and add scroll support too):

    function ShowPopup(id) {
    var elem=document.g etElementById(i d);
    elem.style.visi bility = "visible";
    var root = document.docume ntElement || document.body;
    elem.style.top = event.y + root.scrollTop - elem.offsetHeig ht;
    elem.style.left = event.x + root.scrollLeft - elem.offsetWidt h;
    }

    (Is there a reason for using ShowPupup.argum ents[0] istead of just
    specifying a named parameter?)
    [color=blue]
    > ...
    >
    > <a href="www.googl e.com" onmouseover="Sh owPopup('ID_01' )"
    > onmouseout="Hid ePopup('ID_01') ">PopItUp</a>
    > ...
    >
    > <table id="ID_01" style="position :absolute; visibility:hidd en">
    > <tr><td>This is a popup</td></tr>
    > </table>[/color]

    Why use a table? If you want to support Netscape 4, you will want to
    make it a div instead. You can put the table inside the div then, if
    you really need it.
    [color=blue]
    > I can't figure out what i have to do if there are scrollbars.[/color]

    See above.
    [color=blue]
    > As a second question: what do i have to change to let it work on NN?[/color]

    Which NN. Netscape 4 and Netscape 6/7 are completely unrelated browsers.


    Actually, in both Netscape 4 and Mozilla based browsers (including
    Netscape 6+), you have an easier time finding the mouse coordinates
    wrt. the page instead of the viewport, which is what you need to
    place something absolutely next to the mouse. Both browsers' event
    object have the properties pageX/pageY that are relative to the page,
    as opposed to clientX/clientY (or the equivalent x/y that you use)
    that are relative to the client window.

    It is harder to get the width and height of the element in Netscape 4
    (i.e., I couldn't find a way).

    The following code works in IE, Mozilla and Opera 7:

    ---
    <script type="text/javascript">
    function getElement(id) {
    var elem;
    if (document.getEl ementById) { // standard browsers
    elem = document.getEle mentById(id);
    } else if (document.all) { // IE 4
    elem = document.all[id];
    }
    return elem;
    }

    function ShowPopup(id,ev ent) {
    var elem = getElement(id);
    var elemStyle = elem.style || elem; // for NS4, not used here

    elemStyle.visib ility = "visible";

    var width = elem.offsetWidt h || elem.width || 100; // default to 100
    var height = elem.offsetHeig ht || elem.height || 100;

    var posX,posY;
    if (event.pageX) { // NS 4, Mozilla
    posX = event.pageX;
    posY = event.pageY;
    } else { // IE, Opera
    var root = document.docume ntElement || document.body;
    posX = event.clientX - root.scrollLeft ;
    posY = event.clientY - root.scrollTop;
    }

    elemStyle.top = posY - height + "px";
    elemStyle.left = posX - width + "px";
    }

    function HidePopup(id) {
    var elem = getElement(id);
    var elemStyle = elem.style || elem;
    elemStyle.visib ility = "hidden";
    }
    </script>
    <div id="foo"
    style="position :absolute;visib lity:hidden;bac kground:yellow; ">
    Some<br>Text
    </div>
    <br>
    <p>Argle Bargle Glop Glyf
    <a href=""
    onmouseover="Sh owPopup('foo',e vent)"
    onmouseout="Hid ePopup('foo')"> Link is here </a>
    <p>
    ---

    Other browsers will need other fixes.
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Helmut

      #3
      Re: Popup above cursor

      Thanks Lasse - that helped me a lot.

      [color=blue]
      > (Is there a reason for using ShowPupup.argum ents[0] istead of just
      > specifying a named parameter?)[/color]
      Yes: Lack of knowledge ;-)
      [color=blue]
      > Why use a table? If you want to support Netscape 4, you will want to
      > make it a div instead. You can put the table inside the div then, if
      > you really need it.
      >[/color]
      changed my code to that


      It now works for me but I had to do a little change:


      [color=blue]
      > The following code works in IE, Mozilla and Opera 7:
      >[/color]
      ....
      [color=blue]
      > function ShowPopup(id,ev ent) {[/color]
      ....[color=blue]
      > var posX,posY;
      > if (event.pageX) { // NS 4, Mozilla
      > posX = event.pageX;
      > posY = event.pageY;
      > } else { // IE, Opera
      > var root = document.docume ntElement || document.body;
      > posX = event.clientX - root.scrollLeft ;
      > posY = event.clientY - root.scrollTop;[/color]
      |
      -----------------------------
      It works if I use:
      posX = event.clientX + root.scrollLeft ;
      posY = event.clientY + root.scrollTop;

      I hope this was just a typo or did I miss something?

      Helmut

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Popup above cursor

        muggelkid@yahoo .de (Helmut) writes:
        [color=blue]
        > It works if I use:
        > posX = event.clientX + root.scrollLeft ;
        > posY = event.clientY + root.scrollTop;
        >
        > I hope this was just a typo or did I miss something?[/color]

        Doh. Just a typo.
        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        Working...