css 导航itens不显示当我悬停按钮

yhived7q  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(74)

我已经实现了一个CSS汉堡图标,我遇到了一个问题,当我悬停在按钮上时,导航项不显示。图标动画工作正常,但导航不可见。
我尝试使用以下代码解决此问题:

:is(header:hover, header:focus-within) nav {
    display: block;}

字符集
虽然上面的修改部分解决了这个问题,但它会导致导航在悬停在整个标题上时显示。我需要帮助,使导航只在悬停在按钮上时可见。我如何才能正确实现这一点?

@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --HEADER-BGCOLOR: #333;
  --HEADER-COLOR: whitesmoke;
}

/*FONT STYLES*/

html {
  font-size: 1.5rem;
  font-family: 'Roboto', sans-serif;
}

body {
  min-height: 100vh;
  display: flex;
  flex-flow: column nowrap;
}

header {
  background-color: var(--HEADER-BGCOLOR);
  color: var(--HEADER-COLOR);
}

.header-title-line {
  padding: 0.25rem 0.5rem;
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-between;
}

/*BUTTON RESET*/

.menu-button {
  background-color: transparent;
  border: none;
  width: 48px;
  height: 48px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

/*HAMBURGER ICON*/

.menu-icon,
.menu-icon::before,
.menu-icon::after {
  background-color: var(--HEADER-COLOR);
  width: 40px;
  height: 5px;
  border-radius: 5px;
  position: absolute;
  transition: all 0.5s;
}

.menu-icon::before,
.menu-icon::after {
  content: "";
}

.menu-icon::before {
  transform: translate(-20px, -12px);
}

.menu-icon::after {
  transform: translate(-20px, 12px);
}

/*HAMBURGER ICON*/

:is(.menu-button:hover,
.menu-button:focus-within) .menu-icon {
  background-color: transparent;
  transform: rotate(720deg);
}

:is(.menu-button:hover,
.menu-button:focus-within) .menu-icon::before {
  transform: translateX(-20px) rotate(45deg);
}

:is(.menu-button:hover,
.menu-button:focus-within) .menu-icon::after {
  transform: translateX(-20px) rotate(-45deg);
}

:is(.menu-button:hover,
.menu-button:focus-within) nav {
  display: block;
}

nav {
  display: none;
  background-color: var(--HEADER-BGCOLOR);
  transform-origin: top center;
  animation: showMenu 0.5s ease-in-out forwards;
}

@keyframes showMenu {
  0% {
    transform: scaleY(0);
  }
  80% {
    transform: scaleY(1.2);
  }
  100% {
    transform: scaleY(1);
  }
}

nav ul {
  list-style-type: none;
  display: flex;
  flex-flow: column nowrap;
}

nav li {
  padding: 0.5rem;
  border-top: 1px solid var(--HEADER-COLOR);
}

nav a {
  display: block;
  text-align: center;
  width: 80%;
  margin: auto;
}

nav a:any-link {
  color: var(--HEADER-COLOR);
  font-weight: bold;
  text-decoration: none;
}

nav a:hover,
nav a:focus {
  transform: scale(1.2);
  transition: all 0.3s;
}
<header>
  <section class="header-title-line">
    <h1>Preguiça Co.</h1>
    <button class="menu-button">
                <div class="menu-icon"></div>
            </button>
  </section>

  <nav>
    <ul>
      <li><a href="Home.html">Home</a></li>
      <li><a href="#">Item1</a></li>
      <li><a href="#">Item2</a></li>
      <li><a href="#">Item3</a></li>
    </ul>
  </nav>
</header>

的数据

sf6xfgos

sf6xfgos1#

你可以使用header:has(.menu-button:hover, .menu-button:focus-within) nav

@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');

/*RESET*/
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --HEADER-BGCOLOR: #333;
    --HEADER-COLOR: whitesmoke;
}

/*FONT STYLES*/
html {
    font-size: 1.5rem;
    font-family: 'Roboto', sans-serif;
}

body {
    min-height: 100vh;
    display: flex;
    flex-flow: column nowrap;
}

header {
    background-color: var(--HEADER-BGCOLOR);
    color: var(--HEADER-COLOR);
}

.header-title-line {
    padding: 0.25rem 0.5rem;
    display: flex;
    flex-flow: row nowrap;
    justify-content: space-between;
}

/*BUTTON RESET*/
.menu-button {
    background-color: transparent;
    border: none;
    width: 48px;
    height: 48px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
}

/*HAMBURGER ICON*/
.menu-icon,
.menu-icon::before,
.menu-icon::after {
    background-color: var(--HEADER-COLOR);
    width: 40px;
    height: 5px;
    border-radius: 5px;
    position: absolute;
    transition: all 0.5s;
}

.menu-icon::before,
.menu-icon::after {
    content: "";
}

.menu-icon::before {
    transform: translate(-20px, -12px);
}

.menu-icon::after {
    transform: translate(-20px, 12px);
}
/*HAMBURGER ICON*/

:is(.menu-button:hover, .menu-button:focus-within) .menu-icon {
    background-color: transparent;
    transform: rotate(720deg);
}

:is(.menu-button:hover, .menu-button:focus-within) .menu-icon::before {
    transform: translateX(-20px) rotate(45deg);
}

:is(.menu-button:hover, .menu-button:focus-within) .menu-icon::after {
    transform: translateX(-20px) rotate(-45deg);
}

header:has(.menu-button:hover, .menu-button:focus-within) nav {
    display: block;
}

nav {
    display: none;
    background-color: var(--HEADER-BGCOLOR);
    transform-origin: top center;
    animation: showMenu 0.5s ease-in-out forwards;
}

@keyframes showMenu {
    0% {
        transform: scaleY(0);
    }

    80% {
        transform: scaleY(1.2);
    }

    100% {
        transform: scaleY(1);
    }
}

nav ul {
    list-style-type: none;
    display: flex;
    flex-flow: column nowrap;
}

nav li{
    padding: 0.5rem;
    border-top: 1px solid var(--HEADER-COLOR);
}

nav a {
    display: block;
    text-align: center;
    width: 80%;
    margin: auto;
}

nav a:any-link {
    color: var(--HEADER-COLOR);
    font-weight: bold;
    text-decoration: none;
}

nav a:hover,
nav a:focus {
    transform: scale(1.2);
    transition: all 0.3s;
}

个字符

相关问题