Guest User

Untitled

a guest
Apr 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=utf-8>
  5. <style>
  6. #root {
  7. position: absolute;
  8. left: 50px;
  9. top: 50px;
  10. width: 100px;
  11. height: 100px;
  12. background-color: yellow;
  13. }
  14.  
  15. #d1 {
  16. position: absolute;
  17. left: -12px;
  18. top: 0px;
  19. width: 10px;
  20. height: 10px;
  21. background-color: blue;
  22. }
  23.  
  24. #output {
  25. position: absolute;
  26. top: 160px;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="root">
  32. <div id="d1"></div>
  33. </div>
  34.  
  35. <pre id="output"></pre>
  36. <script>
  37. let output = document.getElementById("output");
  38. let d1 = document.getElementById("d1");
  39.  
  40. function log(message) {
  41. output.textContent += message + '\n';
  42. }
  43.  
  44. let obs = new IntersectionObserver(records => {
  45. if (records.lenth == 0)
  46. log("no records")
  47. else
  48. log(`record[0]: ${records[0].isIntersecting} (${records[0].intersectionRatio})`);
  49. }, { root: document.getElementById("root"), threshold: [.5] });
  50. obs.observe(d1);
  51.  
  52. let d1pos = -11;
  53. function budge() {
  54. d1pos += 1;
  55. d1.style.left = `${d1pos}px`;
  56. }
  57.  
  58. let ival = setInterval(() => {
  59. budge();
  60. if (d1pos > 10) {
  61. clearInterval(ival);
  62. }
  63. }, 100);
  64. </script>
  65. </body>
  66. </html>
Add Comment
Please, Sign In to add comment