wow
wow

Reputation: 8239

How do I scroll to the last id with jQuery

I want when I click load more button content will automatic scroll to the last id. Hmmm

Example

$(document).ready(function(){
    $("#loadmorebutton").click(function (){
        $('#loadmorebutton').html('');
    $.ajax({
        url: "loadmore.php?lastid=" + $(".postitem:last").attr("id"),
        success: function(html){
            if(html){
                $("#postswrapper").append(html);
                $('#loadmorebutton').html('Load More');
            }else{
                $('#loadmorebutton').replaceWith('No more posts to show.');
            }
        }
    });
   });
});

Live Demo. On the demo I want, after click load more window will scroll to Post no 51 by example.

Upvotes: 0

Views: 306

Answers (2)

Hussein
Hussein

Reputation: 42818

On Button click, find out the window height var wh = $(window).height(); and apply it to window scrollTop $(window).scrollTop(wh) This will make whatever is on button of the page be first thing on top.

Upvotes: 1

S L
S L

Reputation: 14318

scrollTop() might be the function you might be requiring.

var height = 100;

$("#postswrapper").scrollTop(height)

Upvotes: 1

Related Questions