Home  >  Article  >  Web Front-end  >  JavaScript code to animate text

JavaScript code to animate text

php中世界最好的语言
php中世界最好的语言Original
2018-03-16 16:18:412556browse

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, &#39;<span class="letter">$&</span>&#39;));
    $this.find(&#39;span.letter&#39;).each(function(i, el){
      setTimeout(function(){ $(el).addClass(klass); }, delay * i);
    });
  });
  
};

How to use:

$(&#39;p&#39;).animateText(15, &#39;foo&#39;);

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn