Skip to content

Instantly share code, notes, and snippets.

@simonrw
Created December 27, 2017 23:02
Show Gist options
  • Save simonrw/679c1e4a308fd8c61b1e4f562eb3f2b8 to your computer and use it in GitHub Desktop.
Save simonrw/679c1e4a308fd8c61b1e4f562eb3f2b8 to your computer and use it in GitHub Desktop.
Scroll position saver
<!doctype html>
<html>
<head>
<meta charset="utf8">
<style>
div#main {
border: 1px solid black;
display: inline-block;
width: 1024px;
max-width: 1024px;
height: 1024px;
max-height: 1024px;
}
</style>
</head>
<body>
<div id="main"></div>
<script type="text/javascript">
class ScrollPoster {
constructor(url) {
this.url = url;
this.isScrolling = null;
this.init();
}
post(location, position) {
console.log('Posting: ', { href: location.href, pos: position });
}
init() {
var self = this;
window.addEventListener('scroll', function(e) {
window.clearTimeout(this.isScrolling);
this.isScrolling = setTimeout(function() {
const pos = window.scrollY;
self.post(window.location, pos);
}, 333);
}, false);
}
};
window.onload = () => {
const handler = new ScrollPoster('http://example.com');
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment