html - 在灵活高度的内容框上悬停设置宽度
问题描述
我在一个灵活的内容框上创建了一个悬停效果,它改变了内框的宽度以显示里面的文本。我需要这个框的高度灵活,以便可以添加更多内容,但是,我不希望在悬停时改变高度。
我已经在下面链接了到目前为止我得到的东西,左边的框显示了我想要的静态高度效果,右边的框显示了当我添加更多文本时会发生什么,这会导致我悬停时高度变化的问题通过将文本包装在其中。
https://jsfiddle.net/ndpty6xa/
.flex {
display: flex;
justify-content: center;
}
.box {
width: 50%;
background-color: red;
border: 2px solid black;
min-width: 200px;
flex-direction: column;
margin: 10px;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.content {
position: relative;
display: flex;
justify-content: center;
align-items: center;
background-color: white;
width: 0%;
height: 90%;
transition: all .5s;
}
.text {
position: relative;
text-align: center;
overflow: hidden;
}
.box:hover>.content {
width: 90%;
}
@media(max-width: 450px) {
.flex {
flex-wrap: wrap;
}
.box {
flex-grow: 1;
}
}
<div class=flex>
<div class=box>
<div class=content>
<div class=text>
<h3>This</h3>
<p>Works</p>
</div>
</div>
</div>
<div class=box>
<div class=content>
<div class=text>
<h3>This doesnt</h3>
<p>TestTe stTestTestTest TestTestTestT estTestT estTes tTestTe stTestTestTest</p>
</div>
</div>
</div>
</div>
我不认为我很擅长解释这一点,但希望有人明白这一点。
解决方案
您可以有不同的想法,而不是为宽度设置动画,而是在其上方设置一个叠加层以模拟相同的效果:
.flex {
display: flex;
justify-content: center;
}
.box {
width: 50%;
background-color: red;
border: 2px solid black;
min-width: 200px;
flex-direction: column;
margin: 10px;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.content {
position: relative;
display: flex;
flex-direction:column;
justify-content: center;
align-items: center;
text-align: center;
background-color: white;
width: 90%;
height: 90%;
}
.content:before {
content:"";
position:absolute;
top:0;
left:50%;
right:0;
bottom:0;
background:red;
z-index:1;
transition: all .5s;
}
.content:after {
content:"";
position:absolute;
top:0;
right:50%;
left:0;
bottom:0;
background:red;
z-index:1;
transition: all .5s;
}
.box:hover>.content:before {
left: 100%;
}
.box:hover>.content:after {
right: 100%;
}
@media(max-width: 450px) {
.flex {
flex-wrap: wrap;
}
.box {
flex-grow: 1;
}
}
<div class="flex">
<div class="box">
<div class="content">
<h3>This</h3>
<p>Works</p>
</div>
</div>
<div class="box">
<div class="content">
<h3>This also works!</h3>
<p>TestTe stTestTestTest TestTestTestT estTestT estTes tTestTe stTestTestTest</p>
</div>
</div>
</div>
推荐阅读
- python - 如何在函数内将参数声明为 datetime64?
- android - 如何在 Android 上旋转 SurfaceView?
- git - 使用 Git 中给定功能的文件夹结构打包修改/新文件
- html - 试图仅捕获图像名称而不是完整的图像路径
- tensorflow - 调用 dispose 后未知的遗留张量
- c++ - C ++ 17将列表(或其他容器)转换为字符串跨平台的最快方法是什么
- python - 在一个脚本中更新列表并从另一个脚本访问更新的列表
- ios - 附加到 ARAnchor 的 SpriteKit SKLabelNode 不出现或全屏显示
- ssh - 如何使用 SSH 使 samba 服务器在 Internet 上可用
- javascript - 将 React 环境变量插入 HTML 脚本标签