【问题标题】:Hide a text when the container slides容器滑动时隐藏文本
【发布时间】:2021-10-22 22:42:04
【问题描述】:

为什么我的文字不在 “文本溢出:省略号”? 我希望我的文本直接在省略号中,并且在容器滑动时隐藏多余的文本。 您可以在附件中找到我的代码。 谢谢你的帮助。

http://jsfiddle.net/5gLtraxh/12/

.wrapper {
    position: relative;
    overflow: hidden;
    width: 400px;
    height: 100px; 
    border: 1px solid black;  
    }
.description{ overflow:hidden;
  text-overflow: ellipsis;
}
.description2{
  white-space: nowrap;
  overflow:hidden;
  text-overflow: ellipsis;
}
.price{
    position: absolute;
    right: 0;
    bottom: 0;
    width: 100px;
    height: 100%;
    background: green;
    transition: 1s;
}
.slide {
    position: absolute;
    right: -100px;
    bottom: 0;
    width: 100px;
    height: 100%;
    background: blue;
    transition: 1s;
}
.wrapper:hover .slide {
    transition: 1s;
    right: 0;
}
.wrapper:hover .price {
    transition: 1s;
    right: 100px;
}
<div class="wrapper">
   <h4 class="description">Lorem ipsum dolor sit amet, consectetur</h4>
   <p class="description2">
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Beatae nulla
   </p>
   <span class="price"></span>
   <div class="slide"></div>
</div>

【问题讨论】:

  • 你好 Jusai,你能否在你的帖子中包含你的 css 代码,以便我们更容易理解?

标签: overflow hidden ellipsis


【解决方案1】:

在这种情况下您不能使用position: absolute,因为它会使&lt;div&gt;不再占用任何空间。您可以使用float 定位它们,例如:

.wrapper {
    position: relative;
    overflow: hidden;
    width: 400px;
    height: 100px; 
    border: 1px solid black;  
}

.description
{
  white-space: nowrap; /*I am guessing you want this*/
  overflow:hidden;
  text-overflow: ellipsis;
}

.description2
{
  white-space: nowrap;
  overflow:hidden;
  text-overflow: ellipsis;
}

.price
{
    float: right;
    width: 100px;
    height: 100%;
    background: green;
    transition: 1s;
}

.slide {
    float: right;
    width: 0px;
    height: 100%;
    background: blue;
    transition: 1s;
}

.wrapper:hover .slide {
    transition: 1s;
    width: 100px;
}

.wrapper:hover .price {
    transition: 1s;
    right: 100px;
}
<div class="wrapper">
   <div class="slide"></div>
   <span class="price"></span>
   <h4 class="description">Lorem ipsum dolor sit amet, consectetur</h4>
   <p class="description2">
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Beatae nulla nihil fugit debitis assumenda reiciendis ab velit cupiditate blanditiis perspiciatis hic itaque reprehenderit, enim officia iusto, sint quod modi architecto!
   </p>
</div>

弹性盒解决方案:

.wrapper {
    display: flex;
    overflow: hidden;
    width: 400px;
    height: 100px; 
    border: 1px solid black;  
    justify-content: space-between;
}

.description-wrapper {
  min-width: 0;
}

.slider-wrapper {
  display: flex;
}

.description
{
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.description2
{
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.price
{
    flex-shrink: 0;
    width: 100px;
    height: 100%;
    background: green;
    transition: 1s;
}

.slide {
    flex-shrink: 0;
    width: 0px;
    height: 100%;
    background: blue;
    transition: 1s;
}

.wrapper:hover .slide {
    transition: 1s;
    width: 100px;
}

.wrapper:hover .price {
    transition: 1s;
}
<div class="wrapper">
   <div class="description-wrapper">
     <h4 class="description">Lorem ipsum</h4>
     <p class="description2">
    Lorem ipsum dolor sit amet</p>
   </div>
   <div class="slider-wrapper">
     <span class="price">sd</span>
     <div class="slide"></div>
   </div>
</div>

【讨论】:

  • 感谢这个解决方案,出于好奇,有没有其他方法可以在没有浮动的情况下获得类似的结果?
  • @Jysai 是的,除了position: absolute 之外的任何用于定位的东西,例如flexbox。
  • 我很想知道如何使用 flexbox 做到这一点?你能告诉我吗
  • @Jysai 我添加了一个 flexbox 解决方案。
  • @Jysai 很高兴它有帮助!如果这已经解决了你的问题,你可以考虑accepting it
【解决方案2】:

您好,当我折叠文本时,我注意到代码没有按预期工作。项目不再位于包装器的末尾。 正如您在下面的代码中看到的那样。

.wrapper {
    display: flex;
    overflow: hidden;
    width: 400px;
    height: 100px; 
    border: 1px solid black;  
}

.description-wrapper {
  min-width: 0;
}

.description
{
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.description2
{
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.price
{
    flex-shrink: 0;
    width: 100px;
    height: 100%;
    background: green;
    transition: 1s;
}

.slide {
    flex-shrink: 0;
    width: 0px;
    height: 100%;
    background: blue;
    transition: 1s;
}

.wrapper:hover .slide {
    transition: 1s;
    width: 100px;
}
<div class="wrapper">
   <div class="description-wrapper">
     <h4 class="description">Lorem ipsuctetur</h4>
     <p class="description2">
    Lorem ipsum dolor sit ame</p>
   </div>
   <span class="price">33$</span>
   <div class="slide"></div>
</div>

【讨论】:

  • 您好,您能解释一下您所做的更改以便我跟进吗?
  • 我只减少了

    &

    中的文字
  • 谢谢,我已经在我的 flexbox 解决方案中编辑了我的答案。应该已经修好了。
猜你喜欢
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
  • 2019-10-03
  • 2019-06-14
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
相关资源
最近更新 更多