Browse Source

Link

master
David Larlet 1 month ago
parent
commit
aba9b651e0
Signed by: David Larlet <david@larlet.fr> GPG Key ID: 3E2953A359E7E7BD

+ 238
- 0
cache/2024/5ff0fe74d43b57fa41db8851bc56ac88/index.html View File

@@ -0,0 +1,238 @@
<!doctype html><!-- This is a valid HTML5 document. -->
<!-- Screen readers, SEO, extensions and so on. -->
<html lang="en">
<!-- Has to be within the first 1024 bytes, hence before the `title` element
See: https://www.w3.org/TR/2012/CR-html5-20121217/document-metadata.html#charset -->
<meta charset="utf-8">
<!-- Why no `X-UA-Compatible` meta: https://stackoverflow.com/a/6771584 -->
<!-- The viewport meta is quite crowded and we are responsible for that.
See: https://codepen.io/tigt/post/meta-viewport-for-2015 -->
<meta name="viewport" content="width=device-width,initial-scale=1">
<!-- Required to make a valid HTML5 document. -->
<title>Highlight Text When a User Scrolls Down to That Piece of Text (archive) — David Larlet</title>
<meta name="description" content="Publication mise en cache pour en conserver une trace.">
<!-- That good ol' feed, subscribe :). -->
<link rel="alternate" type="application/atom+xml" title="Feed" href="/david/log/">
<!-- Generated from https://realfavicongenerator.net/ such a mess. -->
<link rel="apple-touch-icon" sizes="180x180" href="/static/david/icons2/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/static/david/icons2/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/static/david/icons2/favicon-16x16.png">
<link rel="manifest" href="/static/david/icons2/site.webmanifest">
<link rel="mask-icon" href="/static/david/icons2/safari-pinned-tab.svg" color="#07486c">
<link rel="shortcut icon" href="/static/david/icons2/favicon.ico">
<meta name="msapplication-TileColor" content="#f7f7f7">
<meta name="msapplication-config" content="/static/david/icons2/browserconfig.xml">
<meta name="theme-color" content="#f7f7f7" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#272727" media="(prefers-color-scheme: dark)">
<!-- Is that even respected? Retrospectively? What a shAItshow…
https://neil-clarke.com/block-the-bots-that-feed-ai-models-by-scraping-your-website/ -->
<meta name="robots" content="noai, noimageai">
<!-- Documented, feel free to shoot an email. -->
<link rel="stylesheet" href="/static/david/css/style_2021-01-20.css">
<!-- See https://www.zachleat.com/web/comprehensive-webfonts/ for the trade-off. -->
<link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_regular.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
<link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_bold.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
<link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_italic.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
<link rel="preload" href="/static/david/css/fonts/triplicate_t3_regular.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
<link rel="preload" href="/static/david/css/fonts/triplicate_t3_bold.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
<link rel="preload" href="/static/david/css/fonts/triplicate_t3_italic.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
<script>
function toggleTheme(themeName) {
document.documentElement.classList.toggle(
'forced-dark',
themeName === 'dark'
)
document.documentElement.classList.toggle(
'forced-light',
themeName === 'light'
)
}
const selectedTheme = localStorage.getItem('theme')
if (selectedTheme !== 'undefined') {
toggleTheme(selectedTheme)
}
</script>

<meta name="robots" content="noindex, nofollow">
<meta content="origin-when-cross-origin" name="referrer">
<!-- Canonical URL for SEO purposes -->
<link rel="canonical" href="https://frontendmasters.com/blog/highlight-text-when-a-user-scrolls-down-to-that-piece-of-text/">

<body class="remarkdown h1-underline h2-underline h3-underline em-underscore hr-center ul-star pre-tick" data-instant-intensity="viewport-all">


<article>
<header>
<h1>Highlight Text When a User Scrolls Down to That Piece of Text</h1>
</header>
<nav>
<p class="center">
<a href="/david/" title="Aller à l’accueil"><svg class="icon icon-home">
<use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-home"></use>
</svg> Accueil</a> •
<a href="https://frontendmasters.com/blog/highlight-text-when-a-user-scrolls-down-to-that-piece-of-text/" title="Lien vers le contenu original">Source originale</a>
<br>
Mis en cache le 2024-03-12
</p>
</nav>
<hr>
<p><a href="https://www.lenesaile.com/en/blog/about-subgrid-and-colored-grid-lines/">I was reading a great post on Lene Saile’s blog</a> and noticed a cool little design feature on her site that highlights a line of text once you scroll to it. Here’s a video so you can see what I mean:</p>

<p>The highlighted line is done with a <code>&lt;mark&gt;</code> element in HTML, which feels right. I noticed the class name on Lene’s implementation is <code>.gsap-highlight</code> which implies GSAP is used which has as <a href="https://gsap.com/docs/v3/Plugins/ScrollTrigger/">a great Scroll Trigger plugin</a>. Let’s do this <em>without</em> JavaScript though, especially now that <a href="https://frontendmasters.com/blog/background-size-zooming-with-scroll-driven-animations/">I’m hip to Scroll-Driven Animations</a>. </p>

<h2 class="wp-block-heading" id="toc-1"><a href="#basic-html" aria-hidden="true" class="aal_anchor" id="basic-html"><svg aria-hidden="true" class="aal_svg" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Basic HTML</h2>

<p>A paragraph with a mark (with a class):</p>

<pre class="wp-block-code" aria-describedby="shcb-language-1" data-shcb-language-name="HTML, XML" data-shcb-language-slug="xml"><span><code class="hljs language-xml shcb-wrap-lines"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Lorem, ipsum dolor sit amet <span class="hljs-tag">&lt;<span class="hljs-name">mark</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"scroll-highlight"</span>&gt;</span>consectetur adipisicing elit<span class="hljs-tag">&lt;/<span class="hljs-name">mark</span>&gt;</span>. Magnam voluptas aliquid, distinctio voluptatum neque qui modi. In adipisci ratione id officiis nulla veritatis, porro explicabo illum laudantium iure eius velit!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></code></span><small class="shcb-language" id="shcb-language-1"><span class="shcb-language__label">Code language:</span> <span class="shcb-language__name">HTML, XML</span> <span class="shcb-language__paren">(</span><span class="shcb-language__slug">xml</span><span class="shcb-language__paren">)</span></small></pre>

<h2 class="wp-block-heading" id="toc-2"><a href="#css-mark-styling-in-css" aria-hidden="true" class="aal_anchor" id="css-mark-styling-in-css"><svg aria-hidden="true" class="aal_svg" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>CSS Mark Styling in CSS</h2>

<p>I just want a solid background on the mark. But I’m <em>not</em> going to use <code>background-color</code>. Instead I’m going to use <code>background-image</code>, because then I can control the <code>background-size</code> which I ultimately want to animate. So:</p>

<pre class="wp-block-code" aria-describedby="shcb-language-2" data-shcb-language-name="CSS" data-shcb-language-slug="css"><span><code class="hljs language-css"><span class="hljs-selector-tag">mark</span><span class="hljs-selector-class">.scroll-highlight</span> {
<span class="hljs-attribute">background-size</span>: <span class="hljs-number">100%</span> <span class="hljs-number">100%</span>;
<span class="hljs-attribute">background-repeat</span>: no-repeat;
<span class="hljs-attribute">background-color</span>: transparent;
<span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">linear-gradient</span>(purple, purple);
}</code></span><small class="shcb-language" id="shcb-language-2"><span class="shcb-language__label">Code language:</span> <span class="shcb-language__name">CSS</span> <span class="shcb-language__paren">(</span><span class="shcb-language__slug">css</span><span class="shcb-language__paren">)</span></small></pre>

<figure class="wp-block-image size-large"><img decoding="async" src="https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/Screenshot-2024-01-23-at-2.20.48%E2%80%AFPM-1024x322.png?resize=1024%2C322&amp;ssl=1" alt="" class="wp-image-643" data-recalc-dims="1"></figure>

<p>Now I can animate that <code>background-size</code> from <code>0% 100%</code> to <code>100% 100%</code> which is the look we’re after. It even works when the text breaks across lines which is a miracle.</p>

<figure class="wp-block-image size-full"><img decoding="async" src="https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.22.33.gif?resize=800%2C187&amp;ssl=1" alt="" class="wp-image-644" data-recalc-dims="1"></figure>

<p>Now it’s a matter of <em>when</em> to run the animation. </p>

