Reputation: 1229
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
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;
}
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(''))
Upvotes: 1