javascript - Return to scroll position after the page has been scrolled to top -


i trying make sidebar appear when relevant button clicked, on small devices, i'm facing difficulties.

i found way toggle class make dis/appear on click , automatically scroll page top once done, top of sidemenu shown viewers regardless of part of page viewing @ moment. however, find quite hard combine these way make page return scroll point visitor at, after sidemenu has disappeared. me this?

html:

<div class="col-md-2" role="navigation">     <ul class="nav" id="sidemenu">       <li><a href="#">lorem ipsum dolo</a></li>       <li><a href="#">lorem ipsum dolo</a></li>       <li><a href="#">lorem ipsum dolo</a></li>            </ul> </div>  

js:

$('#sidebutton').click(function() { $('#sidemenu').toggleclass("current") && $('html,body').scrolltop(0); $('.nodisplay').toggleclass("hidden"); }); 

css (just in case):

 #sidemenu, .hidden {  display: none; }  #sidemenu.current { display:block; } 

when menu hidden, save current scroll position somewhere (local/sessionstorage, variable, ever) , scroll top. when menu not hidden, scroll position saved:

var scrollto; var prevscrolpos;  $('#sidebutton').click(function () {      if (!$('#sidemenu').hasclass("current")) {         // if menu hidden, save current scroll position , scroll top         prevscrolpos = $('body').scrolltop();         scrollto = 0;     } else {         // if menu not hidden, scroll saved position         scrollto = prevscrolpos;     }      $('#sidemenu').toggleclass("current") && $('html,body').scrolltop(scrollto);     $('.nodisplay').toggleclass("hidden");  }); 

a demo: http://jsfiddle.net/o7btkgog/


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -