html - 如何使用:hover on two of the same elements

标签 html css

我正在试验 CSS 动画,我制作了一个导航栏,其中的标签具有轮廓,并且波浪效果是通过使用剪辑路径多边形为另一个标签制作动画来制作的。我目前正试图做到这一点,在悬停时,内部元素的高度增加以完全填充轮廓。但我不知道如何将 :hover 与同一标签的两个元素一起使用。我试过 .nthChild 但它似乎没有用。不过,这可能是我的过渡。另外,全屏运行代码段,因为我没有让它响应。谢谢:)

* {
  margin: 0;
  padding: 0;
  pointer-events: all;
}

nav {
  align-items: center;
  background-color: black;
  display: flex;
  justify-content: space-around;
  min-height: 8vh;
}

.logo{
  position: relative;
}

.logo h2 {
  font-family: 'Poppins', sans-serif;
  color: #fff;
  font-size: 32px;
  letter-spacing: 5px;
  position: absolute;
  text-transform: uppercase;
  transform: translate(-50%, -50%);
}
.logo h2:nth-child(1)
{
  color: transparent;
  -webkit-text-stroke: 1px #03a9f4;
}
.logo h2:nth-child(2)
{
  animation: animate 3s ease-in-out infinite;
  color: #03a9f4;
}

@keyframes animate
{
    0%,100%
  {
    clip-path: polygon(0% 45%, 15% 44%, 32% 50%, 54% 60%, 70% 61%, 84% 59%, 100% 52%, 100% 100%, 0% 100%);
  }
  50%
  {
    clip-path: polygon(0% 60%, 16% 65%, 34% 66%, 51% 62%, 67% 50%, 84% 45%, 100% 46%, 100% 100%, 0% 100%);
  }
}

.nav-links{
  display: flex;
  justify-content: space-around;
  position: relative;
  width: 50%
}

.nav-links li{
  list-style: none;
}

.nav-links a {
  color: rgb(238, 238, 238);
  font-family: 'Poppins', sans-serif;
  font-size: 30px;
  font-weight: bold;
  letter-spacing: 3px;
  position: absolute;
  text-decoration: none;
  text-transform: uppercase;
  transform: translate(-50%,-50%);
  transition: height 4s;
}

.nav-links a:nth-child(1) {
  color: transparent;
  -webkit-background-clip: text;
  -webkit-text-stroke: 1px #03a9f4;
  overflow: hidden;
}

.nav-links a:nth-child(2) {
  animation: animate 3s ease-in-out infinite;
  color: #03a9f4;
  overflow: hidden;

}

