Save to locationStorage position to maintain when reloading page

1

When the user clicks on a particular button, the scroll position of an element changes. I would like this action to be stored in LocalStorage, but I do not know how to do it ... can you help me?

Here's my script:

$('.btn.parteum').click(function(){ 
      $('#textarea').animate({ scrollLeft: 0 }, 400);
      return false; 
});
$('.btn.parte2').click(function(){ 
      $('#textarea').animate({ scrollLeft: 140 }, 400);
      return false; 
});

example: link

    
asked by anonymous 06.12.2018 / 06:07

1 answer

3

You can add a callback that will be called at the end of the animation:

//recupera o valor salvo ao recarregar a pagina
if(localStorage.getItem('scrollposition')){
     $('#textarea').scrollLeft(localStorage.getItem('scrollposition'));
}

$('.btn.parteum').click(function(){ 
      $('#textarea').animate({ scrollLeft: 0 }, 400, function(){
          localStorage.setItem('scrollposition', 0);
      });
      return false; 
});
$('.btn.parte2').click(function(){ 
      $('#textarea').animate({ scrollLeft: 140 }, 400, function(){
          localStorage.setItem('scrollposition', 140);
      });
      return false; 
});

Example working : link

  

link

    
06.12.2018 / 11:19