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-snap-target-containing-focused-element.html - WPT Dashboard Interop Dashboard
<!DOCTYPE html>
<meta name="viewport" content="width=device-width,initial-scale=1">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
#scroller {
scroll-snap-type: y mandatory;
overflow: scroll;
width: 200px;
height: 200px;
position: relative;
}
.snap {
width: 100px;
height: 100px;
position: absolute;
top: 0px;
left: 0px;
scroll-snap-align: start;
}
#focus {
width: 50px;
height: 50px;
}
#spacer {
width: 1000px;
height: 1000px;
}
</style>
<div id="scroller">
<div class="snap"></div>
<div class="snap">
<div id="focus" tabindex="-1"></div>
</div>
<div id="spacer"></div>
</div>
<script>
promise_test(async () => {
assert_equals(scroller.scrollTop, 0);
assert_equals(scroller.scrollLeft, 0);
const focusedElement = document.querySelector("#focus");
focusedElement.focus({preventScroll: false});
assert_equals(document.activeElement, focusedElement);
const scrollendPromise = new Promise(resolve => {
scroller.addEventListener("scrollend", resolve);
});
// Move the parent of the focused element.
focusedElement.parentElement.style.top = "10px";
await scrollendPromise;
assert_equals(scroller.scrollTop, 10);
assert_equals(scroller.scrollLeft, 0);
}, "Re-snap prefers snap target containing the focused element when multiple targets are aligned");
</script>