因此,我决定构建一个导航栏,但其中包含一个下拉菜单,因此我这样做了,我不知道如何使箭头保持不断旋转,直到下拉菜单消失为止。请帮忙。

我已经尝试了javascript和其他CSS技巧,但没有想到。如果我最终需要使用javascript来解决问题,那我只是希望它能起作用:)

我希望箭头不断旋转,直到下拉菜单内容消失为止。



* {
  margin: 0;
  padding: 0;
  font-family: Arial, Helvetica, sans-serif;
  outline: 0;
}


/* ===== HEADER ===== */

header {
  padding: 20px 30px 40px 0;
  box-shadow: 0 1px 10px rgba(0, 0, 0, .11);
}

header nav {
  float: right;
}

header li {
  margin: 0 .5em;
  display: inline-block;
}

header a {
  font-size: 20px;
  text-transform: capitalize;
  text-decoration: none;
  color: #666666;
  transition: .2s;
}

header a:hover {
  color: black;
}

header .dropdown-link {
  padding-right: 5px;
}

header i {
  cursor: pointer;
  transition: .35s;
}

header .dropdown-link:hover+i,
header i:hover {
  transform: rotate(180deg);
}

header i:hover+.dropdown-content {
  display: grid;
}

.dropdown-link {
  padding: 20px 0;
}


/* ===== DROPDOWN-CONTENT ===== */

.dropdown-content {
  display: none;
  position: absolute;
  margin: 10px 0 0 0;
  padding: 10px 20px;
  background-color: white;
  /* 0 x and y offset, 10px blur and 5px size */
  box-shadow: 0 0 10px 5px rgba(0, 0, 0, .11);
  border-radius: 10px;
}

.dropdown-content:hover {
  display: grid;
}

.dropdown-link:hover~.dropdown-content {
  display: grid;
}

.dropdown-content a {
  padding: 5px 0;
}

<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />


<header>
  <nav>
    <ul>
      <li><a href="#">home</a></li>
      <li><a href="#">features</a></li>
      <li class="dropdown-item">
        <a class="dropdown-link" href="#">dropdown</a>
        <i class="fa fa-chevron-down"></i>
        <div class="dropdown-content">
          <a href="#">link 1</a>
          <a href="#">link 2</a>
          <a href="#">link 3</a>
          <a href="#">link 4</a>
          <a href="#">link 5</a>
        </div>
      </li>
      <li><a href="#">contact</a></li>
      <li><a href="#">about</a></li>
    </ul>
  </nav>
</header>

最佳答案

问题是,当您将鼠标悬停在<a>标记(dropdown-link)上时,图标会更改。您需要将鼠标悬停在lidropdown-item)元素上,因此当显示UL时,从技术上讲您仍将鼠标悬停在li上。

是重要的代码(也是唯一更新的代码):

header .dropdown-item:hover > i,
header i:hover {
  transform: rotate(180deg);
}




* {
  margin: 0;
  padding: 0;
  font-family: Arial, Helvetica, sans-serif;
  outline: 0;
}


/* ===== HEADER ===== */

header {
  padding: 20px 30px 40px 0;
  box-shadow: 0 1px 10px rgba(0, 0, 0, .11);
}

header nav {
  float: right;
}

header li {
  margin: 0 .5em;
  display: inline-block;
}

header a {
  font-size: 20px;
  text-transform: capitalize;
  text-decoration: none;
  color: #666666;
  transition: .2s;
}

header a:hover {
  color: black;
}

header .dropdown-link {
  padding-right: 5px;
}

header i {
  cursor: pointer;
  transition: .35s;
}

header .dropdown-item:hover>i,
header i:hover {
  transform: rotate(180deg);
}

header i:hover+.dropdown-content {
  display: grid;
}

.dropdown-link {
  padding: 20px 0;
}


/* ===== DROPDOWN-CONTENT ===== */

.dropdown-content {
  display: none;
  position: absolute;
  margin: 10px 0 0 0;
  padding: 10px 20px;
  background-color: white;
  /* 0 x and y offset, 10px blur and 5px size */
  box-shadow: 0 0 10px 5px rgba(0, 0, 0, .11);
  border-radius: 10px;
}

.dropdown-content:hover {
  display: grid;
}

.dropdown-link:hover~.dropdown-content {
  display: grid;
}

.dropdown-content a {
  padding: 5px 0;
}

<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />


<header>
  <nav>
    <ul>
      <li><a href="#">home</a></li>
      <li><a href="#">features</a></li>
      <li class="dropdown-item">
        <a class="dropdown-link" href="#">dropdown</a>
        <i class="fa fa-chevron-down"></i>
        <div class="dropdown-content">
          <a href="#">link 1</a>
          <a href="#">link 2</a>
          <a href="#">link 3</a>
          <a href="#">link 4</a>
          <a href="#">link 5</a>
        </div>
      </li>
      <li><a href="#">contact</a></li>
      <li><a href="#">about</a></li>
    </ul>
  </nav>
</header>

07-25 23:11