scroll.js 839 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* Continuously scrolls the page
  2. */
  3. (function(){
  4. class Scroll {
  5. constructor (options) {
  6. this.scrolled = new Map ();
  7. this.interval = window.setInterval (this.scroll.bind (this), 200);
  8. }
  9. stop() {
  10. window.clearInterval (this.interval);
  11. window.scrollTo (0, 0);
  12. this.scrolled.forEach (function (value, key, map) {
  13. key.scrollTop = value;
  14. });
  15. }
  16. /* save initial scroll state */
  17. save(obj) {
  18. if (!this.scrolled.has (obj)) {
  19. this.scrolled.set (obj, obj.scrollTop);
  20. }
  21. }
  22. /* perform a single scroll step */
  23. scroll (event) {
  24. window.scrollBy (0, window.innerHeight/2);
  25. document.querySelectorAll ('html body *').forEach (
  26. function (d) {
  27. if (d.scrollHeight-d.scrollTop > d.clientHeight) {
  28. this.save (d);
  29. d.scrollBy (0, d.clientHeight/2);
  30. }
  31. }.bind (this));
  32. return true;
  33. }
  34. }
  35. return Scroll;
  36. }())