简体   繁体   English

如何使用innerHTML平滑地进行文本过渡,并在更改时淡入/淡出?

[英]How can I make a text transition with innerHTML smooth, with a fade in/out when it changes?

This is my first post so if I'm a bit off on the format of posting or something, please forgive me. 这是我的第一篇文章,因此,如果我对发布的格式不满意,请原谅。 So I'm coding a website for a gaming team, and they want their roster (there are boxes that have the player names in them) to expand and give some info when you hover over a name. 因此,我正在为一个游戏团队编写一个网站,他们希望他们的名册(其中有包含玩家姓名的盒子)能够扩展,并在您将鼠标悬停在姓名上时提供一些信息。 I've gotten it to expand and change the text, but it looks a bit abrupt. 我已经得到它来扩展和更改文本,但它看起来有点突然。 Is there a way to make the text change smoother? 有没有一种方法可以使文本更改更流畅? I can't have a seperate CSS for each one because there will be at least 20 of them. 我不能为每一个单独的CSS,因为至少会有20个CSS。 My progress so far is below. 到目前为止,我的进展如下。 Thanks in advance! 提前致谢!

  function replace(element, replacement) { element.innerHTML = replacement; } function unreplace(element, replacement) { element.innerHTML = replacement; } 
 .box { background-color: #81C441; display: inline-block; margin: 0px 10px; text-align: center; line-height: 180px; width: 200px; height: 180px; margin-bottom: 20px; font-family: arial; transition: 1s; transform: scale(1.0); } .box:hover { transition: 1s; transform: scale(1.1); } 
 <div class="boxcontainer"> <div onmouseover="replace(this, 'ROLE')" onmouseout="unreplace(this, 'NAME')" class="box w3-center w3-animate-top">NAME</div> </div> 

You cannot (easily) animate change of content - by the same logic, that you cannot animate font-family for example. 您不能(轻松地)为content变化制作动画-例如,基于同样的逻辑,您不能为font-family制作动画。

You can however overlap the two texts and just change their properties by hovering over a parent element and not using JS. 但是,您可以将两个文本重叠,只需将它们悬停在父元素上而不使用JS即可更改其属性。

Demo included. 包括演示。

 .wrapper { position: relative; } .toHide, .toShow { transition: opacity 1s ease-in-out; position: absolute; } .toShow { opacity: 0; } .wrapper:hover .toHide { opacity: 0; } .wrapper:hover .toShow { opacity: 1; } 
 <div class="wrapper"> <span class="toHide">NAME</span> <span class="toShow">POSITION</span> </div> <br> <div class="wrapper"> <span class="toHide">NAME</span> <span class="toShow">POSITION</span> </div> <br> <div class="wrapper"> <span class="toHide">NAME</span> <span class="toShow">POSITION</span> </div> <br> <div class="wrapper"> <span class="toHide">NAME</span> <span class="toShow">POSITION</span> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM