Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
div {
position: absolute;
margin: 0px;
}
#scroller {
height: 500px;
width: 500px;
overflow: hidden;
scroll-snap-type: both mandatory;
}
#target {
width: 300px;
height: 300px;
background-color: blue;
}
#another-target {
width: 300px;
height: 300px;
top: 400px;
left: 400px;
background-color: blue;
scroll-snap-align: start;
}
</style>
<div id="scroller">
<div style="width: 2000px; height: 2000px;"></div>
<div id="target"></div>
<div id="another-target"></div>
</div>
<script>
test(() => {
target.style.scrollSnapAlign = "start";
target.style.scrollMargin = "100px";
target.style.left = "300px";
target.style.top = "300px";
scroller.scrollTo(0, 0);
// `target position (300px, 300px)` - `margin (100px, 100px)`.
assert_equals(scroller.scrollLeft, 200);
assert_equals(scroller.scrollTop, 200);
target.style.scrollSnapAlign = "end";
// `target position (300px, 300px)` + `target size (300px, 300px) +
// `margin (100px, 100px) - `scroller size (500px, 500px)`.
scroller.scrollTo(0, 0);
assert_equals(scroller.scrollLeft, 200);
assert_equals(scroller.scrollTop, 200);
}, "Snaps to the positions adjusted by scroll-margin");
test(() => {
target.style.left = "0px";
target.style.top = "0px";
target.style.scrollSnapAlign = "start";
// Since the target is at (0px, 0px) in the scroll port, the added margin
// should not be considered, and the snap points for this snap area should be
// the closest points in the scroll port (i.e x=0 or y=0).
target.style.scrollMargin = "200px";
// Distance from target without margin:
// `scroll position (150px, 150px)` - `target position (0px, 0px)`
// = (150px, 150px)
//
// Distance from target with margin:
// `scroll position (150px, 150px)` - [`target position (0px, 0px)` -
// `target margin (200px, 200px)`]
// = (350px, 350px)
//
// Distance from other target:
// `other target position (400px, 400px)` - `scroll position (150px, 150px)`
// = (250px, 250px)
//
// Therefore if the "out-of-scrollport" scroll-margin contributes to the
// calculation, then the other target would be snapped to. However if the
// scroll-margin is not considered, then the (0px, 0px) target should be
// snapped to.
scroller.scrollTo(150, 150);
assert_equals(scroller.scrollLeft, 0);
assert_equals(scroller.scrollTop, 0);
}, "scroll-margin doesn't contribute to the snap position of the element " +
"if it's outside of the scroll port");
</script>