简体   繁体   English

单击注入 HTML 的按钮时,如何使 div 内容显示动画?

[英]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.

相关问题 我如何才能使该div仅在单击单选按钮时出现? - How can I make this div appear only when radio button is clicked? 我可以在Div内显示内容吗? 什么时候将单独的Div悬停在光标上? - Can I Make Content Appear Within a Div; When a Separate Div is Hovered Over With The Cursor? html - 单击按钮后显示列 - html - make column appear after clicking on button 使用JQuery,我该如何制作一个 <div> 出现,然后添加一些HTML,然后删除html然后隐藏div? - Using JQuery, how can I make a <div> appear, then add some html, then remove the html and then hide the div? 单击按钮时如何显示我的定价表? - How to make my pricing table appear when clicking a button? 我有一个记事本,每个笔记都有一个标题和主要内容。 单击标题时如何使内容出现/消失? - I have a notepad that for each note - has a title and the main content. How do I make the content appear/disappear when clicking the title? 如何使php内容出现在html div元素中 - How to make php content appear in a html div element 如何在不单击 HTML 或 JS 中的任何内容的情况下显示文本? - how can i make text appear without clicking anything in HTML or JS? 我如何使 html 代码出现在 animation 的顶部而不是后面 - how do i make the html code appear on top of the animation and not behind it 当你点击一个按钮时,我想让一个 div 出现 - I want to make a div appear when you click a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM