如果可能,滚动页面底部或结果

时间:2014-01-02 23:21:52

标签: php jquery ajax

提交后,如果页面太大,用户必须向下滚动才能看到foo.php给出的结果,我希望它强制浏览器向下滚动到结果消息,如果可能的话。我非常愿意做任何事情让页面自动向下滚动,不需要使用ajax。等

$(function(){
        $('button[type=submit]').click(function(e){
            e.preventDefault();
            $.ajax({
                type: "POST",
                url: "foo.php",
                data: $("#myform").serialize(),
                beforeSend: function(){
                    $('#result').html('<div class="success"><img src="../images/loading-blue.gif" width="25" /></div>');
                },
                success: function(data){
                    $('#result').html(data);
                    //var objDiv = document.getElementById("#result");
                    //objDiv.scrollTop = objDiv.scrollHeight; 
                    //(example I found online, didn't work)
                }
            });
        });
    });

4 个答案:

答案 0 :(得分:3)

在jQuery中,这应该是你的票。

$('html, body').animate({scrollTop:$(document).height()}, 'slow');

答案 1 :(得分:1)

使用javascriptresult是结果id

div的位置
document.getElementById('result').scrollIntoView();

答案 2 :(得分:0)

$("body").animate({scrollTop:$("#result").offset().top},500);

这将在$("#result")向右滚动。

为了更好的回答你还应该给我们#result的CSS,因为我们不知道它的位置(绝对的,固定的,静态的,相对的)

答案 3 :(得分:0)

如果结果确实是页面上的最后一个元素,那么对Rottingham的答案的改进就是这样做:

$("html, body").animate({scrollTop:$(document).height()-$(window).height(), "slow");

这样更好,因为第二个参数中指定的时间应该是窗口到达页面末尾所需的时间,而使用Rottingham的答案时,它会向下移动一点点。

相关问题