From the code below, can we change the typing animation for the second index of strings ('an aspiring developer!') ?
document.querySelectorAll('.home-name-text').forEach(function(el) {
new Typed(el, {
strings: ['Ricko Wijaya,', 'an aspiring developer!'],
loop: true,
typeSpeed: 100,
backSpeed: 80,
startDelay: 100,
backDelay: 1500,
})
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<span class="home-name-text"></span>
You may use the
preStringTypedattribute, of the config object that you pass to theTypedconstructor, to change the speed of typing when the index of the string to be typed is1(that's the index of "an aspiring developer!" of your strings array).The idea is simple, as
preStringTypedis invoked "before each string is typed" you can, at that moment, change/update thetypeSpeedproperty of theTypedobject.We will be able to do that thanks to the parameters that
preStringTypedaccepts:arrayPos: the position (index) of the string to be typed (as we saw "Before each string is typed" is at the index1).self: is the actualTypedobject. We can use that parameter to update thetypeSpeedproperty.Basically, what we will do is when the
arrayPosis1we increase the speed (as you requested), otherwise we set it to100(as your original code). You can play around that and change the values per your requirements.To illustrate here's a live demo:
The above solution works as needed, but personally, I would like to make things even more dynamic. The idea here is to have a separate object that has a key-value pairs structure where the keys are the indexes of the desired string that is found in the strings array and the values are the speeds to use for the string at that index.
Here's a demo to illustrate: