Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: Position on Web Page

by erroneousBollock (Curate)
on Aug 19, 2007 at 17:12 UTC ( [id://633624]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Position on Web Page
in thread Position on Web Page

This is quite clearly best solved by Javascript.

Basically you want to draw your arrow (or whatever) so that its tip points to the area of interest.

Best bet is to:

  1. find the area in question (say by using document.getElementById)
  2. compute the (possibly relative) position of the element node.
  3. reposition/reorient your pointer to point to it.
The second step is generally a difficult problem for arbitrary DHTML documents in various browsers.

I recommend the "walk up the tree" approach... which is basically just walking up the .parent chain from your interesting node until you find the first node that is absolutely positioned.. then you walk back down, asking the browser for the "computed" styles of each container element until you get to the target node, collecting positioning attributes on the way down. You should end up with a "recipe" for how to compute the position of the target-node. (A thorough understanding of DOM, CSSp and browser DOM-bugs is required... implementation left as an exercise for the reader ;)

-David

Replies are listed 'Best First'.
Re^5: Position on Web Page
by user2000 (Sexton) on Aug 20, 2007 at 05:31 UTC
    Hi, Thankyou everyone for your replies. I am able to compute the position of the element node. Now using the element node, I am able to compute the position of the node. However say my node is a paragraph:
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam tellus dui, pharetra nec, pharetra ut, congue non, mauris. Vestibulum id lorem sit amet pede faucibus aliquet. Nunc nec nunc. Sed nec dui placerat elit pulvinar congue. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cum sociis natoque penatibus et magnis dis parturient montes
    In the above table, as you resize the window, the position of "Placerat" word changes. Now if my arrow was pointing to say "Placerat", its position should always be maintained irrespecitve if the browser is resized or not. When the page is fixed design, this works fine. But when using a table with %, it does not work. Is there somehow I can permanently attach the arrow to a particular word. Thank you very much, Anant
      Not easily.

      The most direct way to synchronize the position of the word with the pointer is to relatively position the pointer (wrt an invisible div surrounding the word)... but that would create text-flow problems around the target word.

      The "walk up the tree" approach I mentioned is till the best way to reliably find the correct position irrespective of layout technique... also, showing the pointer as an absolutely positioned div (above the standard content) doesn't affect the flow of the document.

      You can attach an event handler to the resize of the browser window such that the positioning code is re-called to reposition the pointer.

      You should probably invest in some good books about unobtrusive Javascript and the HTML DOM.

      -David

        Hi, Thank you for your reply. I have written code to find the location of the div:
         function findPosX(obj)
          {
            var curleft = 0;
            if(obj.offsetParent)
                while(1) 
                {
                  curleft += obj.offsetLeft;
                  if(!obj.offsetParent)
                    break;
                  obj = obj.offsetParent;
                }
            else if(obj.x)
                curleft += obj.x;
            return curleft;
          }
        
        Basically it keeps adding all the left offsets for the parent nodes until i reach the top. Is this alright? Once i get that I can only approsimately get the right position. Say the node is 'p tag'. Then it could have lots of text which will eventually resize. So i can get my arrow somewhere in 'p tag' for sure. But what about the location in that. If you resize this browser window, you will see that the text above that i have written will readjust too. However the smallest dom node returned will be 'p tag' for the above text. I can get using dom the location of p tag but how can i maintin the position of the arrow within that 'p tag' tag so that it points to the same word? or will it always be approximate? Also, if I have a node which I have got using event object, can I store it in some form (like offset from root etc...) so that I can retrieve it at a later stage (e.g. when I revisit the page, I will use perl to place the arrow over there but for that i need to know the node which I had clicked earlier). Is there anyway to store the node information so that I can retrieve it later? Thank you for your time and effort. Anant

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://633624]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-29 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found