nce*_*sar 0 html css css-transitions
我在一些文本中设置了white-space: nowrapwith text-overflow: ellipsis,当用户悬停时,它应该将空白值更改为正常。悬停代码工作正常,但我无法使转换工作。
这是 CodePen 链接: https: //codepen.io/anon/pen/qwYoyR
.App p {
margin-bottom: 3px;
}
.App .bg-row {
box-shadow: 0px 0px 5px #bdc3c7;
background-color: #ecf0f1a3;
padding: 20px;
border-radius: 5px;
}
.App .bg-row .repositorios {
transition: all 0.2s ease;
margin-bottom: 20px;
}
.App .bg-row .repositorios:hover .repo-content {
background-color: #d2d6d8;
transition: 500ms ease-in-out;
}
.App .bg-row .repositorios .repo-image {
width: 100%;
height: auto;
box-shadow: 0px 0px 5px #bdc3c7;
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
-o-transition: all 0.2s ease;
}
.App .bg-row .repositorios .repo-content {
padding: 10px;
transition: 500ms ease-in-out;
overflow: hidden;
box-shadow: 0px 0px 5px #bdc3c7;
transition: 500ms ease-in-out;
background-color: #ecf0f1;
}
.App .bg-row .repositorios .repo-content p {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.App .bg-row .repositorios .repo-content .read-more {
transition: 500ms ease-in-out;
cursor: pointer;
}
.App .bg-row .repositorios .repo-content .read-more:hover {
white-space: normal;
transition: 500ms ease-in-out;
}Run Code Online (Sandbox Code Playgroud)
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container App">
<div class="row bg-row">
<div class="col-sm-6 col-md-4 col-lg-3 repositorios">
<div class="repo-content">
<p class="read-more">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
</div>
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
这些不是可以转换的属性。
white-space: nowrap;
text-overflow: ellipsis;
Run Code Online (Sandbox Code Playgroud)
为了将一个属性转换为另一个属性,CSS 需要中间值。从 nowrap 到 normal 没有中间值,就像从 heright: 0 到 auto 或从 display: none 到 block 没有中间值一样。
使用 CSS 实现此目的的唯一方法是对元素使用 max-height,并在悬停时进行转换。然而,空白/省略号本身必须被删除 => 将它们替换为与父元素具有相同背景颜色的伪元素:
.x {
position: relative;
overflow: hidden;
max-height: 20px; /* set this to the height your first row would be */
background-color: red; /* you can change this, ofc */
transition: max-height .5s; /* the duration of the transition */
}
.x:after {
content: '...';
position: absolute;
top: 0; right: 0;
height: 1.5em; /* positioning it in line with the text - hacky */
padding: 0 .2em;
background-color: red; /* keep the same as parent - it cannot be transparent unfortunately */
opacity: 1;
transition: opacity 0s .5s; /* will delay the appearance of the '...' until the expanding transition has finished */
}
.x:hover {
max-height: 1000px; /* can be lower to create smoother response time */
}
.x:hover::after {
opacity: 0;
transition: opacity 0s 0s; /* will apply the appearance of the '...' when hovered */
}
Run Code Online (Sandbox Code Playgroud)
Hacky,可改进,但仅使用 CSS。