3

I am new to JavaScript and jQuery development and I want to ask what is the most simple way of making html text slowly disappear letter by letter using JavaScript?

Thanks.

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
Shaker26
  • 41
  • 3
  • Have a look at http://stackoverflow.com/a/11665782/1048572. You need the same code structure, only that you remove elements instead of appending them. – Bergi Apr 25 '13 at 17:25

1 Answers1

4

Since I got even more curious, I think I created the exact one that you are looking for,

DEMO: http://jsfiddle.net/DbknZ/3/

$(function() {
  var $test = $('#test');
  var initText = $.trim($test.text()), ptr = 0;
  var timer = setInterval(function() {
    var ln = $.trim($test.find('.trans').text().length);
    if (ln == initText.length) {
        $test.empty();
        clearInterval(timer);
    }

    $('#test').html(function() {    

        return $('<span>').addClass('removeMe')
            .html(initText.substring(ptr++ , ptr))
            .before($('<span>').addClass('trans').
                   html(initText.substring(0 , ptr-1)))
            .after(initText.substring(ptr));
    }).find('span.removeMe').animate({'opacity': 0}, 10);

  }, 20);
});

I just got curious and wrote a small thing for you.. which could be a starter..

Basically it is a timer which removes letter by letter.. See below.

DEMO: http://jsfiddle.net/TTv7L/1/

HTML:

<div id="test">
   This is a test page to demonstrate text disapper letter by letter
</div>

JS:

$(function () {
    var $test = $('#test');
    var timer = setInterval( function () {
        var ln = $test.text().length;
        if (ln == 0) clearInterval(timer);

        $('#test').text(function (i, v) {
            return v.substring(1);
        });
    }, 100);
});
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134