Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
- This WPT test may be referenced by the following Test IDs:
- /css/css-scroll-snap/snap-after-relayout/multiple-aligned-targets/prefer-inner-target.html - WPT Dashboard Interop Dashboard
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/dom/events/scrolling/scroll_support.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
</head>
<body>
<style>
.scroller {
overflow: scroll;
position: relative;
height: 400px;
width: 400px;
border:solid 1px black;
scroll-snap-type: y mandatory;
}
.no-snap { scroll-snap-align: none }
.scroller div:focus {
border: solid 1px red;
}
.large-space {
height: 300vh;
width: 300vw;
position: absolute;
}
.target {
scroll-snap-align: start;
position: absolute;
width: 100px;
height: 100px;
}
.top {
top: 0px;
}
.left {
left: 0px;
}
.right {
left: 200px;
}
.bottom {
top: 200px;
}
.inner {
text-align: right;
}
.inner1 {
height: 150px;
width: 150px;
top: 0px;
left: 50px;
background-color: blue;
}
.inner2 {
height: 100px;
width: 100px;
top: 0px;
left: 0px;
background-color: pink;
}
.inner3 {
height: 75px;
width: 75px;
top: 0px;
left: 0px;
background-color: green;
}
.inner4 {
height: 50px;
width: 50px;
top: 0px;
left: 0px;
background-color: grey;
}
.outer {
height: 200px;
width: 200px;
top: 150px;
left: 50px;
background-color: yellow;
}
</style>
<div id="scroller" class="scroller">
<div class="large-space"></div>
<div class="top left target">Top Left</div>
<div class="top right target">Top Right</div>
<div class="outer target" id="outer">Outer
<div class="inner inner1 target" id="inner1">I1
<div class="inner inner2 target" id="inner2">I2
<div class="inner inner3 target" id="inner3">I3
<div class="inner inner4 target" id="inner4">I4</div>
</div>
</div>
</div>
</div>
</div>
<script>
window.onload = (async () => {
const inner1 = document.getElementById("inner1");
const inner2 = document.getElementById("inner2");
const inner3 = document.getElementById("inner3");
const inner4 = document.getElementById("inner4");
const outer = document.getElementById("outer");
const scroller = document.getElementById("scroller");
promise_test(async (t) => {
t.add_cleanup(() => {
scroller.scrollTop = 0;
});
await waitForCompositorCommit();
scroller.scrollTop = outer.offsetTop;
assert_equals(scroller.scrollTop, outer.offsetTop,
"scroll position should be at the snap target");
t.add_cleanup(() => {
// Reset inner4's style.
inner4.style.left = "";
});
// Push inner4 outside the snapport. It should no longer be considered
// the snap target; inner3 is next in line.
inner4.style.left = "500px";
assert_equals(scroller.scrollTop, outer.offsetTop,
"scroll position should be at the snap target");
assert_equals(scroller.scrollLeft, 0,
"horizontal scroll position should remain unchanged");
t.add_cleanup(() => {
inner3.style.top = "";
});
// Move inner3 so that snap-after-relayout follows it.
inner3.style.top = "100px";
assert_equals(scroller.scrollTop, outer.offsetTop + 100,
"scroll position should follow inner3 snap target after relayout");
}, "snap container selects innermost area as snap target");
promise_test(async (t) => {
t.add_cleanup(() => {
outer.style.top = "";
});
await waitForCompositorCommit();
// Move outer target; all inner targets move with it, so every
// inner target's Y offset equals outer's.
outer.style.top = "400px";
// Scroll to the position where all snap targets are co-located;
// inner4 is selected as the snap target since it is the innermost.
scroller.scrollTop = outer.offsetTop;
assert_equals(scroller.scrollTop, outer.offsetTop,
"scroll position should be at the snap target");
t.add_cleanup(() => {
inner4.style.top = "";
});
// Move the innermost target so that snap-after-relayout follows it.
inner4.style.top = "100px";
assert_equals(scroller.scrollTop, outer.offsetTop + 100,
"scroll position should follow the innermost snap target after relayout");
}, "snap container follows the innermost snap target after layout change " +
"(not the co-located outer target)");
});
</script>
</body>
</html>