mirror of
https://github.com/sveltejs/svelte.git
synced 2024-12-01 17:30:59 +01:00
36 lines
665 B
Svelte
36 lines
665 B
Svelte
<script>
|
|
let visible = false;
|
|
|
|
function typewriter(node, { speed = 50 }) {
|
|
const valid = (
|
|
node.childNodes.length === 1 &&
|
|
node.childNodes[0].nodeType === 3
|
|
);
|
|
|
|
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;
|
|
|
|
return {
|
|
duration,
|
|
tick: t => {
|
|
const i = ~~(text.length * t);
|
|
node.textContent = text.slice(0, i);
|
|
}
|
|
};
|
|
}
|
|
</script>
|
|
|
|
<label>
|
|
<input type="checkbox" bind:checked={visible}>
|
|
visible
|
|
</label>
|
|
|
|
{#if visible}
|
|
<p in:typewriter>
|
|
The quick brown fox jumps over the lazy dog
|
|
</p>
|
|
{/if} |