【问题标题】:How can I trigger multiple hover actions on links when hovering over a div?将鼠标悬停在 div 上时,如何触发链接上的多个悬停动作?
【发布时间】:2019-06-01 13:35:22
【问题描述】:

我试图在我的主 div 上悬停时触发多个悬停效果。我无法弄清楚如何使线条同时在所有三个链接上制作动画,以及这是否可以仅在 css 中完成,或者我是否必须使用 javascript,并且我找不到以前帖子的正确解决方案。代码如下:

.right-project-headline h2 a {
    text-decoration: none;
    color: black;
    position: relative;
}
.right-project-headline h2 a::before, .right-project-headline h2 a::after {
  content: '';
  background: black;
  position: absolute;
  top: 55%;
  height: 3px;
  width: 0;
}
.right-project-headline h2 a::before {
  left: 0;
}
.right-project-headline h2 a::after {
  right: 0;
  transition: width 500ms ease;
}
.right-project-headline h2 a:hover::before {
  width: 100%;
  transition: width 500ms ease;
}
.right-project-headline h2 a:hover::after {
  width: 100%;
  background: transparent;
  transition: 0;
}
<div class="right-project-headline">
  <h2><a href="industrial-rev.html">The</a></h2>
  <h2><a href="industrial-rev.html">Industrial</a></h2>
  <h2><a href="industrial-rev.html">Revolutions</a></h2>
</div>

【问题讨论】:

    标签: html css hover


    【解决方案1】:

    我会像下面这样简化您的代码并在父容器上应用悬停效果

    .right-project-headline h2 a {
      text-decoration: none;
      color: black;
      background: linear-gradient(black, black) no-repeat;
      background-size: 0% 4px; /*left:0 top:55%*/
      background-position: 0% 55%; /*left:0 top:55%*/
      transition: background-size 0.5s, background-position 0s 0.5s;
    }
    
    .right-project-headline:hover h2 a {
      background-size: 100% 4px; /*width:100% height:4px*/
      background-position: 100% 55%; /*right:0 top:55%*/
    }
    <div class="right-project-headline">
      <h2><a href="industrial-rev.html">The</a></h2>
      <h2><a href="industrial-rev.html">Industrial</a></h2>
      <h2><a href="industrial-rev.html">Revolutions</a></h2>
    </div>

    这是背景位置的另一种语法(更直观):

    .right-project-headline h2 a {
      text-decoration: none;
      color: black;
      background: linear-gradient(black, black) no-repeat;
      background-size: 0% 4px; 
      background-position: left 0 top 55%; 
      transition: background-size 0.5s, background-position 0s 0.5s;
    }
    
    .right-project-headline:hover h2 a {
      background-size: 100% 4px; 
      background-position: right 0 top 55%; 
    }
    <div class="right-project-headline">
      <h2><a href="industrial-rev.html">The</a></h2>
      <h2><a href="industrial-rev.html">Industrial</a></h2>
      <h2><a href="industrial-rev.html">Revolutions</a></h2>
    </div>

    另一个有简写版本和更少代码的:

    .right-project-headline h2 a {
      text-decoration: none;
      color: black;
      background: linear-gradient(black, black) var(--p,0%) 55%/var(--p,0%) 4px no-repeat;
      transition: background-size 0.5s, background-position 0s 0.5s;
    }
    
    .right-project-headline:hover h2 a {
      --p:100%;
    }
    <div class="right-project-headline">
      <h2><a href="industrial-rev.html">The</a></h2>
      <h2><a href="industrial-rev.html">Industrial</a></h2>
      <h2><a href="industrial-rev.html">Revolutions</a></h2>
    </div>

    【讨论】:

    • 好,你的努力值得+1!
    • 喜欢使用更少的代码,但是如果您没有将鼠标悬停在链接上足以完成动画,那么这个版本会有点错误,然后它会弹回左侧而不是向右。不过还是谢谢 :)
    • @KongGeorg 稍微提高速度以确保它会完成;)
    【解决方案2】:

    您必须在 CSS 中将 :hover 添加到您的 div,而不是添加到您的 a 元素中。

    这是 div 中的选择器

    div a
    

    这是一个悬停在 div 中的选择器

    div a:hover
    

    这是一个悬停div的选择器

     div:hover a
    

    所以这应该可以解决问题

    .right-project-headline h2 a {
        text-decoration: none;
        color: black;
        position: relative;
    }
    .right-project-headline h2 a::before, .right-project-headline h2 a::after {
      content: '';
      background: black;
      position: absolute;
      top: 55%;
      height: 3px;
      width: 0;
    }
    .right-project-headline h2 a::before {
      left: 0;
    }
    .right-project-headline h2 a::after {
      right: 0;
      transition: width 500ms ease;
    }
    .right-project-headline:hover h2 a::before {
      width: 100%;
      transition: width 500ms ease;
    }
    .right-project-headline:hover h2 a::after {
      width: 100%;
      background: transparent;
      transition: 0;
    }
    

    【讨论】:

      【解决方案3】:

      您需要在 .right-project-headline 类的元素上捕获悬停事件,然后将样式应用于其中的 &lt;a&gt; 元素,如下所示:

      .right-project-headline:hover a::before {
        width: 100%;
        transition: width 500ms ease;
      }
      
      .right-project-headline:hover a::after {
        width: 100%;
        background: transparent;
        transition: 0;
      }
      

      完整示例:

      .right-project-headline {
         border: 1px solid red;
      }
      
      .right-project-headline a {
          text-decoration: none;
          color: black;
          position: relative;
      }
      
      .right-project-headline a::before, .right-project-headline a::after {
        content: '';
        background: black;
        position: absolute;
        top: 55%;
        height: 3px;
        width: 0;
      }
      
      .right-project-headline a::before {
        left: 0;
      }
      
      .right-project-headline a::after {
        right: 0;
        transition: width 500ms ease;
      }
      
      .right-project-headline:hover a::before {
        width: 100%;
        transition: width 500ms ease;
      }
      
      .right-project-headline:hover a::after {
        width: 100%;
        background: transparent;
        transition: 0;
      }
      <div class="right-project-headline">
        <h2><a href="industrial-rev.html">The</a></h2>
        <h2><a href="industrial-rev.html">Industrial</a></h2>
        <h2><a href="industrial-rev.html">Revolutions</a></h2>
      </div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-28
        • 2017-03-06
        • 1970-01-01
        • 2010-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多