You can use a small bit of JS (uses jQuery) like in the below snippet to split the content of the element into individual characters, wrap them all into separate spans and add the class to each of them one by one. This would produce the effect of each character fading in one by one.
This answer is very similar to Farzad's answer in the sense that it uses multiple span
characters but there is a significant difference between the two. While the other answer manually adds the delay and thus makes it difficult to adapt to varying length texts, the snippet in this answer uses transitionend
event which adds the class to the next sibling span
as soon as the transition for the current span
is completed. Thus, it can automatically adapt itself to varying text lengths also.
$(document).ready(function() {
$this = $('.str');
var characters = $this.text().split("");
$this.empty();
$.each(characters, function(i, el) {
$this.append("<span>" + el + "</span");
});
$(".btn").on("click", function() {
$(".str span:nth-child(1)").addClass('present');
});
$('.str span').on('transitionend', function() {
if ($(this).next('span').length != 0)
$(this).next('span').eq(0).addClass('present');
});
});
.str span {
opacity: 0;
font-size: 32px;
font-weight: bold;
transition: all 0.35s;
}
.str .present {
opacity: 0.9;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn">Display</button>
<div class="str">String</div>
Note: The code for splitting the contents into individual characters is not my own. It was picked from a Stack Overflow answer long time back and I don't have the link to it at present.