Last active
May 16, 2020 18:38
-
-
Save leemartin/cbbae8eda29dd47a65aceb047bf9c935 to your computer and use it in GitHub Desktop.
The "Hitchhiker Typer" component used on Shelter In Space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<p><span class="start">{{ start }}</span><span class="end">{{ end }}</span></p> | |
</template> | |
<script> | |
export default{ | |
props: { | |
phrase: { | |
type: String, | |
required: true | |
} | |
}, | |
data() { | |
return { | |
index: 0 | |
} | |
}, | |
computed: { | |
typed() { | |
return phrase.substring(0, this.index) | |
}, | |
start() { | |
return this.typed.substring(0, this.typed.length - 1) | |
}, | |
end() { | |
return this.typed[this.typed.length - 1] | |
} | |
}, | |
methods: { | |
startTyping() { | |
this.typing = setInterval(() => { | |
this.index += 1 | |
if (this.index > this.phrase.length) { | |
this.stopTyping() | |
} | |
}, 50) | |
}, | |
stopTyping() { | |
clearInterval(this.typing) | |
} | |
}, | |
mounted() { | |
this.startTyping() | |
}, | |
beforeDestroy() { | |
this.stopTyping() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment