summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-scroll-snap/scroll-snap-stop-change.html
blob: 4615487f56bf5cafdeb43f65f2f9dcf19f4d5adf (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
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-scroll-snap/#scroll-snap-stop" />
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
div, html, body {
  margin: 0;
  padding: 0;
}

html {
  scroll-snap-type: x mandatory;
  overflow: scroll;
}

#scroller {
  scroll-snap-type: x mandatory;
  overflow: scroll;
  height: 400px;
  width: 400px;
}

.large_space {
  width: 2000px;
  height: 2000px;
}

.snap_area {
  scroll-snap-align: none start;
  width: 100px;
  height: 100px;

  background-color: blue;
}

.snap_area:nth-child(1) {
  margin-left: 0;
}

.snap_area:nth-child(2) {
  margin-left: 100px;
}

.snap_area:nth-child(3) {
  margin-left: 300px;
}

.snap_area:nth-child(4) {
  margin-left: 500px;
}
</style>

<!-- Add snap area to the root scroller -->
<div class="snap_area"></div>
<div class="snap_area"></div>
<div class="snap_area"></div>
<div class="snap_area"></div>

<div id="scroller">
  <div class="snap_area"></div>
  <div class="snap_area"></div>
  <div class="snap_area"></div>
  <div class="snap_area"></div>
  <div class="large_space"></div>
</div>

<div class="large_space"></div>

<script>
const scrollers = [document.scrollingElement, document.getElementById("scroller")];
for (const scroller of scrollers) {
  test(_ => {
    assert_equals(scroller.scrollLeft, 0); // sanity check

    scroller.querySelectorAll('.snap_area').forEach(area => area.style.scrollSnapStop = 'always');
    scroller.scrollBy(500, 0);
    assert_equals(scroller.scrollLeft, 100, "scrollBy should not skip area with stop always");

    scroller.querySelectorAll('.snap_area').forEach(area => area.style.scrollSnapStop = 'normal');
    scroller.scrollBy(500, 0);
    assert_equals(scroller.scrollLeft, 500, "scrollBy should skip secon area with stop normal");

    // When snap type is changed back to mandatory, scrolling should snap.
    scroller.querySelectorAll('.snap_area').forEach(area => area.style.scrollSnapStop = 'always');
    scroller.scrollBy(-500, 0);
    assert_equals(scroller.scrollLeft, 300, "scrollBy should not skip area with stop always");
  }, `scroll-snap-stop for areas on ${scroller.nodeName} should control snapping behavior and changing it takes effect`);
}
</script>