forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersection-observer-callback-timestamp.html
99 lines (87 loc) · 2.52 KB
/
intersection-observer-callback-timestamp.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE HTML>
<html>
<head>
<script src="../resources/js-test-pre.js"></script>
<style>
#scrollable {
width: 100px;
height: 100px;
border: 1px solid black;
overflow-y: auto;
}
#container {
width: 100px;
height: 200px;
}
#box {
position: relative;
top: 100px;
width: 100px;
height: 100px;
background-color: green;
}
</style>
</head>
<body>
<div id="scrollable">
<div id="container">
<div id="box"></div>
</div>
</div>
<script>
var animateTimestamp = 0;
var intersectTimestamp = 0;
var MaxFrames = 2;
var currentFrame = 0;
function scrollBox()
{
var scrollable = document.querySelector("#scrollable");
var height = scrollable.clientHeight;
scrollable.scroll(0, (height / MaxFrames) * currentFrame);
document.body.offsetHeight;
}
function handleAnimate(timestamp)
{
if (++currentFrame > MaxFrames) {
finishJSTest();
return;
}
animateTimestamp = timestamp;
scrollBox();
requestAnimationFrame(handleAnimate);
}
function handleIntersect(entries, observer)
{
entries.forEach((entry) => {
intersectTimestamp = entry.time;
shouldBe("Math.round(animateTimestamp)", "Math.round(intersectTimestamp)");
});
}
function buildThresholdList()
{
let thresholds = [];
for (let i = 1.0; i <= MaxFrames; i++) {
let ratio = i / MaxFrames;
thresholds.push(ratio);
}
return thresholds;
}
function createObserver()
{
let observer;
let options = {
root: document.querySelector("#scrollable"),
rootMargin: "0px",
threshold: buildThresholdList()
};
observer = new IntersectionObserver(handleIntersect, options);
observer.observe(document.querySelector("#box"));
}
window.jsTestIsAsync = true;
description("Test the IntersectionObserver timestamp is the same as the timestamp of the rAF callback.");
createObserver();
requestAnimationFrame(handleAnimate);
</script>
<script src="../resources/js-test-post.js"></script>
</body>
</html>