Implement the updated port/area-based Scroll Snap Module Level 1 Spec
[WebKit-https.git] / LayoutTests / css3 / scroll-snap / scroll-snap-children-with-scroll-snap-margin.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4     <style>
5         #container {
6             position: absolute;
7             width: 400px;
8             height: 400px;
9             top: 0;
10             left: 0;
11             scroll-snap-type: y mandatory;
12             overflow-x: hidden;
13             overflow-y: scroll;
14             border: 1px black dashed;
15             opacity: 0.75;
16             background-color: #EEE;
17         }
18
19         .child {
20             width: 400px;
21             height: 400px;
22             scroll-snap-align: start;
23             position: absolute;
24             left: 0;
25         }
26     </style>
27     <script>
28     let write = s => output.innerHTML += s + "<br>";
29     if (window.testRunner) {
30         testRunner.dumpAsText();
31         testRunner.waitUntilDone();
32     }
33
34     function deflateScrollSnapMargins(amount) {
35         for (let child of document.querySelectorAll(".child")) {
36             let currentMargin = parseInt(child.style.scrollSnapMargin.replace(/px/, ""));
37             child.style.scrollSnapMargin = `${currentMargin - amount}px`;
38         }
39     }
40
41     function zeroDelayTimeout(action) {
42         return new Promise((resolve) => {
43             setTimeout(() => {
44                 action();
45                 resolve();
46             });
47         });
48     }
49
50     function run()
51     {
52         if (!window.testRunner || !window.internals) {
53             write(`To manually test, verify that scrolling snaps somewhere above each colored box. As you scroll further
54                 down, the distance between the snapped position and the top of the colored box should increase.`);
55             return;
56         }
57
58         zeroDelayTimeout(() => {
59             write("Scroll-snap offsets are: " + internals.scrollSnapOffsets(container));
60             container.scrollTop += 100;
61             container.style.width = "500px";
62         })
63         .then(() => {
64             zeroDelayTimeout(() => {
65                 write("After resizing, the offsets should not have changed: " + internals.scrollSnapOffsets(container));
66                 container.scrollTop += 100;
67                 deflateScrollSnapMargins(10);
68             });
69         })
70         .then(() => {
71             zeroDelayTimeout(() => {
72                 write("After deflating scroll-snap-margin by 10px, the offsets are: " + internals.scrollSnapOffsets(container));
73                 container.scrollTop += 100;
74                 container.style.width = "400px";
75             });
76         })
77         .then(() => {
78             zeroDelayTimeout(() => {
79                 write("After resizing, the offsets should not have changed: " + internals.scrollSnapOffsets(container));
80                 testRunner.notifyDone();
81             });
82         });
83     }
84     </script>
85 </head>
86 <body onload=run()>
87     <div id="container">
88         <div class="child" style="background-color: red;     top: 0px;    scroll-snap-margin: 10px;"></div>
89         <div class="child" style="background-color: green;   top: 720px;  scroll-snap-margin: 20px;"></div>
90         <div class="child" style="background-color: blue;    top: 1440px; scroll-snap-margin: 30px;"></div>
91         <div class="child" style="background-color: aqua;    top: 2160px; scroll-snap-margin: 40px;"></div>
92         <div class="child" style="background-color: yellow;  top: 2880px; scroll-snap-margin: 50px;"></div>
93         <div class="child" style="background-color: fuchsia; top: 3600px; scroll-snap-margin: 60px;"></div>
94     </div>
95     <div id="output"></div>
96 </body>
97 </html>