mirror of https://github.com/sveltejs/svelte
32 lines
690 B
32 lines
690 B
6 years ago
|
<script>
|
||
|
let visible = false;
|
||
|
|
||
4 years ago
|
function typewriter(node, { speed = 1 }) {
|
||
2 years ago
|
const valid = node.childNodes.length === 1 && node.childNodes[0].nodeType === Node.TEXT_NODE;
|
||
6 years ago
|
|
||
|
if (!valid) {
|
||
|
throw new Error(`This transition only works on elements with a single text node child`);
|
||
|
}
|
||
|
|
||
|
const text = node.textContent;
|
||
4 years ago
|
const duration = text.length / (speed * 0.01);
|
||
6 years ago
|
|
||
|
return {
|
||
|
duration,
|
||
2 years ago
|
tick: (t) => {
|
||
3 years ago
|
const i = Math.trunc(text.length * t);
|
||
6 years ago
|
node.textContent = text.slice(0, i);
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
</script>
|
||
|
|
||
|
<label>
|
||
2 years ago
|
<input type="checkbox" bind:checked={visible} />
|
||
6 years ago
|
visible
|
||
|
</label>
|
||
|
|
||
|
{#if visible}
|
||
2 years ago
|
<p transition:typewriter>The quick brown fox jumps over the lazy dog</p>
|
||
5 years ago
|
{/if}
|