Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index variables for animating using CSS #57

Open
flayks opened this issue Feb 13, 2023 · 4 comments
Open

Add index variables for animating using CSS #57

flayks opened this issue Feb 13, 2023 · 4 comments

Comments

@flayks
Copy link

flayks commented Feb 13, 2023

I just discovered the lib recently, it works great and is very lightweight, thanks for all the work 🙌

In order to animate characters, words and lines using CSS, it would be amazing to have some variables attached to each span, like so:

<!-- for chars -->
<span class="word" style="--index-word: 0; display: inline-block;">
    <span class="char" style="--index-char: 0; display: inline-block;">W</span>
    <span class="char" style="--index-char: 1; display: inline-block;">o</span>
    <span class="char" style="--index-char: 2; display: inline-block;">r</span>
    <span class="char" style="--index-char: 3; display: inline-block;">d</span>
</span>

<!-- for words -->
<span class="word" style="--index-word: 0; display: inline-block;">Word1</span>
<span class="word" style="--index-word: 1; display: inline-block;">Word2</span>

<!-- for lines: "--index-line: x;" -->

That way, it would really help animate text using pure CSS transitions and add a stagger effect with a simple transition-delay: calc(50ms * var(--index-word)), then trigger visibility classes with an IntersectionObserver for instance.

I've implemented it in a similar way with a rusty function that only splits words and chars independently: (using Svelte each loop, adding the index to it)

<span class="word">
    {#each split as char, i}
        <span class="char" style:--i-c={i}>{char}</span>
    {/each}
</span>
@lukePeavey
Copy link
Owner

Yes, we could probably add this feature.

Would nth child selectors not work?

@flayks
Copy link
Author

flayks commented Feb 13, 2023

They would definitely work but depending on each text, that would need a lot of CSS code for either 4 or 17 characters for instance. That's why a CSS variable is probably the best way imo as it will not impact the CSS selectors and allow quick calcs.

@lukePeavey
Copy link
Owner

Sorry, I didn't read this carefully... I thought you were talking about incremented classnames.

Indexed css variables is an interesting idea. This would make it possible to calculate duration, delay, etc for each element based on its index, with pure css. Is that correct?

I'm on board for adding this feature

@flayks
Copy link
Author

flayks commented Feb 14, 2023

That's it! It would be very versatile and lightweight for people wanting to avoid js libraries to animate everything 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants