Created
December 27, 2017 23:02
-
-
Save simonrw/679c1e4a308fd8c61b1e4f562eb3f2b8 to your computer and use it in GitHub Desktop.
Scroll position saver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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