Prakhar Thakur
Prakhar Thakur

Reputation: 1229

fading-in every word of a paragraph in different random time effect

I am trying to fade-in every word of a paragraph with random speed, so that it creates an effect of words appearing on paper. I am using jquery and doing it in this way. but this seams quite heavy (is it so?). please suggest a better way to do so.

$('body').children('.word').each(function() {
  $(this).animate({
    "opacity": "1"
  }, Math.floor(Math.random() * 3000) + 500);
});
.word {
  opacity: 0;
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="word">
  hello,
</div>
<div class="word">
  how
</div>
<div class="word">
  are
</div>
<div class="word">
  you
</div>
<div class="word">
  doing
</div>

Upvotes: 1

Views: 838

Answers (1)

Rodion
Rodion

Reputation: 521

Another way is using css transitions

JS:

$('body').children('.word').each(function() {
    var word = this;
    setTimeout(function () {
        $(word).css("opacity", 1);
    }, Math.random() * 3000)
});

CSS:

.word {
  opacity: 0;
  display: inline-block;
  transition: opacity 1s linear;
}

http://jsfiddle.net/n427mLb8/

You could eliminate using wrappers around each word using javascript like that:

$('.content').html($('.content').text().split(/[\s,\.]+/).map(function (word) {
    return '<div class="word">' + word + '</div>'
}).join(''))

http://jsfiddle.net/6e948j26/

Upvotes: 1

Related Questions