{"id":4365,"date":"2024-11-11T12:36:23","date_gmt":"2024-11-11T17:36:23","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=4365"},"modified":"2024-11-21T13:22:13","modified_gmt":"2024-11-21T18:22:13","slug":"scoped-scroll-timelines","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/scoped-scroll-timelines\/","title":{"rendered":"(Up-) Scoped Scroll Timelines"},"content":{"rendered":"\n<p>I keep learning little details about scroll-driven animations! <\/p>\n\n\n\n<p>I started this little journey thinking about if you wanted to do <a href=\"https:\/\/frontendmasters.com\/blog\/scroll-driven-sections\/\">special styling when a page scrolled through a certain section<\/a>. I thought then that in order to pass scrolling information to descendants, you&#8217;d have to do it with <code>--custom-properties<\/code>. That&#8217;s sometimes still a decent idea, but it&#8217;s not strictly true, as <a href=\"https:\/\/frontendmasters.com\/blog\/named-scroll-view-timelines\/\">those descendants can inherit a named timeline<\/a> and tap into that to do styling on themselves. <\/p>\n\n\n\n<p>Then I thought, while that&#8217;s a nice improvement, it&#8217;s still limited in the sense that only descendants can tap into a higher-up-the-DOM element&#8217;s timeline. Like an enforced parent\/child situation. Turns out this isn&#8217;t true either, and again <a href=\"https:\/\/frontendmasters.com\/blog\/named-scroll-view-timelines\/#comment-14690\">thanks to Bramus<\/a> for showing me how it works. <\/p>\n\n\n\n<p>Since we&#8217;re three-deep here on this journey, I figure calling it a series makes sense:<\/p>\n\n\n<div class=\"box article-series\">\n  <header>\n    <h3 class=\"article-series-header\">Article Series<\/h3>\n  <\/header>\n  <div class=\"box-content\">\n            <ol>\n                      <li>\n              <a href=\"https:\/\/frontendmasters.com\/blog\/scroll-driven-sections\/\">Scroll-Driven&#8230; Sections<\/a>\n            <\/li>\n                      <li>\n              <a href=\"https:\/\/frontendmasters.com\/blog\/named-scroll-view-timelines\/\">Named Scroll &amp; View Timelines<\/a>\n            <\/li>\n                      <li>\n              <a href=\"https:\/\/frontendmasters.com\/blog\/scoped-scroll-timelines\/\">(Up-) Scoped Scroll Timelines<\/a>\n            <\/li>\n                  <\/ol>\n        <\/div>\n<\/div>\n\n\n\n<p class=\"learn-more\">Fair warning all this stuff is Chrome &#8216;n&#8217; friends only right now. But I&#8217;ve seen flags in both Safari and Firefox so it&#8217;s coming along.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">You can have any element on the page &#8220;listen&#8221; to a scroll (or view) timeline of a totally different element.<\/h2>\n\n\n\n<p>That&#8217;s the rub. <\/p>\n\n\n\n<p>I wrongly assumed it had to be a parent\/child thing (or parent\/descendant). By default, that&#8217;s true, but if you intentionally move the scope of the timeline to another element up the DOM, you can make it work for any elements. <\/p>\n\n\n\n<p>I&#8217;ll illustrate:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"552\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/11\/timeline-diagram.png?resize=1024%2C552&#038;ssl=1\" alt=\"\" class=\"wp-image-4377\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/11\/timeline-diagram.png?resize=1024%2C552&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/11\/timeline-diagram.png?resize=300%2C162&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/11\/timeline-diagram.png?resize=768%2C414&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/11\/timeline-diagram.png?resize=1536%2C828&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/11\/timeline-diagram.png?w=1676&amp;ssl=1 1676w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>Demo of that:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_LYwMdVq\" src=\"\/\/codepen.io\/anon\/embed\/LYwMdVq?height=450&amp;theme-id=47434&amp;slug-hash=LYwMdVq&amp;default-tab=result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed LYwMdVq\" title=\"CodePen Embed LYwMdVq\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>The idea above is that you scroll the element on the top and the element below rotates. They are sibling elements though, so this is only possibly by &#8220;hoisting&#8221; the <code>scroll-timeline<\/code> to a higher-in-the-DOM element with <code>timeline-scope<\/code> so that the other element can pick up on it.<\/p>\n\n\n\n<p>My ridiculous head thought of trying to make a quiz game or some kind of unlocking puzzle with getting scroll positions just right. I proved out the idea here:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_eYqPWjK\" src=\"\/\/codepen.io\/anon\/embed\/eYqPWjK?height=750&amp;theme-id=47434&amp;slug-hash=eYqPWjK&amp;default-tab=css,result\" height=\"750\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed eYqPWjK\" title=\"CodePen Embed eYqPWjK\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>There are a bunch of abused CSS tricks in there:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Declare a custom property with @property so it&#8217;s value can be animated<\/li>\n\n\n\n<li>Make a scrolling element with a <code>scroll-timeline<\/code><\/li>\n\n\n\n<li>Hoist that timeline up to a parent element<\/li>\n\n\n\n<li>Have the &#8220;number&#8221; element explicitly use that timeline<\/li>\n\n\n\n<li>Make the @keyframes animate that <code>&lt;integer&gt;<\/code> custom property<\/li>\n\n\n\n<li>Display the number using a pseudo element and <code>counter()<\/code><\/li>\n\n\n\n<li>Use <code>@container style()<\/code> queries to check when the custom property is exactly the &#8220;answer&#8221; and update styling.<\/li>\n<\/ol>\n\n\n\n<p>Phew. It all kinda leads up to that very last step where we can react to a value that came from a user scrolling. It might be a fun little project to build a bike lock number-twister thing with this.<\/p>\n\n\n<div class=\"box article-series\">\n  <header>\n    <h3 class=\"article-series-header\">Article Series<\/h3>\n  <\/header>\n  <div class=\"box-content\">\n            <ol>\n                      <li>\n              <a href=\"https:\/\/frontendmasters.com\/blog\/scroll-driven-sections\/\">Scroll-Driven&#8230; Sections<\/a>\n            <\/li>\n                      <li>\n              <a href=\"https:\/\/frontendmasters.com\/blog\/named-scroll-view-timelines\/\">Named Scroll &amp; View Timelines<\/a>\n            <\/li>\n                      <li>\n              <a href=\"https:\/\/frontendmasters.com\/blog\/scoped-scroll-timelines\/\">(Up-) Scoped Scroll Timelines<\/a>\n            <\/li>\n                  <\/ol>\n        <\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>You can give a name (&#8220;custom ident&#8221;) to any scrolling element&#8217;s &#8220;timeline&#8221;, have a parent element pick it up, then have any other element use it for their own animation timeline. It&#8217;s a trip! <\/p>\n","protected":false},"author":1,"featured_media":4377,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[7,57],"class_list":["post-4365","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-css","tag-scroll-driven-animations"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/11\/timeline-diagram.png?fit=1676%2C904&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/4365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=4365"}],"version-history":[{"count":8,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/4365\/revisions"}],"predecessor-version":[{"id":4573,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/4365\/revisions\/4573"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/4377"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=4365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=4365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=4365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}