Restoring page scroll position with jQuery

后端 未结 6 1351
春和景丽
春和景丽 2020-12-05 17:21

I have a page operation that uses something like:

$(\'#thetable tbody\').replaceWith(newtbody);

in an ajax callback. Sometimes, if the user

6条回答
  •  北海茫月
    2020-12-05 17:55

    You could periodically check the div outerHeight, and set the scrollTop property once the div rendering has crossed the scroll value.

    var g_Interval ;
    var g_ScrollTop ;
    
    function checkAndAdjustScroll() {
        var height=$('#content').outerHeight() ;
        if(height>g_ScrollTop) {
                $(window).scrollTop(g_ScrollTop) ;
                clearInterval(g_Interval) ;
        }
    }
    

    And whenever you update your div with AJAX content, do this

    g_ScrollTop=$(window).scrollTop() ;
    g_Interval=setInterval(checkAndAdjustScroll, 100) ;
    

    You might need to make adjustments based on the offset of your div from the top of the page

提交回复
热议问题