summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-scroll-snap/overflowing-snap-areas.html
blob: 08aff5ebaebf71d7a4683dd53aa2b5d9a14a8f30 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-scroll-snap-1/#scroll-snap-type" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
div {
  position: absolute;
}
.scroller-x {
  overflow: scroll;
  scroll-snap-type: x mandatory;
  width: 500px;
  height: 500px;
}
.scroller-y {
  overflow: scroll;
  scroll-snap-type: y mandatory;
  width: 500px;
  height: 500px;
}
.space {
  width: 4000px;
  height: 4000px;
}
.target {
  scroll-snap-align: start;
  height: 400px;
  width: 400px;
}
.large-x {
  width: 3000px;
  background-color: yellow;
}
.large-y {
  height: 2000px;
  background-color: yellow;
}
.small {
  height: 200px;
  width: 200px;
  background-color: black;
}
</style>
<div class="scroller-x" id="one-target">
  <div class="space"></div>
  <div class="large-x target" id="single" style="left: 200px;"></div>
</div>

<div class="scroller-x" id="x">
  <div class="space"></div>
  <div style="left: 200px;">
    <div class="target large-x"></div>
    <div class="target small" style="left: 200px"></div>
    <div class="target small" style="left: 600px"></div>
    <div class="target small" style="left: 1200px"></div>
  </div>
</div>

<div class="scroller-y" id="y">
  <div class="space"></div>
  <div style="top: 200px;">
    <div class="target large-y"></div>
    <div class="target small" style="top: 200px"></div>
    <div class="target small" style="top: 600px"></div>
    <div class="target small" style="top: 1200px"></div>
    <div class="target large-y" style="top: 2000px"></div>
  </div>
</div>

<div class="scroller-x" id="two-axes" style="scroll-snap-type: both mandatory">
  <div class="space"></div>
  <div class="target large-x" style="top: 200px"></div>
</div>

<div class="scroller-x" id="overlapping-overflow" style="scroll-snap-type: both mandatory">
  <div class="space"></div>
  <div style="left: 200px; top: 200px;">
    <div class="target small"></div>
    <div class="target small"></div>
    <div class="target small"></div>
    <div class="target large-y large-x"></div>
    <div class="target small"></div>
    <div class="target small"></div>
    <div class="target small"></div>
  </div>
</div>

<script>
var one_target_scroller = document.getElementById("one-target");
var scroller_x = document.getElementById("x");
var scroller_y = document.getElementById("y");
var two_axes_scroller = document.getElementById("two-axes");
var overlapping_scroller = document.getElementById("overlapping-overflow");

test(() => {
  one_target_scroller.scrollTo(10, 0);
  assert_equals(one_target_scroller.scrollLeft, 200);
  assert_equals(one_target_scroller.scrollTop, 0);
}, "Snaps to the snap position if the snap area doesn't cover the snapport on x.");

test(() => {
  var right_align = 3200 - one_target_scroller.clientWidth;
  one_target_scroller.scrollTo(right_align, 0);
  assert_equals(one_target_scroller.scrollLeft, right_align);
  assert_equals(one_target_scroller.scrollTop, 0);
}, "Snaps to the snap position if the snap area covers the snapport on x on the right border.");

// We use end alignment for this test so that we don't fall on a snap
// position when the snap area just covers the snapport on the left border.
test(() => {
  document.getElementById("single").style.scrollSnapAlign = 'end';
  one_target_scroller.scrollTo(200, 0);
  assert_equals(one_target_scroller.scrollLeft, 200);
  assert_equals(one_target_scroller.scrollTop, 0);
}, "Snaps to the snap position if the snap area covers the snapport on x on the left border.");

test(() => {
  scroller_x.scrollTo(500, 0);
  assert_equals(scroller_x.scrollLeft, 400);
  assert_equals(scroller_x.scrollTop, 0);
}, "Snaps if the distance between the previous(400) and next(800) " +
   "snap positions is smaller than snapport(500) on x.");

test(() => {
  scroller_y.scrollTo(0, 500);
  assert_equals(scroller_y.scrollLeft, 0);
  assert_equals(scroller_y.scrollTop, 400);
}, "Snaps if the distance between the previous(400) and next(800) " +
   "snap positions is smaller than snapport(500) on y.");

test(() => {
  scroller_x.scrollTo(900, 0);
  assert_equals(scroller_x.scrollLeft, 900);
  assert_equals(scroller_x.scrollTop, 0);
}, "Snap to current scroll position which is a valid snap position, " +
   "as the snap area covers snapport on x and the distance between the " +
   "previous(800) and next(1400) is larger than snapport(500).");

test(() => {
  scroller_y.scrollTo(0, 900);
  assert_equals(scroller_y.scrollLeft, 0);
  assert_equals(scroller_y.scrollTop, 900);
}, "Snap to current scroll position which is a valid snap position, " +
   "as the snap area covers snapport on y and the distance between the " +
   "previous(800) and next(1400) is larger than snapport(500).");

test(() => {
  scroller_x.scrollTo(1500, 0);
  assert_equals(scroller_x.scrollLeft, 1500);
  assert_equals(scroller_x.scrollTop, 0);
}, "Snap to current scroll position which is a valid snap position, as the " +
   "snap area covers snapport on x and there is no subsequent snap positions.");

test(() => {
  scroller_y.scrollTo(0, 1500);
  assert_equals(scroller_y.scrollLeft, 0);
  assert_equals(scroller_y.scrollTop, 1500);
}, "Snap to current scroll position which is a valid snap position, as the " +
   "snap area covers snapport on y and there is no subsequent snap positions.");

test(() => {
  const maxScrollTop = scroller_y.scrollHeight - scroller_y.clientHeight;

  // Scroll to the bottom edge which is a valid snap position that a large
  // target element covers the snapport.
  scroller_y.scrollTo(0, maxScrollTop);
  assert_equals(scroller_y.scrollTop, maxScrollTop);

  // Scroll to `the bottom edge + 1`.
  scroller_y.scrollTo(0, maxScrollTop + 1);
  assert_equals(scroller_y.scrollTop, maxScrollTop);
}, "Don't snap back even if scrollTo tries to scroll to positions which are " +
   "outside of the scroll range and if a snap target element covers the snaport");

test(() => {
  two_axes_scroller.scrollTo(10, 100);
  assert_equals(two_axes_scroller.scrollLeft, 10);
  assert_equals(two_axes_scroller.scrollTop, 200);
}, "Snap to current scroll position on x as the area is covering x axis." +
   "However, we snap to the specified snap position on y as the area is not " +
   "covering y axis.");

test(() => {
  overlapping_scroller.scrollTo(200, 800);
  assert_equals(overlapping_scroller.scrollLeft, 200);
  assert_equals(overlapping_scroller.scrollTop, 800);
}, "snap to current scroll position on y as the area is covering y axis, " +
   "even though that area is not the only scroll area at the same position.");

test(() => {
  overlapping_scroller.scrollTo(800, 200);
  assert_equals(overlapping_scroller.scrollLeft, 800);
  assert_equals(overlapping_scroller.scrollTop, 200);
}, "snap to current scroll position on x as the area is covering x axis, " +
   "even though that area is not the only scroll area at the same position.");
</script>
</script>