Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to animate a change to innerHTML using CSS only?

My pure-JS script is changing the text inside a <p> element simply by using innerHTML. Is it possible to animate this change with CSS only, without using jQuery? If so, how?

Thanks!

like image 473
Fabio Bracht Avatar asked Aug 30 '15 08:08

Fabio Bracht


1 Answers

Before setting innerHTML add some class to the container, which through CSS set the pre-animation state, then set innerHTML and remove that class. if the container has transition set it should animate to clean state.

 .container {
        transition: all 1s;
        max-height: 300px;
    }

    .container.pre-animation {
        opacity:0;
        max-height: 0;
    }

setTimeout to make sure it effect more visible

var innerHTMLString = '<h1> I am H1 </h1>';

var container = document.querySelector('.container')

container.classList.add('pre-animation');
container.innerHTML = innerHTMLString +'ravi';
setTimeout(function(){
    container.classList.remove('pre-animation')
},100)
like image 67
Ravi Hamsa Avatar answered Nov 07 '22 03:11

Ravi Hamsa