By clicking “Accept”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy for more information.
Tutorial on creating text animations with GSAP, SplitType and custom JavaScript
I thought this might be useful for developers just getting to grips with GSAP. The possibilities for text animation really are endless, but here are 8 useful ones to begin with.
I go over the logic and exactly how to write the JavaScript in the YouTube tutorial, so this post will focus on instructions for how to use the code in your projects.
Include the CDNs
Firstly, you want to make sure you're linking to both the GSAP and SplitType libraries. Here are the CDNs:
To simplify your JavaScript, save all the elements you want to animate as variables, to make their selection in the code simpler. For the headings below, I have already intialised the SplitType object for each of them. For a detailed explanation of this, see the tutorial.
function typewriterEffect(text, typingSpeed = 90){
const typewriterText = document.getElementById('typewriter');
typewriterText.innerHTML = "";
let i = 0;
let timer = setInterval(() => {
if (i < text.length){
typewriterText.innerHTML += text.charAt(i);
i++;
} else {
clearInterval(timer)
}
}, typingSpeed);
}
Call the functions when you want the animations to play
runAnimation1();
runAnimation2();
runAnimation3();
runAnimation4();
runAnimation5();
runAnimation6();
runAnimation7();
typewriterEffect("This is text that will be typewritten!", 50);
Here are the selection of button click event listeners
So I hope this was helpful to you, we will be diving deeper into GSAP & JavaScript, so if you're interested please head over to the LIONIZE YouTube channel and like and subscribe.