safe scrollIntoView() for AJAX

scrollIntoView() is a very handy function, when you use elements with a scrolling overflow. if some element is not visible, because it is scrolled away, you can scroll until it is visible. there is just one little problem. it is not standard (introduced originally by Microsoft for the document.all-object) and not working in old Opera- and Safari-browsers. this is why handgestrickt wrote a safe scrollIntoView()-function, which tries several ways to do it. this function needs Prototype.js to work properly. feel free to use it.

var foo = {
        scrollIntoView: function(frame,element) {
                if(frame && $(frame) && element && $(element)) {
                        try { $(element).scrollIntoView(false); }
                        catch(error) {
                                var difference = (
                                        (Position.positionedOffset($(element))[1]+$(element).getHeight())-
                                        (Position.positionedOffset($(frame))[1]+$(frame).getHeight())
                                );
                                try {
                                        if(difference > 0) $(frame).scrollTop = difference;
                                        else $(frame).scrollTop = 0;
                                }
                                catch(error2) {
                                        if(difference > 0) $(frame).scrollTo(0,difference);
                                        else $(frame).scrollTo(0,0);
                                }
                        }
                }
        },
}

Monday, 19. March 2007 • trackback url

Add Comment

( to reply to a comment, click the reply link next to the comment )

Comment Title:
Your Name:
Email Address:
Make Public?
Website:
Make Public?

Comment:


Allowed XHTML tags : a, b, i, strong, code, acrynom, blockquote, abbr. Linebreaks will be converted automatically.


Captcha:

captcha image

please type the content of the above image into the following form-field: