Gator_Bks

Script Animasi Teks Mengetik

Feb 25th, 2020 (edited)
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.56 KB | Source Code | 0 0
  1. <h1>
  2.     <span class="text-white typewrite" data-period="2000"
  3.        data-type='[ "Hello World" , "NaFaZ Was Here" , "NO SYSTEM IS SAFE"]'>
  4.         <span class="wrap"></span>
  5.     </span>
  6. </h1>
  7. <script type="text/javascript">
  8.     //=========================================//
  9.     /*/* 11) Typed Text animation (animation) */
  10.     //=========================================//
  11.  
  12.     try {
  13.         var TxtType = function (el, toRotate, period) {
  14.             this.toRotate = toRotate;
  15.             this.el = el;
  16.             this.loopNum = 0;
  17.             this.period = parseInt(period, 10) || 2000;
  18.             this.txt = '';
  19.             this.tick();
  20.             this.isDeleting = false;
  21.         };
  22.  
  23.         TxtType.prototype.tick = function () {
  24.             var i = this.loopNum % this.toRotate.length;
  25.             var fullTxt = this.toRotate[i];
  26.             if (this.isDeleting) {
  27.                 this.txt = fullTxt.substring(0, this.txt.length - 1);
  28.             } else {
  29.                 this.txt = fullTxt.substring(0, this.txt.length + 1);
  30.             }
  31.             this.el.innerHTML = '<span class="wrap">' + this.txt + '</span>';
  32.             var that = this;
  33.             var delta = 200 - Math.random() * 100;
  34.             if (this.isDeleting) { delta /= 2; }
  35.             if (!this.isDeleting && this.txt === fullTxt) {
  36.                delta = this.period;
  37.                 this.isDeleting = true;
  38.             } else if (this.isDeleting && this.txt === '') {
  39.                this.isDeleting = false;
  40.                 this.loopNum++;
  41.                 delta = 500;
  42.             }
  43.             setTimeout(function () {
  44.                 that.tick();
  45.             }, delta);
  46.         };
  47.  
  48.         function typewrite() {
  49.             if (toRotate === 'undefined') {
  50.                 changeText()
  51.             }
  52.             else
  53.                 var elements = document.getElementsByClassName('typewrite');
  54.             for (var i = 0; i < elements.length; i++) {
  55.                var toRotate = elements[i].getAttribute('data-type');
  56.                var period = elements[i].getAttribute('data-period');
  57.                if (toRotate) {
  58.                    new TxtType(elements[i], JSON.parse(toRotate), period);
  59.                }
  60.            }
  61.            // INJECT CSS
  62.            var css = document.createElement("style");
  63.            css.type = "text/css";
  64.            css.innerHTML = ".typewrite > .wrap { border-right: 0.08em solid transparent}";
  65.             document.body.appendChild(css);
  66.         };
  67.         window.onload(typewrite());
  68.     } catch(err) {
  69.  
  70.     }
  71. </script>
Tags: html js
Advertisement
Add Comment
Please, Sign In to add comment