burjulius
burjulius

Reputation: 289

Move window to top from current position

I have a collapse button for text viewing which works fine expect jQuery animate function.

When I click on a button, window moves to the bottom and then moves to the top. If I remove one line (which hides part of the text) everything works fine (without moving to the bottom).

Any ideas how to make it work without moving to the bottom first?

Source code:

$('.collapse_btn').click(function() {
    $('.r1').css({'max-height':r1h_nh});
    $(this).hide();
    $('.expand_btn').show();
    $('html, body').animate({scrollTop:0}, 'slow');
});

Upvotes: 1

Views: 876

Answers (1)

shershen
shershen

Reputation: 10003

Seems that your window is scrolled to bottom because element .collapse_btn disappeas. So what if you do the hide/show operation after you scroll window to top using jQuery.animate callback?

   $('.collapse_btn').click(function() {
        $('html, body').animate({scrollTop:0}, 'slow', 'swing', function(){
//scrollTop is done, now perform what you need, it won't be visible anyway
          $('.r1').css({'max-height':r1h_nh});
             $('.collapse_btn').hide();
             $('.expand_btn').show();
          });
        });

Upvotes: 2

Related Questions