Home  >  Article  >  Web Front-end  >  Blinking input effect implemented with JavaScript and jQuery_javascript skills

Blinking input effect implemented with JavaScript and jQuery_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:14:531192browse

The example in this article describes the blinking input effect implemented by JavaScript and jQuery. Share it with everyone for your reference, the details are as follows:

html part

<div id="code">
  <p>/**</p>
  <p>*2014-2-12</p>
  <p>*代码自动闪烁输入</p>
  <p>*/</p>
  2014-2-14,I want to say:<br />
  Baby, I love you forever!<br />
</div>

js part

function typewriter(id){
  var $ele = document.getElementById(id);
  var str = $ele.innerHTML, progress = 0;
  $ele.innerHTML = '';
  var timer = setInterval(function() {
    var current = str.substr(progress, 1);
    if (current == '<') {
      progress = str.indexOf('>', progress) + 1;
    } else {
      progress++;
    }
    $ele.innerHTML =str.substring(0, progress) + (progress & 1 &#63; '_' : '');
    if (progress >= str.length) {
      clearInterval(timer);
    }
  }, 75);
}

How to use:

typewriter("code");

Make it a jquery plug-in

(function($) {
  $.fn.typewriter = function() {
    var $ele = $(this), str = $ele.html(), progress = 0;
    $ele.html('');
    var timer = setInterval(function() {
      var current = str.substr(progress, 1);
      if (current == '<') {
        progress = str.indexOf('>', progress) + 1;
      } else {
        progress++;
      }
      $ele.html(str.substring(0, progress) + (progress & 1 &#63; '_' : ''));
      if (progress >= str.length) {
        clearInterval(timer);
      }
    }, 75);
  };
})(jQuery);

How to use:

$("#code").typewriter();

Readers who are interested in more JavaScript-related content can check out the special topics on this site: "Summary of json operation skills in JavaScript", "Summary of JavaScript animation special effects and skills", " Summary of JavaScript expansion techniques", "Summary of JavaScript motion effects and techniques", "Summary of JavaScript mathematical operation usage" and "Javascript object-oriented introductory tutorial

I hope this article will be helpful to everyone in JavaScript programming.

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