Jump to content
Search Community

Split Text - Bold?

Lawrie test
Moderator Tag

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,

I really like the SplitText utility, but I'm having some problems with it.

Is it possible to animate a block of text that contains some bold words? So far, SplitText seems to disregard any <b> or <strong> tags, so the animated elements are not bold, and then the bold "pops back" on when I revert to the original text.

 

I've forked the SplitText Starter Pen here, though I have just added the <b> tags on line 5 of the html, and an onComplete function to revert the SplitText.

 

See the Pen Iqhfm by LawrieCape (@LawrieCape) on CodePen



Thanks.

Lawrie.

Link to comment
Share on other sites

  • 1 year later...

--EDIT--

If the formatted text wraps over 2 lines this would not work either which would be a huge drawback. So I would strongly advise against this code and can't see how to delete this post at the moment.

 

 

You could do a little workaround, but I wouldn't bank on the automatic line breaks working. For small formatting such as a couple of bold words, or italics this doesn't do too badly. This is by no means full proof and wouldn't work for example if you have the same text in 2 places with the second being bold. 

var $mainText = $(".mainText"),//the container holding paragraph tags
                //select just the paragraph tags (I have a div in there as well that I will leave out of the animation as it doesn't contain text)
		$p = $mainText.children("p"),
                //jquery - clone all the children in each paragraph, which you should only have tags around areas of text that you'd like to format
		$newChildren = $p.children().clone(true);

                //splitText as normal
		var splitMainText = new SplitText($p,{type:"lines"});

                //loop through the resulting lines
		$(splitMainText.lines).each(function(){
			var $splitTextLine = $(this),
			lineText = $splitTextLine.html();
    
                        //loop through the formatted areas you cloned earlier
    			$newChildren.each(function(){
                                        //put this in a container so we can find the HTML and Text equivalent that it will have been converted to
				        var $newChild = $(this),
				        $tempDiv = $("<div />");
				        $tempDiv.append($newChild);

                                        //get the text equivalent of this formatted area and check if that is in this line
				        var changedToText = $tempDiv.text(),
        				indexx = lineText.indexOf(changedToText);
				        if(indexx!==-1){
                                                    //if the text was found, replace it with the formatted version and update variables
					            var newHTML = lineText.replace(changedToText,$tempDiv.html());
					
					            $splitTextLine.html(newHTML);

					            $newChildren.detach($newChild);
					            lineText = $splitTextLine.html();
				        }
				        $tempDiv.remove();
			    });
		});
Link to comment
Share on other sites

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.
×
×
  • Create New...