mituri

[JS] Intersection Observer

Jun 4th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 0.73 KB | None | 0 0
  1. <!-- link: https://jsfiddle.net/5rnLwcab/15/-->
  2.  
  3. <div class="container">
  4.   <div class="box" id="box-1">box 1</div>
  5.   <div class="box" id="box-2">box 2</div>
  6.   <div class="box" id="box-3">box 3</div>
  7.   <div class="box" id="box-4">box 4</div>
  8.   <div class="box" id="box-5">box 5</div>  
  9. </div>
  10.  
  11. <script>
  12. const io = new IntersectionObserver(entries => {
  13.     const io = new IntersectionObserver(entries => {
  14.     for (const entry of entries) {
  15.     console.log(`${entry.target.id} is in view ${entry.isIntersecting}`)
  16.   }
  17. })
  18.  
  19. document.querySelectorAll('.box').forEach(elem => {
  20.     io.observe(elem)
  21. })
  22. </script>
  23.  
  24. <style>
  25. .container {
  26.  min-height: 400px;
  27. }
  28.  
  29. .box {
  30.   background-color: steelblue;
  31.   margin-bottom: 200px;
  32. }
  33. </style>
Add Comment
Please, Sign In to add comment