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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
<!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>
.scroller {
overflow: auto;
position: relative;
height: 200px;
width: 100px;
border: 1px solid black;
}
/* Create scrollable content. */
.scroller::after {
content: "";
display: block;
height: 2000px;
}
/* Outer scroller container is positioned at 1000px */
.tests > .scroller > * {
position: relative;
top: 999px; /* content inside container starts at 1000px */
height: 150px;
width: 90%;
margin: 0 auto;
border: 1px solid black;
}
.visible {
overflow: visible;
}
.clip {
overflow: clip;
}
.target {
background: green;
height: 40px;
width: 40px;
margin: 0 auto;
position: relative;
}
.scroll-margin {
scroll-margin-top: 40px;
}
.scroll-padding {
scroll-padding-top: 20px;
}
.slightly-down { top: 20px; }
.far-down { top: 400px; }
.slightly-up { top: -20px; }
.tests {
display: flex;
}
</style>
<h3>overflow: visible</h3>
<div id="overflow-visible" class="tests">
<div class="scroller">
<div class="visible">
<div class="scroll-margin target far-down"></div>
</div>
</div>
<div class="scroller">
<div class="visible">
<div class="scroll-margin target slightly-down"></div>
</div>
</div>
<div class="scroller">
<div class="visible">
<div class="scroll-margin target at-top"></div>
</div>
</div>
<div class="scroller">
<div class="visible">
<div class="scroll-margin target slightly-up"></div>
</div>
</div>
</div>
<h3>overflow: clip</h3>
<div id="overflow-clip" class="tests">
<div class="scroller">
<div class="clip">
<div class="scroll-margin target far-down"></div>
</div>
</div>
<div class="scroller">
<div class="clip">
<div class="scroll-margin target slightly-down"></div>
</div>
</div>
<div class="scroller">
<div class="clip">
<div class="scroll-margin target at-top"></div>
</div>
</div>
<div class="scroller">
<div class="clip">
<div class="scroll-margin target slightly-up"></div>
</div>
</div>
</div>
<h3>overflow: auto</h3>
<div id="overflow-auto" class="tests">
<div class="scroller">
<div class="scroller">
<div class="scroll-margin target far-down"></div>
</div>
</div>
<div class="scroller">
<div class="scroller">
<div class="scroll-margin target slightly-down"></div>
</div>
</div>
<div class="scroller">
<div class="scroller">
<div class="scroll-margin target at-top"></div>
</div>
</div>
<div class="scroller">
<div class="scroller">
<div class="scroll-margin target slightly-up"></div>
</div>
</div>
</div>
<h3>overflow: auto with scroll padding</h3>
<div id="overflow-auto-scroll-padding" class="tests">
<div class="scroller scroll-padding">
<div class="scroller scroll-padding">
<div class="scroll-margin target far-down"></div>
</div>
</div>
<div class="scroller scroll-padding">
<div class="scroller scroll-padding">
<div class="scroll-margin target slightly-down"></div>
</div>
</div>
<div class="scroller scroll-padding">
<div class="scroller scroll-padding">
<div class="scroll-margin target at-top"></div>
</div>
</div>
<div class="scroller scroll-padding">
<div class="scroller scroll-padding">
<div class="scroll-margin target slightly-up"></div>
</div>
</div>
</div>
<script>
// Scroll all target elements into view.
for (const target of document.querySelectorAll('.target')) {
target.scrollIntoView();
}
// Scroll main frame to top
document.scrollingElement.scrollTo(0, 0);
test(() => {
const container = document.getElementById('overflow-visible');
// The element is 400px below container at 1400px,
// we should scroll to 1360 to give a 40px scroll margin.
const farDownScroller = container.querySelector('.scroller:has(.far-down)');
assert_equals(farDownScroller.scrollTop, 1360,
'gives 40px margin to element far below visible container');
// The element is 20px below container at 1020px,
// we should scroll to 980 to give a 40px scroll margin.
const slightlyDownScroller = container.querySelector('.scroller:has(.slightly-down)');
assert_equals(slightlyDownScroller.scrollTop, 980,
'gives 40px margin to element slightly below visible container');
// The element is at top of container at 1000px,
// we should scroll to 960 to give a 40px scroll margin.
const atTopScroller = container.querySelector('.scroller:has(.at-top)');
assert_equals(atTopScroller.scrollTop, 960,
'gives 40px margin to element at top of visible container');
// The element is 20px above container at 980px,
// we should scroll to 940 to give a 40px scroll margin.
const aboveTopScroller = container.querySelector('.scroller:has(.slightly-up)');
assert_equals(aboveTopScroller.scrollTop, 940,
'gives 40px margin to element slightly above visible container');
}, "scroll-margin is respected when overflowing an overflow: visible container");
test(() => {
const container = document.getElementById('overflow-clip');
// The element is 400px below container at 1400px,
// we should scroll to 1360 to give a 40px scroll margin
// even though the element is completely clipped.
const farDownScroller = container.querySelector('.scroller:has(.far-down)');
assert_equals(farDownScroller.scrollTop, 1360,
'gives 40px margin to element far below clip container');
// The element is 20px below container at 1020px,
// we should scroll to 980 to give a 40px scroll margin.
const slightlyDownScroller = container.querySelector('.scroller:has(.slightly-down)');
assert_equals(slightlyDownScroller.scrollTop, 980,
'gives 40px margin to element slightly below clip container');
// The element is at top of container at 1000px,
// we should scroll to 960 to give a 40px scroll margin.
const atTopScroller = container.querySelector('.scroller:has(.at-top)');
assert_equals(atTopScroller.scrollTop, 960,
'gives 40px margin to element at top of clip container');
// The element is 20px above container at 980px,
// we should scroll to 940 to give a 40px scroll margin
// even though the top of the element is clipped.
const aboveTopScroller = container.querySelector('.scroller:has(.slightly-up)');
assert_equals(aboveTopScroller.scrollTop, 940,
'gives 40px margin to element slightly above clip container');
}, "scroll-margin is respected when overflowing an overflow: clip container");
test(() => {
const container = document.getElementById('overflow-auto');
// The element is 400px below container at 1400px,
// we should scroll inner container to 360 to give 40px margin,
// and scroll outer container to 1000px to bring it to the top.
const farDownScroller = container.querySelector('.scroller:has(.far-down)');
const farDownInnerScroller = farDownScroller.querySelector('.scroller');
assert_equals(farDownInnerScroller.scrollTop, 360,
'gives 40px margin to element far down in inner scroller');
assert_equals(farDownScroller.scrollTop, 1000,
'scrolls to top of outer scroller');
// The element is 20px below container at 1020px,
// we can only give it 20px of margin in the inner scroller by scrolling to 0,
// so we should give it an additional 20px of margin in the outer scroller.
const slightlyDownScroller = container.querySelector('.scroller:has(.slightly-down)');
const slightlyDownInnerScroller = slightlyDownScroller.querySelector('.scroller');
assert_equals(slightlyDownInnerScroller.scrollTop, 0,
'gives all 20px possible for 40px margin in inner scroller');
assert_equals(slightlyDownScroller.scrollTop, 980,
'gives remaining 20px margin on outer scroller');
// The element is at top of container at 1000px,
// we can't give any margin on the inner scroller,
// we should scroll to 960 to give a 40px scroll margin.
const atTopScroller = container.querySelector('.scroller:has(.at-top)');
const atTopInnerScroller = atTopScroller.querySelector('.scroller');
assert_equals(atTopInnerScroller.scrollTop, 0,
'scrolls to top of inner scroller');
assert_equals(atTopScroller.scrollTop, 960,
'gives 40px margin to element in outer scroller');
// The element is 20px above container at 980px,
// we should scroll the inner container to the top,
// and since the element is above we still
// only need to give 40px above the inner scroller of margin.
const aboveTopScroller = container.querySelector('.scroller:has(.slightly-up)');
const aboveTopInnerScroller = aboveTopScroller.querySelector('.scroller');
assert_equals(aboveTopInnerScroller.scrollTop, 0,
'scrolls to top of inner scroller');
assert_equals(aboveTopScroller.scrollTop, 960,
'gives 40px margin to element slightly above inner scroller');
}, "scroll-margin is respected when overflowing an overflow: auto container");
test(() => {
const container = document.getElementById('overflow-auto-scroll-padding');
// The element is 400px below container at 1400px,
// we should scroll inner container to 340 to give 40px margin + 20px padding,
// and scroll outer container to 1000px as this will also have
// 40px margin + 20px padding from the target box.
const farDownScroller = container.querySelector('.scroller:has(.far-down)');
const farDownInnerScroller = farDownScroller.querySelector('.scroller');
assert_equals(farDownInnerScroller.scrollTop, 340,
'gives 40px margin and 20px padding to element far down in inner scroller');
assert_equals(farDownScroller.scrollTop, 1000,
'scrolls to top of outer scroller');
// The element is 20px below container at 1020px,
// we can only give it 20px of margin in the inner scroller by scrolling to 0,
// so we should give it an additional 20px of margin and 20px padding
// in the outer scroller plus an additional 20px padding for the outer
// scroller's padding. Note we don't duplicate the padding the inner
// scroller has already reserved.
const slightlyDownScroller = container.querySelector('.scroller:has(.slightly-down)');
const slightlyDownInnerScroller = slightlyDownScroller.querySelector('.scroller');
assert_equals(slightlyDownInnerScroller.scrollTop, 0,
'gives all 20px possible in inner scroller');
assert_equals(slightlyDownScroller.scrollTop, 960,
'gives remaining 20px margin and 20px padding on outer scroller');
// The element is at top of container at 1000px,
// we can't give any margin or padding on the inner scroller,
// we should scroll to 940 to give a 40px scroll margin + 20px padding.
const atTopScroller = container.querySelector('.scroller:has(.at-top)');
const atTopInnerScroller = atTopScroller.querySelector('.scroller');
assert_equals(atTopInnerScroller.scrollTop, 0,
'scrolls to top of inner scroller');
assert_equals(atTopScroller.scrollTop, 940,
'gives 40px margin and 20px padding to element in outer scroller');
// The element is 20px above container at 980px,
// we should scroll the inner container to the top,
// and since the element is above we still
// only need to give 40px margin + 20px padding above the inner scroller of margin.
const aboveTopScroller = container.querySelector('.scroller:has(.slightly-up)');
const aboveTopInnerScroller = aboveTopScroller.querySelector('.scroller');
assert_equals(aboveTopInnerScroller.scrollTop, 0,
'scrolls to top of inner scroller');
assert_equals(aboveTopScroller.scrollTop, 940,
'gives 40px margin and 20px padding to element slightly above inner scroller');
}, "scroll-margin and scroll-padding are respected when overflowing an overflow: auto container");
</script>
|