<h2 class="wp-block-heading" id="toc-3"><a href="#scroll-driven-animation-for-the-mark" aria-hidden="true" class="aal_anchor" id="scroll-driven-animation-for-the-mark"><svg aria-hidden="true" class="aal_svg" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Scroll-Driven Animation for the Mark</h2>

<p>Here’s the whole trick:</p>

<pre class="wp-block-code" aria-describedby="shcb-language-3" data-shcb-language-name="CSS" data-shcb-language-slug="css"><span><code class="hljs language-css"><span class="hljs-selector-tag">mark</span><span class="hljs-selector-class">.scroll-highlight</span> {
<span class="hljs-attribute">background-size</span>: <span class="hljs-number">0</span> <span class="hljs-number">100%</span>;
<span class="hljs-attribute">background-repeat</span>: no-repeat;
<span class="hljs-attribute">background-color</span>: transparent;
<span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">linear-gradient</span>(purple, purple);

<span class="hljs-attribute">animation</span>: mark-it linear;
<span class="hljs-attribute">animation-fill-mode</span>: forwards;
<span class="hljs-attribute">animation-timeline</span>: <span class="hljs-built_in">view</span>();
<span class="hljs-attribute">animation-iteration-count</span>: <span class="hljs-number">1</span>;
<span class="hljs-attribute">animation-range</span>: contain <span class="hljs-number">0%</span> contain <span class="hljs-number">25%</span>;
}

<span class="hljs-keyword">@keyframes</span> mark-it {
0% {
<span class="hljs-attribute">background-size</span>: <span class="hljs-number">0</span> <span class="hljs-number">100%</span>;
}
100% {
<span class="hljs-attribute">background-size</span>: <span class="hljs-number">100%</span> <span class="hljs-number">100%</span>;
}
}</code></span><small class="shcb-language" id="shcb-language-3"><span class="shcb-language__label">Code language:</span> <span class="shcb-language__name">CSS</span> <span class="shcb-language__paren">(</span><span class="shcb-language__slug">css</span><span class="shcb-language__paren">)</span></small></pre>

<p>The coolest part to me is the <code>animation-range</code> which gives us the opportunity to say when to start and end the animation with a solid amount of control. In the code above we’re saying to start the animation as soon as the element is fully within the viewport, then finish when it’s 25% of the way up the currently visible viewport. </p>

<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" src="https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.29.33@2x.png?resize=1024%2C638&amp;ssl=1" alt="" class="wp-image-645" srcset="https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.29.33@2x.png?resize=1024%2C638&amp;ssl=1 1024w, https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.29.33@2x.png?resize=300%2C187&amp;ssl=1 300w, https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.29.33@2x.png?resize=768%2C478&amp;ssl=1 768w, https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.29.33@2x.png?resize=1536%2C957&amp;ssl=1 1536w, https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.29.33@2x.png?resize=2048%2C1275&amp;ssl=1 2048w" sizes="(max-width: 1000px) 100vw, 1000px" data-recalc-dims="1"><figcaption class="wp-element-caption">Here the element is maybe 20% up the current viewport, so the animation is 80% finished.</figcaption></figure>

<h2 class="wp-block-heading" id="toc-4"><a href="#demo" aria-hidden="true" class="aal_anchor" id="demo"><svg aria-hidden="true" class="aal_svg" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Demo</h2>

<p>Remember <a href="https://caniuse.com/mdn-css_properties_animation-range">native support for Scroll-Driven Animations</a> is essentially Chrome ‘n’ friends only right now. You could easily treat this as a progressive enhancement, wrapping the animation stuff in a <code>@supports (animation-timeline: view()) { }</code> block or however you wanna do it.</p>
</article>


<hr>

