Re: Scrolling to DOM node with JavaScript?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Scrolling to DOM node with JavaScript?

harry-6
Heikki Toivonen wrote:
> As the subject says, if I have a DOM node, is there any way I can scroll to
> the node with JavaScript? Meaning, if the node is not visible in the window
> I want the window to scroll so that the node becomes visible.
>
> --
>   Heikki Toivonen
>   http://www.citec.fi
>   http://www.doczilla.com

This is the way I do: I is the element to scroll to (an image in my
case) and w is the containing window.

var x=I.offsetLeft, y=I.offsetTop, h=I.offsetHeight, win=I.offsetWidth;
for(var P=I.offsetParent; P && P!=w; P=P.offsetParent)
{
   x+=P.offsetLeft; y+=P.offsetTop;
}
if(y<0)   y=0;
if(x<0)   x=0;
w.scrollTo(x, y);

The key is to summarize all offsets until the top level (the window) is
reached.

Harry
_______________________________________________
mozilla-layout mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-layout
Reply | Threaded
Open this post in threaded view
|

Re: Scrolling to DOM node with JavaScript?

Martin Honnen-3


harry wrote:

> Heikki Toivonen wrote:
>
>> As the subject says, if I have a DOM node, is there any way I can
>> scroll to
>> the node with JavaScript? Meaning, if the node is not visible in the
>> window
>> I want the window to scroll so that the node becomes visible.

For element nodes there is
   element.scrollIntoView(true)
or
   element.scrollIntoView(false)
implemented in Mozilla, following IE's implementation
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/scrollintoview.asp>

--

        Martin Honnen
        http://JavaScript.FAQTs.com/
_______________________________________________
mozilla-layout mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-layout
Reply | Threaded
Open this post in threaded view
|

Re: Scrolling to DOM node with JavaScript?

harry-6
In reply to this post by harry-6
Heikki Toivonen wrote:
> As the subject says, if I have a DOM node, is there any way I can scroll to
> the node with JavaScript? Meaning, if the node is not visible in the window
> I want the window to scroll so that the node becomes visible.
>
> --
>   Heikki Toivonen
>   http://www.citec.fi
>   http://www.doczilla.com

This is the way I do: I is the element to scroll to (an image in my
case) and w is the containing window.

var x=I.offsetLeft, y=I.offsetTop, h=I.offsetHeight, win=I.offsetWidth;
for(var P=I.offsetParent; P && P!=w; P=P.offsetParent)
{
   x+=P.offsetLeft; y+=P.offsetTop;
}
if(y<0)   y=0;
if(x<0)   x=0;
w.scrollTo(x, y);

The key is to summarize all offsets until the top level (the window) is
reached.

Harry
_______________________________________________
mozilla-layout mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-layout