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

假如想象 提交于 2021-01-27 04:17:48

问题


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!


回答1:


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)


来源:https://stackoverflow.com/questions/32294635/is-it-possible-to-animate-a-change-to-innerhtml-using-css-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!