<footer>
<p>
<a href="/david/" title="Aller à l’accueil"><svg class="icon icon-home">
<use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-home"></use>
</svg> Accueil</a> •
<a href="/david/log/" title="Accès au flux RSS"><svg class="icon icon-rss2">
<use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-rss2"></use>
</svg> Suivre</a> •
<a href="http://larlet.com" title="Go to my English profile" data-instant><svg class="icon icon-user-tie">
<use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-user-tie"></use>
</svg> Pro</a> •
<a href="mailto:david%40larlet.fr" title="Envoyer un courriel"><svg class="icon icon-mail">
<use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-mail"></use>
</svg> Email</a> •
<abbr class="nowrap" title="Hébergeur : Alwaysdata, 62 rue Tiquetonne 75002 Paris, +33184162340"><svg class="icon icon-hammer2">
<use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-hammer2"></use>
</svg> Légal</abbr>
</p>
<template id="theme-selector">
<form>
<fieldset>
<legend><svg class="icon icon-brightness-contrast">
<use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-brightness-contrast"></use>
</svg> Thème</legend>
<label>
<input type="radio" value="auto" name="chosen-color-scheme" checked> Auto
</label>
<label>
<input type="radio" value="dark" name="chosen-color-scheme"> Foncé
</label>
<label>
<input type="radio" value="light" name="chosen-color-scheme"> Clair
</label>
</fieldset>
</form>
</template>
</footer>
<script src="/static/david/js/instantpage-5.1.0.min.js" type="module"></script>
<script>
function loadThemeForm(templateName) {
const themeSelectorTemplate = document.querySelector(templateName)
const form = themeSelectorTemplate.content.firstElementChild
themeSelectorTemplate.replaceWith(form)

form.addEventListener('change', (e) => {
const chosenColorScheme = e.target.value
localStorage.setItem('theme', chosenColorScheme)
toggleTheme(chosenColorScheme)
})

const selectedTheme = localStorage.getItem('theme')
if (selectedTheme && selectedTheme !== 'undefined') {
form.querySelector(`[value="${selectedTheme}"]`).checked = true
}
}

const prefersColorSchemeDark = '(prefers-color-scheme: dark)'
window.addEventListener('load', () => {
let hasDarkRules = false
for (const styleSheet of Array.from(document.styleSheets)) {
let mediaRules = []
for (const cssRule of styleSheet.cssRules) {
if (cssRule.type !== CSSRule.MEDIA_RULE) {
continue
}
// WARNING: Safari does not have/supports `conditionText`.
if (cssRule.conditionText) {
if (cssRule.conditionText !== prefersColorSchemeDark) {
continue
}
} else {
if (cssRule.cssText.startsWith(prefersColorSchemeDark)) {
continue
}
}
mediaRules = mediaRules.concat(Array.from(cssRule.cssRules))
}

// WARNING: do not try to insert a Rule to a styleSheet you are
// currently iterating on, otherwise the browser will be stuck
// in a infinite loop…
for (const mediaRule of mediaRules) {
styleSheet.insertRule(mediaRule.cssText)
hasDarkRules = true
}
}
if (hasDarkRules) {
loadThemeForm('#theme-selector')
}
})
</script>
</body>
</html>

+ 100
- 0
cache/2024/5ff0fe74d43b57fa41db8851bc56ac88/index.md View File

@@ -0,0 +1,100 @@
title: Highlight Text When a User Scrolls Down to That Piece of Text
url: https://frontendmasters.com/blog/highlight-text-when-a-user-scrolls-down-to-that-piece-of-text/
hash_url: 5ff0fe74d43b57fa41db8851bc56ac88
archive_date: 2024-03-12
og_image: https://frontendmasters.com/blog/wp-json/social-image-generator/v1/image/639
description: I was reading a great post on Lene Saile’s blog and noticed a cool little design feature on her site that highlights a line of text once you scroll to it. Here’s a video so you can see …
favicon: https://frontendmasters.com/favicon-32x32.png
language: en_US

<p><a href="https://www.lenesaile.com/en/blog/about-subgrid-and-colored-grid-lines/">I was reading a great post on Lene Saile’s blog</a> and noticed a cool little design feature on her site that highlights a line of text once you scroll to it. Here’s a video so you can see what I mean:</p>


<p>The highlighted line is done with a <code>&lt;mark&gt;</code> element in HTML, which feels right. I noticed the class name on Lene’s implementation is <code>.gsap-highlight</code> which implies GSAP is used which has as <a href="https://gsap.com/docs/v3/Plugins/ScrollTrigger/">a great Scroll Trigger plugin</a>. Let’s do this <em>without</em> JavaScript though, especially now that <a href="https://frontendmasters.com/blog/background-size-zooming-with-scroll-driven-animations/">I’m hip to Scroll-Driven Animations</a>. </p>



