简体   繁体   中英

Make 'cursor' blink and fadeOut using jquery for typewriter effect

I am using jquery to set up a typewriter effect on a div. I am not using css to do this because the sentence will be cover more than 1 line. The problem I am facing is getting the cursor to blink, then fade away when line is typed.

Html:

<div class="typewriter">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>

JS

/*****Start Typewriter effect*****/
  $('.typewriter').css('display', 'none');
  setTimeout(function(){
    $('.typewriter').css('display', 'flex');

    var str = $('.typewriter').html() + 1,
    i = 0,
    isTag,
    text,
    cursor = "|",
    timer;

    (function type() {
      text = str.slice(0, ++i);
      if (text === str){ 
        return;
      }
      $('.typewriter').html(text + " " + cursor);
      timer = setTimeout(type, 40);
    }());
  }, 300);
  /*****End Typewriter effect*****/

Here is a jsfiddle https://jsfiddle.net/ht4569wv/

Just validate when the effect you already made is done and set up another timer to blink the cursor:

/*****Start Typewriter effect*****/
  $('.typewriter').css('display', 'none');
  setTimeout(function(){
    $('.typewriter').css('display', 'flex');

    var str = $('.typewriter').html(),
    i = 0,
    isTag,
    text,
    cursor = "|",
    timer;

    (function type() {
      text = str.slice(0, ++i);
      if (text === str){ 
        i = 0;
        blink();
        return;
      }
      $('.typewriter').html(text + " " + cursor);
      timer = setTimeout(type, 40);
    }());

    function blink() {
      i++;
      const foo = str + " " + (i%2 ? cursor : '');
      $('.typewriter').html(foo);
      if (i < 10) timer = setTimeout(blink, 600);
      else fade();
    }

    function fade() {
        $('.typewriter').html(str);
    }

  }, 300);
  /*****End Typewriter effect*****/

demo: https://jsfiddle.net/y2s3fv6d/

I have slightly changed your approach, and with the help of CSS I have created a blinking cursor.

here is the JSfiddle: https://jsfiddle.net/mkbctrlll/65ay3q8o/72/

JS:

var $typewriter = $('.typewriter')

/*****Start Typewriter effect*****/
setTimeout(function() {

  console.log('Start!')
  $typewriter.css('display', 'block');

  var str = $typewriter.html() + 1,
    i = 0,
    isTag,
    text,
    cursor = document.createElement('span'),
    timer;

  cursor.classList.add('cursor');


  (function type() {
    text = str.slice(0, ++i);

    if (text === str) {
        console.log('Done')

      setTimeout(function() {
        $(cursor).addClass('hidden')
      }, 2000);
      return;
    }

    $typewriter.html(text + " ");
    $typewriter.append(cursor)

    timer = setTimeout(type, 0);

  }());
}, 300);

/*****End Typewriter effect*****/

CSS:

.typewriter {
  display: none;
  overflow: hidden;
  /* Ensures the content is not revealed until the animation */
}

.cursor {
  transition: opacity 0.6s;
  border-right: .15em solid orange; /* The typwriter cursor */
  animation: blink-caret .5s step-end infinite;
}

.cursor.hidden {
   opacity: 0
}

/* The typewriter cursor effect */

@keyframes blink-caret {
  from,
  to {
    border-color: transparent
  }
  50% {
    border-color: orange
  }
}

Use my .typeText function.

 //<![CDATA[ /* external.js */ $(function(){ // jQuery onload $.fn.extend({ typeText:function(interval){ var t = this, s = this.text().split(''), ti, i = 0; this.text(s[0]+'|'); ti = setInterval(function(){ t.text(t.text().replace(/\\|$/, '')); if(s[++i]){ t.append(s[i]+'|'); } else{ clearInterval(ti); } }, interval); } }); $('.typeview').css('display', 'block').each(function(i, e){ $(e).typeText(50); }); }); // jQuery onload end //]]> 
 /* external.css */ *{ box-sizing:border-box; padding:0; margin:0; } html,body{ width:100%; height:100%; } body{ background:#ccc; } #content{ padding:10px; } .typeview{ display:none; text-align:justify; background:#fff; padding:8px 10px; } 
 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'> <head> <meta charset='UTF-8' /><meta name='viewport' content='width=device-width, height=device-height, initial-scale:1' /> <title>Test Template</title> <link type='text/css' rel='stylesheet' href='external.css' /> <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script> <script type='text/javascript' src='external.js'></script> </head> <body> <div id='content'> <div class='typeview'> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> </div> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM