简体   繁体   中英

div scrolls to top after refresh need to stop auto scroll to the top

I am trying to get a div that refreshes every 2 seconds to stop scrolling back to the top after the 2 second refresh I have PHP code and javascript. The Javascript I am using is:

function at_Ticket_scrollBottom()
{
   var objDiv = document.getElementById("cartTicket");
   objDiv.scrollTop = objDiv.scrollHeight;
}

function at_Tabs_Update()
{

if(div_WPOSVar_IsVisible())
{
    //calling setTimeout without clearing any existing timeout can add multiple calls.
    //IE the normal 2 second sequence, then call at_Tabs_Update two more times, and
    //     now we have 3 timeouts set to call at_Tabs again, etc.
    //This wouldn't be an issue except that we call at_Tabs_Update directly to cause
    //     immediate refresh from many places.
    //So clear the handle everytime to get rid of the last one we set.
    clearTimeout(at_Tabs_Timer);
    at_Tabs_Timer=setTimeout("at_Tabs_Update()", 2*1000); //every 2 seconds
    return;
}
}

So after the refresh if I scroll down to the bottom of the ticket it jumps back to the top after the next refresh so I can never get to the bottom and select an item and edit it before the refresh how do I stop the auto scroll back to the top.

from the scars infos I can gather here I think your best bet would be to save your current scroll position before you refresh and after the ajax call scroll to that saved position. use jQuerys .scrollTop() function for both reading and setting the scroll.

some pseudo code for illustration:

at ajax refresh function
    var curPos $(element).scrollTop();
    ... do ajax call ..
        ajax callback: $(element).scrollTop(curPos);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM