html - CSS:过渡不适用于悬停

标签 html css

我有一个包含图像和用户名的框。最初宽度是固定的以节省空间,在 hover 上,我希望展开该部分以显示全部内容。我已经做到了这一点,但我无法添加 transition 来使动画流畅。

为什么它不起作用?

.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>

最佳答案

问题是您将宽度从特定值 (9.5%) 更改为具有非特定值的宽度 (auto)。为了使过渡正常工作,您需要在悬停状态上使用特定值

所以从 width:auto 更改为 width:100% 。好的,现在它可以工作了,但它并不完全像你想要的那样工作。将 hover-container 的宽度更改为 auto 。所以它将继承它的 child 的宽度。通过设置 width:9.5% 给它的子元素,它会和 width 一样。然后,当 children 展开时,它也会展开。

请看下面的片段

编辑:如果你有多个并排的,在 hover-container 上使用 max-width:9.5% 并在悬停时添加样式容器也是 ( max-width:100% ) 。它会扩展(带有过渡),但只会扩展到文本的宽度。

.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>

关于html - CSS:过渡不适用于悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46603397/

相关文章:

html - html正文中的空格

html - 淡出速度不同

javascript - 使用 vba 应用程序在 IE 中的文本字段中输入值

javascript - 我们如何在 file.txt 中找到没有 HTML 的 JavaScript 中的单词?

html - 如何在 HTML 中指定相对定位?

javascript - 如果单击消息框,防止鼠标离开元素淡入淡出

javascript - 单击时使图像填满屏幕

php - 在 PHP 中,如何使用 DOMDocument 类替换 IMG 标签的 src= 属性?

css - 如何将div定位在窗口底部?

html - 如何固定div的最大宽度?如果那个 div 的内容超过最大宽度,我们得到滚动条?