LoginSignup
1
0

More than 3 years have passed since last update.

ハンバーガーメニューを開いても背景が透けない!?

Last updated at Posted at 2020-11-19

ハンバーガーメニューを開いた際に、なぜか背景が透けなかったので原因を分析してみました。

元のコード

index.html
<span class="nav-menu">
        <i class="nav-menu-i"></i>
        <i class="nav-menu-i"></i>
        <i class="nav-menu-i"></i>
      </span>
      <nav class="nav">
        <ul class="header-nav">
          <li class="header-nav-list"><a href="#"></a></li>
          <li class="header-nav-list"><a href=""></a></li>
          <li class="header-nav-list"><a href=""></a></li>
          <li class="header-nav-list"><a href=""></a></li>
          <li class="header-nav-list"><a href=""></a></li>
          <li class="header-nav-list"><a href=""></a></li>
        </ul>
      </nav>
stylesheet.scss
.nav {
  position: relative;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  visibility: hidden;
  transition: opacity .5s, visibility .5s
}

.nav.show {
  opacity: 0.9;
  visibility: visible;
  background-color: white;
}

.nav-menu {
  display: block;
  position: relative;
  position: absolute;
  position: fixed;
  width: 1.75rem;
  height: 1.5rem;
  top: 10px;
  right: 10px;
  cursor: pointer;
  z-index: 100;
  .nav-menu-i {
  display: block;
  width: 100%;
  height: 2px;
  background-color: black;
  position: absolute;
  transition: transform .5s, opacity .5s;
  }
  .nav-menu-i:nth-child(1) {
  top: 0;
  }
.nav-menu-i:nth-child(2) {
  top: 0;
  bottom: 0;
  margin: auto;
  }
.nav-menu-i:nth-child(3) {
  bottom: 0;
  }
}

Sassの書き方が汚いのはご愛嬌で…

nav-menu-iはハンバーガーの線を表しています

navを開いても、背景が透けてくれず、困ってたのですが、、

stylesheet.scss
.nav {
  position: relative;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  visibility: hidden;
  transition: opacity .5s, visibility .5s;
  z-index: 99;
}

z-indexを指定してあげると解決しました!
positionを使っているので、重なり順を指定してあげると解決するみたいですね🤔

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0