Skip to content

Commit 1942d09

Browse files
authored
[cssom-view-1] Add scrollIntoView example demonstrating usage of container attribute. (#12450)
1 parent eb4e1f7 commit 1942d09

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

cssom-view-1/Overview.bs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,42 @@ The <dfn method for=Element caniuse=scrollintoview>scrollIntoView(<var>arg</var>
13091309
with <var>behavior</var>, <var>block</var>, <var>inline</var>, and <var>container</var>.
13101310
1. Optionally perform some other action that brings the element to the user's attention.
13111311

1312+
<div class='example'>
1313+
A component can use scrollIntoView to scroll content of interest into the specified alignment:
1314+
1315+
<pre><code highlight="html">
1316+
&lt;style&gt;
1317+
.scroller { overflow: auto; scroll-padding: 8px; }
1318+
.slide { scroll-margin: 16px; scroll-snap-align: center; }
1319+
&lt;/style&gt;
1320+
&lt;div class="carousel"&gt;
1321+
&lt;div class="slides scroller"&gt;
1322+
&lt;div id="s1" class="slide"&gt;
1323+
&lt;div id="s2" class="slide"&gt;
1324+
&lt;div id="s3" class="slide"&gt;
1325+
&lt;/div&gt;
1326+
&lt;div class="markers"&gt;
1327+
&lt;button data-target="s1"&gt;1&lt;/button&gt;
1328+
&lt;button data-target="s2"&gt;2&lt;/button&gt;
1329+
&lt;button data-target="s3"&gt;3&lt;/button&gt;
1330+
&lt;/div&gt;
1331+
&lt;/div&gt;
1332+
&lt;script&gt;
1333+
document.querySelector('.markers').addEventListener('click', (evt) => {
1334+
const target = document.getElementById(evt.target.dataset.target);
1335+
if (!target) return;
1336+
// scrollIntoView correctly aligns target item respecting scroll-snap-align,
1337+
// scroll-margin, and the scroll container's scroll-padding.
1338+
target.scrollIntoView({
1339+
// Only scroll the nearest scroll container.
1340+
container: 'nearest',
1341+
behavior: 'smooth'
1342+
});
1343+
});
1344+
&lt;/script&gt;
1345+
</code></pre>
1346+
</div>
1347+
13121348
The <dfn method for=Element lt="scroll(options)|scroll(x, y)">scroll()</dfn> method must run these steps:
13131349

13141350
1. If invoked with one argument, follow these substeps:

0 commit comments

Comments
 (0)