summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-scroll-anchoring/adjustment-followed-by-scrollBy.html
blob: 7428147b83562412903b8f9bfa76d37879a400ba (plain)
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
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="author" title="Mozilla" href="https://mozilla.org">
<link rel="help" href="https://drafts.csswg.org/css-scroll-anchoring/">
<link rel="help" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1874551">
<style>
div {
  height: round(20vh, 1px);
}
</style>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div id="center" style="height: 100px; background-color: blue;"></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<script>
promise_test(async t => {
  assert_equals(window.scrollY, 0);

  // Center an element.
  center.scrollIntoView({ block: "center" }, { behavior: "instant" });
  await new Promise(resolve => window.addEventListener("scroll", resolve));

  const originalPosition = center.getBoundingClientRect().top;

  // Given that now the element is centered, one of div elements above the
  // centered element is used as the scroll anchor node. Hide two div elements,
  // the first div and the div element just above the centered one to move the
  // anchor node, thus it will trigger scroll anchoring adjustment.
  center.previousElementSibling.style.display = "none";
  document.querySelectorAll("div")[0].style.display = "none";

  // And adjust the scroll position where the centered element is still
  // positioned at the center of the scroll container.
  window.scrollBy(0, center.getBoundingClientRect().top - originalPosition);
  await new Promise(resolve => window.addEventListener("scroll", resolve));

  const centeredPosition = center.getBoundingClientRect().top;

  // Now try to scrollIntoView({ block: "center" }) and make sure the position
  // is unchanged.
  center.scrollIntoView({ block: "center" }, { behavior: "instant" });

  // Wait two frames to be able to scroll if it's possible.
  await new Promise(resolve => requestAnimationFrame(resolve));
  await new Promise(resolve => requestAnimationFrame(resolve));

  assert_equals(centeredPosition, center.getBoundingClientRect().top);
}, "Scroll anchoring followed by scrollBy call");
</script>
</html>