简体   繁体   English

返回动态大小的页面上的滚动条位置

[英]Go back to the scroll bar position on a dynamically sized page

I have a webpage that will dynamically load each time it's refreshed. 我有一个网页,每次刷新时都会动态加载。 If a user is editing an entry in the webpage, drills into a link for example. 如果用户正在编辑网页中的条目,则钻取到链接中。 Then tries to press the back button, or hit a tab corresponding to the previous page also, how can I save the vertical scroll bar position and return them to the same position when clicking either the back button or the tab corresponding to the same page. 然后尝试按后退按钮,或按对应于先前页面也一个标签,我怎么能节省垂直滚动条的位置,并将其返回到相同的位置单击任意一个后退按钮或对应于同一页面的标签时。

I've tried this, but it works for only pages that aren't dynamically loaded. 我试过这个,但它只适用于非动态加载的页面。 How can I retain the scroll position of a scrollable area when pressing back button? 按下后退按钮时,如何保留可滚动区域的滚动位置?

You could use the same function in the question you linked but listen to the scroll event as opposed to the page unload event. 您可以在链接的问题中使用相同的功能,但是听取滚动事件而不是页面卸载事件。 That way the scroll position will be updated and stored every time the user scrolls. 这样,每次用户滚动时,滚动位置将被更新和存储。

Since the page is loaded dynamically, you can trigger an event once the content is loaded, that will cause the page to scroll: 由于页面是动态加载的,因此您可以在加载内容后触发事件,这将导致页面滚动:

$.get('/resource').done(function(){
    // Render...
    // Add a trigger after the content is loaded and rendered
    $(window).trigger('content:loaded');
}

var prevScrollHorizontal = 0;
var prevScrollVertical = 0;

$(function() {
   $("div#element").scroll(function() { // Listens for scroll events

      var currentHorizontal = $(this).scrollLeft();
      var currentVertical = $(this).scrollTop();

      if(prevScrollHorizontal != currentHorizontal) {
          prevScrollHorizontal = currentHorizontal;
          localStorage.setItem("scrollPositionHorizontal", currentHorizontal);
          // Scrolled horizontally
      }

      if(prevScrollVertical != currentVertical) {
          prevScrollVertical = currentVertical;
          localStorage.setItem("scrollPositionVertical", currentVertical);
          // Scrolled vertically
      }

   });

   // Listen for the 'content:loaded' event triggered above 
   $(window).on('content:loaded', function() {       
       if(localStorage.scrollPositionVertical) {                 
          $("div#element").scrollTop(localStorage.getItem("scrollPositionVertical"));
       }
       if(localStorage.scrollPositionHorizontal) {                 
          $("div#element").scrollLeft(localStorage.getItem("scrollPositionHorizontal"));
       }
    });
});

You can separate the different stored scroll objects using the window.location.pathname value to differentiate them: 您可以使用window.location.pathname值分隔不同的存储滚动对象以区分它们:

$(function() {
   $(window).scroll(function() { // Listens for scroll events
      var scrollPosition = $("div#element").scrollTop();
      // Uses the pathname to set the scroll value
      localStorage.setItem("scrollPosition_"+window.location.pathname, scrollPosition);
   });

   if(localStorage.scrollPosition) {
      // Uses the pathname to get the scroll value   
      $("div#element").scrollTop(localStorage.getItem("scrollPosition_"+window.location.pathname)); 
   }
});

Read more about the scroll event here . 在此处阅读有关滚动事件的更多信息
and more about jQuery trigger() here 还有更多关于jQuery trigger() 信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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