summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-scroll-snap/scroll-snap-type.html
blob: 1577aa7afc66571dd010da4103c0ccd5eaee3cc5 (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
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
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-scroll-snap/#scroll-snap-type" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<style>
div {
  position: absolute;
  margin: 0px;
}
#scroller {
  height: 400px;
  width: 400px;
  overflow: scroll;
}
#space {
  width: 2000px;
  height: 2000px;
}

.snap {
  width: 200px;
  height: 200px;
  background-color: blue;
  scroll-snap-align: start;
}
#left-top {
  left: 0px;
  top: 0px;
}
#right-bottom {
  left: 1000px;
  top: 1000px;
}
</style>

<div id="scroller">
  <div id="space"></div>
  <div class="snap" id="left-top"></div>
  <div class="snap" id="right-bottom"></div>
</div>

<script>
var scroller = document.getElementById("scroller");
var visible_x = 1000 - scroller.clientWidth;
var visible_y = 1000 - scroller.clientHeight;

test(() => {
  scroller.style.scrollSnapType = "both mandatory";
  scroller.scrollTo(0, 0);
  assert_equals(scroller.scrollLeft, 0);
  assert_equals(scroller.scrollTop, 0);

  scroller.scrollTo(visible_x + 10, visible_y + 10);
  assert_equals(scroller.scrollLeft, 1000);
  assert_equals(scroller.scrollTop, 1000);
}, "mandatory scroll-snap-type should snap as long as the element is visible.");

test(() => {
  scroller.style.scrollSnapType = "both proximity";
  scroller.scrollTo(0, 0);
  assert_equals(scroller.scrollLeft, 0);
  assert_equals(scroller.scrollTop, 0);

  scroller.scrollTo(visible_x + 10, visible_y + 10);
  assert_equals(scroller.scrollLeft, visible_x + 10);
  assert_equals(scroller.scrollTop, visible_y + 10);
}, "proximity scroll-snap-type shouldn't snap if the snap position is too far away.");

test(() => {
  scroller.style.scrollSnapType = "both proximity";
  scroller.scrollTo(0, 0);
  assert_equals(scroller.scrollLeft, 0);
  assert_equals(scroller.scrollTop, 0);

  scroller.scrollTo(995, 995);
  assert_equals(scroller.scrollLeft, 1000);
  assert_equals(scroller.scrollTop, 1000);
}, "proximity scroll-snap-type should snap if the snap position is close.");

test(_ => {
  scroller.style.scrollSnapType = "none";
  scroller.scrollTo(100, 100);
  assert_equals(scroller.scrollLeft, 100, "scrolling should not snap");
  assert_equals(scroller.scrollTop, 100, "scrolling should not snap");
}, "none scroll-snap-type shouldn't snap.");
</script>