【问题标题】:How to show full text on hover but after some delay using CSS?如何在悬停时显示全文,但在使用 CSS 延迟一段时间后?
【发布时间】:2021-01-26 19:37:15
【问题描述】:

我想在光标悬停时显示全文,但仅在延迟 1 秒后显示。我尝试添加 transition 属性,但它似乎不适用于 absolute 元素。这是codepen demo。 代码如下:

.main {
  display: flex;
  justify-content: center;
}

.main .parent:hover .cell {
  position: absolute !important;
  z-index: 1 !important;
  padding-right: 10px;
  background: gray;
  transition-delay: 1s;
}

.main .parent {
  display: block;
  width: 200px;
  border-right: 1px solid rgb(237, 237, 236);
  white-space: nowrap;
  min-height: 32px;
  cursor: default;
  height: 32px;
  padding: 5px 8px 6px;
  font-size: 14px;
  overflow: hidden;
}

.main .parent .cell {
  line-height: 1.5;
  white-space: nowrap;
  word-break: normal;
  pointer-events: none;
}
<main class="main">
  <div class="parent">
    <span class="cell">
      In publishing and graphic design, Lorem ipsum is a placeholder text
      commonly used to demonstrate the visual form of a document or a typeface
      without relying on meaningful content.</span
    >
  </div>
</main>

请注意,背景颜色正在正确过渡,但当我悬停时会立即显示全文。

正在寻找一种解决方案,在不使用任何 JavaScript 的情况下增加显示全文的延迟。

【问题讨论】:

  • 这是您要找的东西吗? stackoverflow.com/questions/30089444/…
  • 不,这个问题是关于改变颜色的,我问的是在悬停时显示全文时添加延迟。
  • 您使用哪个属性来显示文本? z-index,溢出?
  • "那个问题是关于改变颜色的";所以...用你想要转换的属性交换颜色。您没有指定任何要转换的属性(transition-property);这就是它当前没有转换的原因(您仅指定了延迟)。
  • 看起来您正在使用 overflow 属性来显示文本 - 这是分配给容器的属性,而不是文本。您是否尝试过将鼠标悬停在容器上?

标签: html css sass


【解决方案1】:

最后通过使用 css animation 使其工作。工作codepan demo

@keyframes hover-delay {
  0%   {
    background: gray;
    position:absolute;
  }
  100% {
    background: gray;
  position: absolute ;}
}

 .parent {
    &:hover {
      .cell {
        z-index: 1 !important;
        padding-right: 10px;
        animation-name : hover-delay;
        animation-delay: 1s;
        animation-duration: 2s;
        // animation-iteration-count: infinite; // play animation infinite times
        
        // alternate way: retain the style values that is set by the last keyframe
         animation-fill-mode: forwards;
      }
    }
    display: block;
    width: 200px;
    border-right: 1px solid rgb(237, 237, 236);
    white-space: nowrap;
    min-height: 32px;
    cursor: default;
    height: 32px;
    padding: 5px 8px 6px;
    font-size: 14px;
    overflow: hidden;

    .cell {
      line-height: 1.5;
      white-space: nowrap;
      word-break: normal;
      pointer-events: none;
    }
  }

【讨论】:

    【解决方案2】:

    对我有用,即使是 position:absolute;

    但我继续添加了一个使用动画的版本(也是过渡)

    https://jsfiddle.net/56sxzcu7/

    /* Using transition */
    
    h1 {
      transition:1s;
      transition-delay:1s;
      opacity:1;
      position:absolute;
    }
    
    h1:hover {
      opacity:0.5;
    }
    
    /* Using animate */
    
    h2:hover {
      animation: example 2s linear 2s infinite alternate;
    }
    
    @keyframes example {
      0% {opacity:1;}
      100% {opacity:0.5;}
    }
    

    【讨论】:

    • 这对我来说绝对有用 - 很高兴你让它工作了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 2020-10-02
    • 1970-01-01
    相关资源
    最近更新 更多