Home > Web Front-end > JS Tutorial > body text

Blinking input effect implemented with JavaScript and jQuery_javascript skills

WBOY
Release: 2016-05-16 15:14:53
Original
1192 people have browsed it

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>

Copy after login

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);
}

Copy after login

How to use:

typewriter("code");
Copy after login

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);

Copy after login

How to use:

$("#code").typewriter();
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!