growing-too-late.html 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Too late</title>
  7. <style>
  8. body { margin: 0; font: 16px/1.5 system-ui, sans-serif; color: #111; background: #fff; }
  9. section { min-height: 100vh; padding: 32px 48px; box-sizing: border-box; border-bottom: 1px solid #ddd; }
  10. .fine { font-size: 9px; }
  11. </style>
  12. </head>
  13. <body>
  14. <main id="list"><section><h1>Slow list</h1><p>Reaching the bottom starts a request; one more screen arrives 900ms later, after the recheck.</p></section><section><p>Second screen.</p></section></main>
  15. <script>
  16. // A finite load that lands after the scroll has already reached the bottom, as a network request would.
  17. const list = document.getElementById("list");
  18. let asked = false;
  19. addEventListener("scroll", () => {
  20. if (asked || scrollY + innerHeight < document.documentElement.scrollHeight - 4) return;
  21. asked = true;
  22. setTimeout(() => {
  23. const s = document.createElement("section");
  24. s.innerHTML = "<p>Third screen.</p>";
  25. list.append(s);
  26. // Reveal-on-scroll: the footnote exists only once the new screen has been scrolled into view.
  27. new IntersectionObserver(([e], o) => {
  28. if (!e.isIntersecting) return;
  29. o.disconnect();
  30. s.insertAdjacentHTML("beforeend", '<p class="fine">Late footnote.</p>');
  31. }).observe(s);
  32. }, 900);
  33. });
  34. </script>
  35. </body>
  36. </html>