Created
September 3, 2021 08:36
-
-
Save bramus/dfe43dd15158f0823eccb43bb7818cd8 to your computer and use it in GitHub Desktop.
Generate CSS ScrollTimelines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This little piece of JS was used to generate the scroll-timelines | |
// used in the demo at https://codepen.io/bramus/pen/wveWXdw | |
const generateCSS = () => { | |
let css = ''; | |
document.querySelectorAll('.slide').forEach((li, i) => { | |
const id = li.getAttribute('id'); | |
const parentId = li.parentElement.getAttribute('id'); | |
css = ` | |
${css} | |
@scroll-timeline scroll-${id}-in-parent-${parentId} { | |
source: selector(#${parentId}); | |
orientation: inline; | |
start: selector(#${id}) end 0; | |
end: selector(#${id}) start 0; | |
scroll-offsets: | |
selector(#${id}) end 0, | |
selector(#${id}) start 0 | |
; | |
} | |
#${id} > img { | |
animation-timeline: scroll-${id}-in-parent-${parentId}; | |
} | |
`; | |
}); | |
return css; | |
} | |
const injectCSS = (css) => { | |
const style = document.createElement('style'); | |
style.innerHTML = css; | |
document.head.appendChild(style); | |
} | |
const css = generateCSS(); | |
console.log(css); | |
injectCSS(generateCSS()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment