Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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