isIntersecting-change-events.html (3904B)
1 <!DOCTYPE html> 2 <meta name="viewport" content="width=device-width,initial-scale=1"> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="./resources/intersection-observer-test-utils.js"></script> 6 7 <style> 8 pre, #log { 9 position: absolute; 10 top: 0; 11 left: 200px; 12 } 13 #root { 14 position: absolute; 15 top: 0; 16 left: 0; 17 width: 150px; 18 height: 200px; 19 overflow-y: scroll; 20 } 21 #target1, #target2, #target3, #target4 { 22 width: 100px; 23 height: 100px; 24 } 25 #target1 { 26 background-color: green; 27 } 28 #target2 { 29 background-color: red; 30 } 31 #target3 { 32 background-color: blue; 33 } 34 #target4 { 35 background-color: yellow; 36 } 37 </style> 38 39 <div id="root"> 40 <div id="target1"></div> 41 <div id="target2"></div> 42 <div id="target3"></div> 43 </div> 44 45 <script> 46 var entries = []; 47 var observer; 48 49 runTestCycle(function() { 50 var root = document.getElementById('root'); 51 var target1 = document.getElementById('target1'); 52 var target2 = document.getElementById('target2'); 53 var target3 = document.getElementById('target3'); 54 assert_true(!!root, "root element exists."); 55 assert_true(!!target1, "target1 element exists."); 56 assert_true(!!target2, "target2 element exists."); 57 assert_true(!!target3, "target3 element exists."); 58 observer = new IntersectionObserver(function(changes) { 59 entries = entries.concat(changes); 60 }, { root: root }); 61 observer.observe(target1); 62 observer.observe(target2); 63 observer.observe(target3); 64 entries = entries.concat(observer.takeRecords()); 65 assert_equals(entries.length, 0, "No initial notifications."); 66 runTestCycle(step0, "Rects in initial notifications should report initial positions."); 67 }, "isIntersecting changes should trigger notifications."); 68 69 function step0() { 70 assert_equals(entries.length, 3, "Has 3 initial notifications."); 71 checkRect(entries[0].boundingClientRect, [0, 100, 0, 100], "Check 1st entry rect"); 72 assert_equals(entries[0].target.id, 'target1', "Check 1st entry target id."); 73 checkIsIntersecting(entries, 0, true); 74 checkRect(entries[1].boundingClientRect, [0, 100, 100, 200], "Check 2nd entry rect"); 75 assert_equals(entries[1].target.id, 'target2', "Check 2nd entry target id."); 76 checkIsIntersecting(entries, 1, true); 77 checkRect(entries[2].boundingClientRect, [0, 100, 200, 300], "Check 3rd entry rect"); 78 assert_equals(entries[2].target.id, 'target3', "Check 3rd entry target id."); 79 checkIsIntersecting(entries, 2, true); 80 runTestCycle(step1, "Set scrollTop=100 and check for no new notifications."); 81 root.scrollTop = 100; 82 } 83 84 function step1() { 85 assert_equals(entries.length, 3, "Has 3 total notifications because isIntersecting did not change."); 86 runTestCycle(step2, "Add 4th target."); 87 root.scrollTop = 0; 88 var target4 = document.createElement('div'); 89 target4.setAttribute('id', 'target4'); 90 root.appendChild(target4); 91 observer.observe(target4); 92 } 93 94 function step2() { 95 assert_equals(entries.length, 4, "Has 4 total notifications because 4th element was added."); 96 checkRect(entries[3].boundingClientRect, [0, 100, 300, 400], "Check 4th entry rect"); 97 assert_equals(entries[3].target.id, 'target4', "Check 4th entry target id."); 98 checkIsIntersecting(entries, 3, false); 99 assert_equals(entries[3].intersectionRatio, 0, 'target4 initially has intersectionRatio of 0.'); 100 runTestCycle(step3, "Set scrollTop=100 and check for one new notification."); 101 root.scrollTop = 100; 102 } 103 104 function step3() { 105 assert_equals(entries.length, 5, "Has 5 total notifications."); 106 checkRect(entries[4].boundingClientRect, [0, 100, 200, 300], "Check 5th entry rect"); 107 assert_equals(entries[4].target.id, 'target4', "Check 5th entry target id."); 108 checkIsIntersecting(entries, 4, true); 109 assert_equals(entries[4].intersectionRatio, 0, 'target4 still has intersectionRatio of 0.'); 110 root.scrollTop = 0; // reset to make it easier to refresh and run the test 111 } 112 113 </script>