Jump to content
GreenSock

a.joshi84

Text Hover Effect

Go to solution Solved by Dipscom,

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hi a.joshi85,

 

Welcome to the forums!

 

All you have to do there is to break each letter of the text you want to animate into an individual tag element, add your desired animation and trigger it with the 'mouseover' event.

 

You can either, use GSAP's SplitText, write your own function to add the spans or divs as necessary in your text or, painstainkly add the span and div tags by hand (as I have done in the example).

 

See the Pen 6fec6d0b125fe69b186e9b139ccbc01c by dipscom (@dipscom) on CodePen

  • Like 5
Link to comment
Share on other sites

  • 3 years later...

Hey @Dipscom!

 

I've made it work with Splitext but all the words come up as opposed to just letters because im targeting the div. Any way to target Split Text than by div? 

Link to comment
Share on other sites

Hey @kennyopr!

 

Can you show a reduced case of what you have achieved? Because, there are several ways to split the text and I'm not quite following what you are saying here.

  • Like 2
Link to comment
Share on other sites

@Dipscom This is what I have achieved so far.  See if you go around the words everything just go up? Because when splittext separates it creates divs but i cannot add classes to characters or spans to identify just the letters. 

 

See the Pen gOzPpwB by kenneth-brian-flores (@kenneth-brian-flores) on CodePen

Link to comment
Share on other sites

  • Solution

Right, I see, you've copied my example and tried to adapt it to using SplitText.

 

You are using an outdated syntax and are missing a few little bits. Have a look at the SplitText docs as well. You will see there is already a ton of functionality there.

 

I recommend you also read the get started section in the site here so you can be familiar with the current GSAP syntax.

 

Finally, below is your code refactored to achieve a pleasant animation. The key points there are: Add a class to each split letter, creating a "dictionary" of tweens, storing an "index" reference of the dictionary entry on each letter as a data-attribute and checking to see if the element hovered over has said dictionary reference and if the tween is playing

 

new SplitText(".wtg", {charsClass: "letter"});

const element = document.querySelector('.wtg');
const letters = gsap.utils.toArray(".letter");
const tweens = {};

element.addEventListener('mouseover', onMouseOver);

letters.forEach((letter, index) => {
   tweens[index] = gsap.to(letter, {yPercent: -50, yoyo:true, repeat:1, paused: true});
  
  letter.dataset.tween = index;
})

function onMouseOver(event) {
  const trg = event.target;
  
  if(trg.dataset.tween) {
    tween = tweens[trg.dataset.tween];
    
    if (!gsap.isTweening(trg)) {
      tween.play(0);
    }
  }
}

 

  • Like 3
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×