views:

18

answers:

1

When the user clicks a certain button at the bottom of the site I am working on, it expands into a comment form. If there is a lot of other content on the page, the comment form will expand below the fold and the user might not notice that it has appeared.

I would like to scroll the page down as the comment form expands to draw the user's attention to it. I looked at this question, but the solution doesn't work because my form animates as it expands, so the scrolling stops with only the top of the form visible. Also, jumping around looks ugly.

Here is my current code.

$("#show").click(function() {
    if (!$("#change-form").is(":visible")) {
        $("#change-form").show("blind",{},500);
        $(document).animate({scrollTop: $(document).height()}, 1000);
    }
    return false;
});

The $(document).animate line is very similar to what I've found in the jQuery documentation, but the page doesn't scroll at all. I've tested this in Chrome 5.0.375.125 and Firefox 3.6.8 with no success, although I've read that $(document) is supposed to work in both browsers. Why won't the page scroll?

A: 

Have you tried scrollTo? http://plugins.jquery.com/project/ScrollTo

Jon McIntosh
I did, and that scrolls, but it's scrolling to where the end of the document *used* to be, not following the moving target. I can set the scrolling amount to something like 150% and the scroll speed to 2000, but that seems to work very sporadically. Any specific suggestions for how to solve this with that library?
James
So just to understand what you're saying, you want the page to scroll to the end of the expanded content? If this is an uncertain position, just simply have the scrollTo command scroll to a hidden element at the end of the content.
Jon McIntosh
That works in Firefox, but Chrome still has the same issue. I've solved it by having using callback function that scrolls down. That way the form is animated if the user can see it, and it scrolls down smoothly using $(document).scrollTo("100%", 250); if the user can't.Thanks for the tip!
James