Take User Back to Where They Scrolled to on previous page when clicking Browser Back Button

后端 未结 6 1136
半阙折子戏
半阙折子戏 2020-12-05 01:14

Is it possible to take a user back to the area of a page where they scrolled down to when pressing the back button in a browser? As in --- pageA is double your screen size

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 01:40

      //For Angular
      //Save your scroll position once you scroll
      @HostListener('window:scroll', ['$event']) onScrollEvent($event){
        sessionStorage.scrollPos = window.scrollY
      } 
    
      //Scroll to the save value postion
      ngAfterViewInit(){
        window.scrollTo(0, sessionStorage.scrollPos)
      }
    

提交回复
热议问题