0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00
svelte/site/content/tutorial/10-transitions/05-custom-js-transitions/text.md
Puru Vijay db2d07f236
feat(site-2): Update links, move blog out of SK folder to content (#8289)
* Update links

* Move blog to site/content

* Update site/content/docs/02-component-format.md

* Fix docs links

* Add global prettierrc

* Auto format

* Fix git merge artifact

* Fix errors

* Update html to svelte(remaining ones)

* Add 2 blog posts

* Modify prettierrc

* Minor design fix

* Switch package lock to spaces, prettier ignore

* Regenerate package lock

* prettier format

* Update deps

* Hack the build into working

* add missing blog post

---------

Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
Co-authored-by: Rich Harris <git@rich-harris.dev>
2023-04-02 11:24:33 -04:00

677 B

title
Custom JS transitions

While you should generally use CSS for transitions as much as possible, there are some effects that can't be achieved without JavaScript, such as a typewriter effect:

function typewriter(node, { speed = 1 }) {
	const valid = node.childNodes.length === 1 && node.childNodes[0].nodeType === Node.TEXT_NODE;

	if (!valid) {
		throw new Error(`This transition only works on elements with a single text node child`);
	}

	const text = node.textContent;
	const duration = text.length / (speed * 0.01);

	return {
		duration,
		tick: (t) => {
			const i = Math.trunc(text.length * t);
			node.textContent = text.slice(0, i);
		}
	};
}