<h2 class="wp-block-heading" id="toc-1"><a href="#basic-html" aria-hidden="true" class="aal_anchor" id="basic-html"><svg aria-hidden="true" class="aal_svg" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Basic HTML</h2>



<p>A paragraph with a mark (with a class):</p>


<pre class="wp-block-code" aria-describedby="shcb-language-1" data-shcb-language-name="HTML, XML" data-shcb-language-slug="xml"><span><code class="hljs language-xml shcb-wrap-lines"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Lorem, ipsum dolor sit amet <span class="hljs-tag">&lt;<span class="hljs-name">mark</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"scroll-highlight"</span>&gt;</span>consectetur adipisicing elit<span class="hljs-tag">&lt;/<span class="hljs-name">mark</span>&gt;</span>. Magnam voluptas aliquid, distinctio voluptatum neque qui modi. In adipisci ratione id officiis nulla veritatis, porro explicabo illum laudantium iure eius velit!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></code></span><small class="shcb-language" id="shcb-language-1"><span class="shcb-language__label">Code language:</span> <span class="shcb-language__name">HTML, XML</span> <span class="shcb-language__paren">(</span><span class="shcb-language__slug">xml</span><span class="shcb-language__paren">)</span></small></pre>


<h2 class="wp-block-heading" id="toc-2"><a href="#css-mark-styling-in-css" aria-hidden="true" class="aal_anchor" id="css-mark-styling-in-css"><svg aria-hidden="true" class="aal_svg" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>CSS Mark Styling in CSS</h2>



<p>I just want a solid background on the mark. But I’m <em>not</em> going to use <code>background-color</code>. Instead I’m going to use <code>background-image</code>, because then I can control the <code>background-size</code> which I ultimately want to animate. So:</p>


<pre class="wp-block-code" aria-describedby="shcb-language-2" data-shcb-language-name="CSS" data-shcb-language-slug="css"><span><code class="hljs language-css"><span class="hljs-selector-tag">mark</span><span class="hljs-selector-class">.scroll-highlight</span> {
<span class="hljs-attribute">background-size</span>: <span class="hljs-number">100%</span> <span class="hljs-number">100%</span>;
<span class="hljs-attribute">background-repeat</span>: no-repeat;
<span class="hljs-attribute">background-color</span>: transparent;
<span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">linear-gradient</span>(purple, purple);
}</code></span><small class="shcb-language" id="shcb-language-2"><span class="shcb-language__label">Code language:</span> <span class="shcb-language__name">CSS</span> <span class="shcb-language__paren">(</span><span class="shcb-language__slug">css</span><span class="shcb-language__paren">)</span></small></pre>


<figure class="wp-block-image size-large"><img decoding="async" src="https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/Screenshot-2024-01-23-at-2.20.48%E2%80%AFPM-1024x322.png?resize=1024%2C322&amp;ssl=1" alt="" class="wp-image-643" data-recalc-dims="1"></figure>



<p>Now I can animate that <code>background-size</code> from <code>0% 100%</code> to <code>100% 100%</code> which is the look we’re after. It even works when the text breaks across lines which is a miracle.</p>



<figure class="wp-block-image size-full"><img decoding="async" src="https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.22.33.gif?resize=800%2C187&amp;ssl=1" alt="" class="wp-image-644" data-recalc-dims="1"></figure>



<p>Now it’s a matter of <em>when</em> to run the animation. </p>



<h2 class="wp-block-heading" id="toc-3"><a href="#scroll-driven-animation-for-the-mark" aria-hidden="true" class="aal_anchor" id="scroll-driven-animation-for-the-mark"><svg aria-hidden="true" class="aal_svg" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Scroll-Driven Animation for the Mark</h2>



<p>Here’s the whole trick:</p>


<pre class="wp-block-code" aria-describedby="shcb-language-3" data-shcb-language-name="CSS" data-shcb-language-slug="css"><span><code class="hljs language-css"><span class="hljs-selector-tag">mark</span><span class="hljs-selector-class">.scroll-highlight</span> {
<span class="hljs-attribute">background-size</span>: <span class="hljs-number">0</span> <span class="hljs-number">100%</span>;
<span class="hljs-attribute">background-repeat</span>: no-repeat;
<span class="hljs-attribute">background-color</span>: transparent;
<span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">linear-gradient</span>(purple, purple);

<span class="hljs-attribute">animation</span>: mark-it linear;
<span class="hljs-attribute">animation-fill-mode</span>: forwards;
<span class="hljs-attribute">animation-timeline</span>: <span class="hljs-built_in">view</span>();
<span class="hljs-attribute">animation-iteration-count</span>: <span class="hljs-number">1</span>;
<span class="hljs-attribute">animation-range</span>: contain <span class="hljs-number">0%</span> contain <span class="hljs-number">25%</span>;
}

<span class="hljs-keyword">@keyframes</span> mark-it {
0% {
<span class="hljs-attribute">background-size</span>: <span class="hljs-number">0</span> <span class="hljs-number">100%</span>;
}
100% {
<span class="hljs-attribute">background-size</span>: <span class="hljs-number">100%</span> <span class="hljs-number">100%</span>;
}
}</code></span><small class="shcb-language" id="shcb-language-3"><span class="shcb-language__label">Code language:</span> <span class="shcb-language__name">CSS</span> <span class="shcb-language__paren">(</span><span class="shcb-language__slug">css</span><span class="shcb-language__paren">)</span></small></pre>


<p>The coolest part to me is the <code>animation-range</code> which gives us the opportunity to say when to start and end the animation with a solid amount of control. In the code above we’re saying to start the animation as soon as the element is fully within the viewport, then finish when it’s 25% of the way up the currently visible viewport. </p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" src="https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.29.33@2x.png?resize=1024%2C638&amp;ssl=1" alt="" class="wp-image-645" srcset="https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.29.33@2x.png?resize=1024%2C638&amp;ssl=1 1024w, https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.29.33@2x.png?resize=300%2C187&amp;ssl=1 300w, https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.29.33@2x.png?resize=768%2C478&amp;ssl=1 768w, https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.29.33@2x.png?resize=1536%2C957&amp;ssl=1 1536w, https://i0.wp.com/frontendmasters.com/blog/wp-content/uploads/2024/01/CleanShot-2024-01-23-at-14.29.33@2x.png?resize=2048%2C1275&amp;ssl=1 2048w" sizes="(max-width: 1000px) 100vw, 1000px" data-recalc-dims="1"><figcaption class="wp-element-caption">Here the element is maybe 20% up the current viewport, so the animation is 80% finished.</figcaption></figure>



<h2 class="wp-block-heading" id="toc-4"><a href="#demo" aria-hidden="true" class="aal_anchor" id="demo"><svg aria-hidden="true" class="aal_svg" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Demo</h2>



<p>Remember <a href="https://caniuse.com/mdn-css_properties_animation-range">native support for Scroll-Driven Animations</a> is essentially Chrome ‘n’ friends only right now. You could easily treat this as a progressive enhancement, wrapping the animation stuff in a <code>@supports (animation-timeline: view()) { }</code> block or however you wanna do it.</p>

+ 2
- 0
cache/2024/index.html View File

@@ -70,6 +70,8 @@
<main>
<ul>
<li><a href="/david/cache/2024/5ff0fe74d43b57fa41db8851bc56ac88/" title="Accès à l’article dans le cache local : Highlight Text When a User Scrolls Down to That Piece of Text">Highlight Text When a User Scrolls Down to That Piece of Text</a> (<a href="https://frontendmasters.com/blog/highlight-text-when-a-user-scrolls-down-to-that-piece-of-text/" title="Accès à l’article original distant : Highlight Text When a User Scrolls Down to That Piece of Text">original</a>)</li>
<li><a href="/david/cache/2024/877ad04fd329c26c80113e15dec540df/" title="Accès à l’article dans le cache local : The Walk and Talk: Everything We Know">The Walk and Talk: Everything We Know</a> (<a href="https://craigmod.com/ridgeline/176/" title="Accès à l’article original distant : The Walk and Talk: Everything We Know">original</a>)</li>
<li><a href="/david/cache/2024/1d60fc5548a6fe61da80a4e16892fa0c/" title="Accès à l’article dans le cache local : Deep Democracy - IAPOP">Deep Democracy - IAPOP</a> (<a href="https://iapop.com/deep-democracy/" title="Accès à l’article original distant : Deep Democracy - IAPOP">original</a>)</li>

Loading…
Cancel
Save