├── demo.html ├── README.md ├── LICENSE └── scrollpersist.js /demo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |

10 | Try scrolling down the page then close your browser tab and open it again. 11 |

12 | 13 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | scrollpersist 2 | ============= 3 | 4 | Persist page scrolling to last position when user returns back to a visited page. 5 | No dependencies are required to run. 6 | 7 | Simply include: 8 | ``` 9 | 10 | ``` 11 | anywhere on your page and the user will resume from their last scroll position when they return to that page. 12 | 13 | Try the [online demo](https://rawgit.com/simon-thorpe/scrollpersist/master/demo.html) 14 | 15 | 16 | ### Optional Addons 17 | For smooth animated scrolling you can also include jQuery and jQuery.scrollTo on your page too: 18 | ``` 19 | 20 | 21 | 22 | ``` 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 simon-thorpe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /scrollpersist.js: -------------------------------------------------------------------------------- 1 | /* 2 | * scrollpersist v1.2.0 3 | * 4 | * https://github.com/simon-thorpe/scrollpersist/ 5 | * 6 | * Include this script on any page that requires persistant scrolling when the user returns to it: 7 | * 8 | * 9 | */ 10 | (function () { 11 | var key = '__scrollpersist_' + btoa(window.location.href); 12 | var currentScript = document.currentScript; 13 | var delay = null; 14 | delay = parseInt(currentScript.getAttribute('data-delay')); 15 | var scroll = function () { 16 | var oldTop = localStorage[key + '_top']; 17 | var oldLeft = localStorage[key + '_left']; 18 | if (oldTop || oldLeft) { 19 | if (typeof (jQuery) !== 'undefined' && typeof (jQuery.scrollTo) !== 'undefined') { 20 | jQuery.scrollTo({ top: oldTop, left: oldLeft }, 1000); 21 | } 22 | else { 23 | window.scrollTo(oldLeft, oldTop); 24 | } 25 | } 26 | }; 27 | window.addEventListener('scroll', function () { 28 | var doc = document.documentElement; 29 | var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0); 30 | var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0); 31 | localStorage[key + '_top'] = top; 32 | localStorage[key + '_left'] = left; 33 | }); 34 | document.addEventListener('DOMContentLoaded', function () { 35 | if (delay) { 36 | setTimeout(scroll, delay); 37 | } 38 | else { 39 | scroll(); 40 | } 41 | }); 42 | 43 | // Global function. 44 | window.scrollpersist = scroll; 45 | })(); 46 | --------------------------------------------------------------------------------