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-1/#scroll-margin" />
<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>
|