【问题标题】:Text slide in on hover CSS悬停 CSS 上的文本滑入
【发布时间】:2020-09-16 07:04:15
【问题描述】:

当鼠标悬停在the wolf 上时,我希望文本Along came 滑入。 但是the wolf 的位置不应该改变。 Along came 应该简单地从左侧滑入并淡入以完成句子。

.main {
  width: 100vw;
  text-align:center;
}

.addText {
  display: none;
  color: rgba(255,255,255,0);
  transition: .5s;
}

.link:hover .addText {
  display: inline;
  color: red;
  transform: translate(-10px, 00%);
}
<div class="main">
  <div class="link">
      <span class="addText">Along came </span>
      the wolf
  </div>
</div>

【问题讨论】:

    标签: css hover slide


    【解决方案1】:

    这是一个想法,您可以在其中制作元素width:0,这样它就不会影响其他元素:

    .main {
      text-align:center;
    }
    .link {
      display:inline-block; 
    }
    .addText {
      display:inline-block; /* inline-block so we can set a width */
      width:0;
      white-space:nowrap; /* keep text one line */
      direction:rtl; /* change direction so the text overflow on the left */
      color: rgba(255,255,255,0);
      transition: .5s;
      transform: translateX(20px); /* put the value you want here */
      pointer-events:none; /* to avoid the hover on the text, remove to see the difference */
    }
    
    .link:hover .addText {
      color: red;
      transform: translateX(0);
    }
    <div class="main">
      <div class="link">
          <span class="addText">Along came </span>
          the wolf
      </div>
    </div>

    如果您希望文本占用空间,您也可以只进行翻译:

    .main {
      text-align:center;
    }
    .link {
      display:inline-block; 
    }
    .addText {
      display:inline-block; 
      color: rgba(255,255,255,0);
      transition: .5s;
      transform: translateX(100%);
    }
    
    .link:hover .addText {
      color: red;
      transform: translateX(0);
    }
    <div class="main">
      <div class="link">
          <span class="addText">Along came </span>
          the wolf
      </div>
    </div>

    【讨论】:

    • 也许让&lt;div class="link"&gt; 内联,这样整行对悬停不敏感?
    • 谢谢@TemaniAfif。我们如何解决这个问题,以便鼠标悬停仅在鼠标悬停the wolf 时才有效。目前,当鼠标悬停在空白区域时,它也会滑动它。
    • @amars 我通过将 inline-block 添加到 .links 来修复它
    • @TemaniAfif 嘿,是的。看到了那个和新的更新。但是在上面的sn-p中它仍然不起作用。当鼠标位于Along came 会来并停止的空白区域时,它仍然会飞入。我以为可能是这个网站,但在小提琴jsfiddle.net/aLdo4h16
    • @amars 再次检查第一个 sn-p,进行了另一个更新
    猜你喜欢
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 2011-08-21
    • 2015-01-09
    • 2016-12-30
    相关资源
    最近更新 更多