Javascript 将窗口从当前位置移到顶部

Javascript 将窗口从当前位置移到顶部,javascript,jquery,Javascript,Jquery,我有一个用于文本查看的折叠按钮,除了jQuery动画功能外,它工作得很好 当我点击一个按钮时,窗口移到底部,然后移到顶部。 如果我删除一行(隐藏部分文本),一切正常(不移到底部) 有没有什么办法可以让它在不首先触底的情况下工作 源代码: $('.collapse_btn').click(function() { $('.r1').css({'max-height':r1h_nh}); $(this).hide(); $('.expand_btn').show();

我有一个用于文本查看的折叠按钮,除了jQuery动画功能外,它工作得很好

当我点击一个按钮时,窗口移到底部,然后移到顶部。 如果我删除一行(隐藏部分文本),一切正常(不移到底部)

有没有什么办法可以让它在不首先触底的情况下工作

源代码:

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

由于元素
.collapse\u btn
消失,您的窗口似乎被滚动到底部。所以,如果在使用滚动窗口到顶部后执行隐藏/显示操作,会怎么样

我想这会让你更好地理解
   $('.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();
          });
        });