Skip to content

Instantly share code, notes, and snippets.

@bramus
Created May 20, 2021 21:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bramus/600dd1bc3745b3b441375e68362e9a0e to your computer and use it in GitHub Desktop.
Save bramus/600dd1bc3745b3b441375e68362e9a0e to your computer and use it in GitHub Desktop.
Generate CSS ScrollTimelines
// This little piece of JS was used to generate the scroll-timelines
// used in the demo at https://codepen.io/bramus/pen/LYbBoRj
const generateCSS = () => {
const css = [];
document.querySelectorAll('section[id]').forEach((section, i) => {
const id = section.getAttribute('id');
css.push(`
@scroll-timeline section-${id}-enter {
source: auto;
start: selector(#${id}) end 0;
end: selector(#${id}) end 1;
scroll-offsets:
selector(#${id}) end 0,
selector(#${id}) end 1
;
time-range: 1s;
}
@scroll-timeline section-${id}-leave {
source: auto;
start: selector(#${id}) start 1;
end: selector(#${id}) start 0;
scroll-offsets:
selector(#${id}) start 1,
selector(#${id}) start 0
;
time-range: 1s;
}
.section-nav li > a[href="#${id}"] {
animation-timeline:
section-${id}-enter,
section-${id}-leave;
;
}
`);
});
return css.join("\n");
}
const injectCSS = (css) => {
const style = document.createElement('style');
style.innerHTML = css;
document.head.appendChild(style);
}
injectCSS(generateCSS());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment