html - 悬停时 anchor 动画的CSS问题

标签 html css

我遇到了一个问题,如果 anchor 链接分成多行,我的 CSS 动画将无法正常工作。

我创建了一个片段来展示我的问题。

body {
  background-color: #a3d5d3;
}

.max-width {
  width: 500px;
}

p {
  line-height: 24px;
}

a {
  position: relative;
  color: blue;
  text-decoration: none;
}

a:hover {
  color: red;
}

a:before {
  content: "";
  position: absolute;
  width: 100%;
  height: 2px;
  bottom: -3px;
  left: 0;
  background-color: #000;
  visibility: hidden;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transition: all 0.3s ease-in-out 0s;
  transition: all 0.3s ease-in-out 0s;
}
a:hover:before {
  visibility: visible;
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
}
<h1>Hi,</h1>
<div class="max-width">
  <h3>The animation works when the anchor link is just on one line, but it's not working when it splits onto multiple lines. See both links in the paragraph below.</h3>
  <p>
    In <a href="https://twitter.com/jane_r?lang=en" target="_blank" rel="noopener">her own words</a>, she is a “digital marketing executive, bourbon lover, spinner, runner and reluctant hockey mom.” And she “<a href="https://www.linkedin.com/in/janericciardelli/"
      target="_blank" rel="noopener">possesses an entrepreneurial bent</a>, having co-founded Medium One, a leading digital agency, which was acquired by Mosaic Group in 2000”.</p>
</div>

我尝试调整线条的宽度和位置,但无法正常工作。

有什么建议吗?

(代码笔 https://codepen.io/jetweevers/pen/EreQaL)

最佳答案

这是更新后的 fiddle :

body {
  background-color: #a3d5d3;
}

.max-width {
  width: 500px;
}

p {
  line-height: 24px;
}

a {
  text-decoration: none;
  background-image: linear-gradient(black, black);
  background-position:bottom center;
  background-repeat: no-repeat;
  background-size: 0% 2px;
  transition: background-size .3s;
}

a:hover,
a:focus {
  background-size: 100% 2px;
}
<h1>Hi,</h1>
<div class="max-width">
  <h3>The animation works when the anchor link is just on one line, but it's not working when it splits onto multiple lines. See both links in the paragraph below.</h3>
  <p>
    In <a href="https://twitter.com/jane_r?lang=en" target="_blank" rel="noopener">her own words</a>, she is a “digital marketing executive, bourbon lover, spinner, runner and reluctant hockey mom.” And she “<a href="https://www.linkedin.com/in/janericciardelli/"
      target="_blank" rel="noopener">possesses an entrepreneurial bent</a>, having co-founded Medium One, a leading digital agency, which was acquired by Mosaic Group in 2000”.</p>
</div>

关于html - 悬停时 anchor 动画的CSS问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54689191/

相关文章:

html - 两种背景颜色(水平)

javascript - <div> Bootstrap 3 的动态高度

html - 如何使用 Bootstrap 轮播显示上一张和下一张图片

javascript - 查看不应该更新的时候

Javascript:构造函数中的随机 Sprite 选择

javascript - jQuery href 导航

html - 无法在div上设置宽度和高度

html - Safari 和 :after , 问题:之前 { box-sizing:border-box; }

html - 栏不粘在图片 css/html 上

css - 无法获取文本框和指向 div 底部的链接