[英]How can i make div content appear with animation when clicking a button that injects HTML?
I am currently learning on my own some CSS & JS and i'm stuck on a part i really want to work on but have trouble finding the right answers online as there seem to be a tons of methods yet i couldn't make any of them work.我目前正在自己学习一些 CSS 和 JS 并且我被困在我真正想研究的部分,但在网上找不到正确的答案时遇到了麻烦,因为似乎有很多方法,但我无法做出任何一个他们工作。
Here is a snippet of what i have in mind :这是我想到的一个片段:
let htmlcontent = `<p>It's me again!</p>`; function animation() { let content = document.getElementById("content"); content.innerHTML = ""; content.innerHTML = htmlcontent; }
#content p { font-size: 100px; }
<div id="content"> <p> Hello world! </p> </div> <button onclick="animation()"> Click here </button>
The idea is that when i click on the button, old content gets replaced by new HTML content and i want that new content to fade in from the right (a transition) every time i click the button.这个想法是,当我点击按钮时,旧内容被新的 HTML 内容替换,我希望每次点击按钮时新内容从右侧淡入(过渡)。
I'm sorry if my question is bad/weird, english isn't my primary language and i have no one else to ask at the moment.如果我的问题不好/奇怪,我很抱歉,英语不是我的主要语言,我目前没有其他人可以问。 Thank you for your patience.感谢您的耐心等待。
To trigger a CSS transition, change the CSS state after you inserted the HTML.要触发 CSS 转换,请在插入 HTML后更改 CSS 状态。 You can do this by changing a class (on the container or an inserted element).您可以通过更改类(在容器或插入的元素上)来做到这一点。
Also see here:另见此处:
Is it possible to animate a change to innerHTML using CSS only? 是否可以仅使用 CSS 对 innerHTML 的更改进行动画处理?
You could just make a CSS animation and play that whenever you click the button.您可以制作 CSS动画并在单击按钮时播放该动画。
let htmlcontent = `<p>It's me again!</p>`; let content = document.getElementById("content"); function animation() { content.innerHTML = htmlcontent; content.classList.add("animate"); setTimeout(function() { content.classList.remove("animate"); }, 500); // 500 is the same time as the CSS animation }
#content p { font-size: 100px; } .animate { animation: fadeIn 500ms ease-out backwards; } @keyframes fadeIn { from { transform: translateX(250px); opacity: 0; } to { transform: translateX(0px); opacity: 1; } }
<div id="content"> <p> Hello world! </p> </div> <button onclick="animation()"> Click here </button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.