observe-box-inside-container-with-scrollable-overflow.html 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <!DOCTYPE html>
  2. <style>
  3. .scroll-box {
  4. width: 300px;
  5. height: 300px;
  6. overflow: scroll;
  7. border: 2px solid black;
  8. }
  9. .content-box {
  10. width: 100%;
  11. height: 600px;
  12. position: relative;
  13. }
  14. .target-box {
  15. width: 100px;
  16. height: 100px;
  17. background-color: red;
  18. position: absolute;
  19. top: 500px;
  20. }
  21. </style>
  22. <script src="../include.js"></script>
  23. <div class="scroll-box"><div class="content-box"><div class="target-box"></div></div></div>
  24. <script>
  25. asyncTest(done => {
  26. function onIntersection(entries) {
  27. entries.forEach((entry) => {
  28. if (entry.isIntersecting) {
  29. println("The box is now visible.");
  30. done();
  31. } else {
  32. println("The box is not visible.");
  33. }
  34. });
  35. }
  36. let observer = new IntersectionObserver(onIntersection, {
  37. root: document.querySelector(".scroll-box"),
  38. });
  39. observer.observe(document.querySelector(".target-box"));
  40. setTimeout(() => {
  41. document.querySelector(".scroll-box").scrollTop = 1000;
  42. }, 100);
  43. });
  44. </script>