简体   繁体   English

CSS:过渡无法在悬停上进行

[英]CSS: Transition not working on hover

I have a box containing an image and a username. 我有一个包含图像和用户名的盒子。 Initially the width is fixed to save space and on hover I want the division to expand to reveal the full content. 最初,宽度是固定的,以节省空间,并且在hover我希望分区扩大以显示全部内容。 I have achieved this but I am not able to add transition to make the animation smooth. 我已经实现了这一点,但是我无法添加transition以使动画流畅。

Why is it not working? 为什么不起作用?

 .peer-person { position: relative; z-index: 1000; width: 9.5%; min-width: 66px; margin: 0px 0px 5px 0px; padding: 6px 0px 5px 0px; display: inline-block; border: 2px solid #efefef; border-radius: 5px; -webkit-transition: all 1s ease-in-out 0s; -moz-transition: all 1s ease-in-out 0s; transition: all 1s ease-in-out 0s; } .hover-container:hover>.peer-person { z-index: 1001; background-color: white; width: auto; } .hover-container { display: inline-block; width: 9.5%; min-width: 66px; } .peer p { margin: 0px; padding: 3px; padding-bottom: 0px; color: grey; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } 
 <span class="hover-container"> <span class="peer-person"> <a href="#" role="button" class="peer" aria-expanded="false"> <img src="http://via.placeholder.com/36x36" class="img-circle"> <p>1.LONGUSERNAMELONGLONG</p> </a> </span> </span> <span class="hover-container"> <span class="peer-person"> <a href="#" role="button" class="peer" aria-expanded="false"> <img src="http://via.placeholder.com/36x36" class="img-circle"> <p>2.LONGUSERNAMELONGLONG</p> </a> </span> </span> <span class="hover-container"> <span class="peer-person"> <a href="#" role="button" class="peer" aria-expanded="false"> <img src="http://via.placeholder.com/36x36" class="img-circle"> <p>3.LONGUSERNAMELONGLONG</p> </a> </span> </span> 

The problem is that you change the width from a specific value ( 9.5% ) , to a width with a non specific value ( auto ) . 问题是您将宽度从特定值(9.5%)更改为具有非特定值的宽度(auto)。 For the transition to work you need to use a specific value on the hover state 为了使过渡正常工作,您需要在悬停状态上使用特定的值

So change from width:auto to width:100% . 因此从width:auto更改为width:100% Ok, now it works but it doesn't exactly work as you would like. 好的,现在可以了,但是并不能完全按照您的意愿工作。 Change the width of hover-container to auto . hover-container的宽度更改为auto So it will inherit the width from it's children. 因此它将继承其子级的宽度。 By setting width:9.5% to it's children, it will have the same with. 通过为其孩子设置width:9.5% ,它将与之相同。 And then , when the children expands, it will expand also. 然后,当孩子扩展时,它也会扩展。

See snippet below 请参见下面的代码段

EDIT : if you have more than one , side-by-side , use max-width:9.5% on hover-container and on hover, add styles to the container as well ( max-width:100% ) . 编辑:如果您有多个并排,请在hover-container和悬停上使用max-width:9.5% ,也hover-container添加样式(max-width:100%)。 it will expand ( with transition ) but only as much as the width of the text. 它将扩展(带有过渡),但仅扩展到文本的宽度。

 .peer-person { position: relative; z-index: 1000; width: 9.5%; min-width: 66px; margin: 0px 0px 5px 0px; padding: 6px 0px 5px 0px; display: inline-block; border: 2px solid #efefef; border-radius: 5px; -webkit-transition: all 1s ease-in-out 0s; -moz-transition: all 1s ease-in-out 0s; transition: all 1s ease-in-out 0s; } .hover-container:hover>.peer-person { z-index: 1001; background-color: white; width: 100%; } .hover-container { display: inline-block; max-width: 9.5%; min-width: 66px; -webkit-transition: all 1s ease-in-out 0s; -moz-transition: all 1s ease-in-out 0s; transition: all 1s ease-in-out 0s; } .hover-container:hover { max-width:100%; } .peer p { margin: 0px; padding: 3px; padding-bottom: 0px; color: grey; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } 
 <span class="hover-container"> <span class="peer-person"> <a href="#" role="button" class="peer" aria-expanded="false"> <img src="http://via.placeholder.com/36x36" class="img-circle"> <p>1.LONGUSERNAMELONGLONG</p> </a> </span> </span> <span class="hover-container"> <span class="peer-person"> <a href="#" role="button" class="peer" aria-expanded="false"> <img src="http://via.placeholder.com/36x36" class="img-circle"> <p>2.LONGUSERNAMELONGLONG</p> </a> </span> </span> <span class="hover-container"> <span class="peer-person"> <a href="#" role="button" class="peer" aria-expanded="false"> <img src="http://via.placeholder.com/36x36" class="img-circle"> <p>3.LONGUSERNAMELONGLONG</p> </a> </span> </span> 

You cannot use auto height/width for transition. 您不能使用auto高度/宽度进行过渡。 One of the easiest way to get the result is to set max-width/max-height with exact value to animate it using transition . 获得结果的最简单方法之一是设置具有精确值的max-width/max-height来使用transition对其进行动画处理。 Check this link for more details and other options to get output. 检查此链接以获取更多详细信息和其他选项以获取输出。

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

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