Home >Web Front-end >JS Tutorial >JavaScript code to animate text
This time I will bring you the JavaScript code to animate text. What are the precautions for using JavaScript code to animate text? The following is a practical case. Let’s take a look. one time.
Sometimes you want to add animation to a piece of text so that every word in it moves. You can use the following jQuery plug-in code to achieve this effect. Of course you need to combine a CSS3 transition style to achieve better results.
$.fn.animateText = function(delay, klass) { var text = this.text(); var letters = text.split(''); return this.each(function(){ var $this = $(this); $this.html(text.replace(/./g, '<span class="letter">$&</span>')); $this.find('span.letter').each(function(i, el){ setTimeout(function(){ $(el).addClass(klass); }, delay * i); }); }); };
How to use:
$('p').animateText(15, 'foo');
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese website Other related articles!
Recommended reading:
JavaScript code snippet to determine whether a date is valid
Detailed explanation of Event Loop of Node.js
JavaScript running mechanism Why JavaScript is single-threaded
The above is the detailed content of JavaScript code to animate text. For more information, please follow other related articles on the PHP Chinese website!