@keyframes animate
{
    0%,100%
  {
    clip-path: polygon(0% 45%, 15% 44%, 32% 50%, 54% 60%, 70% 61%, 84% 59%, 100% 52%, 100% 100%, 0% 100%);
  }
  50%
  {
    clip-path: polygon(0% 60%, 16% 65%, 34% 66%, 51% 62%, 67% 50%, 84% 45%, 100% 46%, 100% 100%, 0% 100%);
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <link rel="stylesheet" type="text/css" href="style.css">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Liquid</title>
</head>

<body>

  <nav class="nav">
    <div class="logo">
      <h2>Nav</h2>
      <h2>Nav</h2>
    </div>
    <ul class="nav-links">
      <li>
         <a id='outline' href="./index.html"><h3>Home</h3></a>
          <a id='wave' href="./index.html"><h3>Home</h3></a>
       </li>
       
      <li>
        <a id='outline' href="./contact.html"><h3>Contact</h3></a>
        <a id='wave'href="./contact.html"><h3>Contact</h3></a>
      </li>

      <li>
        <a id='outline' href="./canvas.html"><h3>Visualizer</h3></a>
        <a id='wave'href="./canvas.html"><h3>Visualizer</h3></a>
      </li>
    </ul>
  </nav>

  <script src="app.js" charset="utf-8"></script>

</body>

<main></main>

</html>

最佳答案

在这种情况下,您将需要两个选择器:

  • .nav-links a:nth-child(1):hover + a
  • .nav-links a:nth-child(2):hover

你可以用逗号将它们组合起来,像这样:

.nav-links a:nth-child(1):hover + a,
.nav-links a:nth-child(2):hover {
  animation: none;
}

工作示例:

* {
  margin: 0;
  padding: 0;
  pointer-events: all;
}

nav {
  align-items: center;
  background-color: black;
  display: flex;
  justify-content: space-around;
  min-height: 8vh;
}

.logo{
  position: relative;
}

.logo h2 {
  font-family: 'Poppins', sans-serif;
  color: #fff;
  font-size: 32px;
  letter-spacing: 5px;
  position: absolute;
  text-transform: uppercase;
  transform: translate(-50%, -50%);
}
.logo h2:nth-child(1)
{
  color: transparent;
  -webkit-text-stroke: 1px #03a9f4;
}
.logo h2:nth-child(2)
{
  animation: animate 3s ease-in-out infinite;
  color: #03a9f4;
}

.nav-links{
  display: flex;
  justify-content: space-around;
  position: relative;
  width: 50%
}

.nav-links li{
  list-style: none;
}

.nav-links a {
  color: rgb(238, 238, 238);
  font-family: 'Poppins', sans-serif;
  font-size: 30px;
  font-weight: bold;
  letter-spacing: 3px;
  position: absolute;
  text-decoration: none;
  text-transform: uppercase;
  transform: translate(-50%,-50%);
  transition: height 4s;
}

.nav-links a:nth-child(1) {
  color: transparent;
  -webkit-background-clip: text;
  -webkit-text-stroke: 1px #03a9f4;
  overflow: hidden;
}

.nav-links a:nth-child(2) {
  animation: animate 3s ease-in-out infinite;
  color: #03a9f4;
  overflow: hidden;

}

@keyframes animate
{
    0%,100%
  {
    clip-path: polygon(0% 45%, 15% 44%, 32% 50%, 54% 60%, 70% 61%, 84% 59%, 100% 52%, 100% 100%, 0% 100%);
  }
  50%
  {
    clip-path: polygon(0% 60%, 16% 65%, 34% 66%, 51% 62%, 67% 50%, 84% 45%, 100% 46%, 100% 100%, 0% 100%);
  }
}

.nav-links a:nth-child(1):hover + a,
.nav-links a:nth-child(2):hover {
  animation: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <link rel="stylesheet" type="text/css" href="style.css">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Liquid</title>
</head>

<body>

  <nav class="nav">
    <div class="logo">
      <h2>Nav</h2>
      <h2>Nav</h2>
    </div>
    <ul class="nav-links">
      <li>
         <a id='outline' href="./index.html"><h3>Home</h3></a>
          <a id='wave' href="./index.html"><h3>Home</h3></a>
       </li>
       
      <li>
        <a id='outline' href="./contact.html"><h3>Contact</h3></a>
        <a id='wave'href="./contact.html"><h3>Contact</h3></a>
      </li>

      <li>
        <a id='outline' href="./canvas.html"><h3>Visualizer</h3></a>
        <a id='wave'href="./canvas.html"><h3>Visualizer</h3></a>
      </li>
    </ul>
  </nav>

  <script src="app.js" charset="utf-8"></script>

</body>

<main></main>

</html>

关于html - 如何使用:hover on two of the same elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67042394/

相关文章:

javascript - 使用 JavaScript 控制 HTML 中的 mp3 播放

javascript - ThreeJS 使用 HTML Canvas 更新纹理在移动设备(iPhone Safari 等)上给出 "WebGL: INVALID_VALUE: texImage2D: no canvas"

javascript - "Scroll up button"不是在窗口上而是在某个 div 上可能吗?

jquery - 如何使用域 url 而不是本地路径调用 font-face

javascript - 如何使用 JavaScript 和 CSS 不显眼地设置表单元素的样式?

html - 如何使页脚对齐到最底部?

悬停时 HTML 表格高亮行,第一行(标题)除外

javascript - 突出显示图像 map 上的区域

html - 限制div的最小宽度

css - 如何在不同的 React 组件之间重用 styled-components?