Noise of Web Programming

主にHTML/CSS/JavaScriptやWebサービスの情報を配信しています。可能な限り実装方法まで書いていくつもりです。

ブログを移転しました。

Tecuration.com

このサイト同様、HTML/CSS/JavaScriptなどのフロントサイドWeb開発に関する最新情報やライフハックを配信しています。

「くるっと回る」メニューバーをHTML/CSSで実装する方法

最近こんな感じで「くるっと回る」かっこいいメニューバーってよく見る。

Check out this Pen!


以下のサイトで実装方法が紹介されていたので試してみた。

Animated 3D Flipping Menu with CSS


HTMLとCSSのみで実装することが出来る。上記サイトに記載されているコードには誤りがあって動かないので、ここに動くコードを記載しておきますよっと。

<ul class="block-menu">
  <li><a href="#" class="three-d">
    Home
    <span aria-hidden="true" class="three-d-box">
      <span class="front">Home</span>
      <span class="back">Home</span>
    </span>
  </a></li>
  <li><a href="#" class="three-d">
    About
    <span aria-hidden="true" class="three-d-box">
      <span class="front">About</span>
      <span class="back">About</span>
    </span>
  </a></li>
  <!-- more items here -->
</ul>


ちなみにCSS適用前のHTMLのみだとこんな感じ。


これに以下のCSSを適用する。

.block-menu {
  display: block;
  background: #000;
}

.block-menu li {
  display: inline-block;
}

.block-menu li a {
  color: #fff;
  display: block;
  text-decoration: none;
  font-family: 'Passion One',Arial,sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-font-smoothing: antialiased;
  font-smoothing: antialiased;
  text-transform: uppercase;
  overflow: visible;
  line-height: 20px;
  font-size: 24px;
  padding: 15px 10px;
}

/* animation domination */
.three-d {
  -webkit-perspective: 200px;
  -moz-perspective: 200px;
  perspective: 200px;
  -webkit-transition: all .07s linear;
  -moz-transition: all .07s linear;
  transition: all .07s linear;
  position: relative;
}

  .three-d:not(.active):hover {
    cursor: pointer;
  }

  .three-d:not(.active):hover .three-d-box, 
  .three-d:not(.active):focus .three-d-box {
    -moz-transform: translateZ(-25px) rotateX(90deg);
    -webkit-transform: translateZ(-25px) rotateX(90deg);
    -o-transform: translateZ(-25px) rotateX(90deg);
    transform: translateZ(-25px) rotateX(90deg);
  }

.three-d-box {
  -webkit-transition: all .3s ease-out;
  -moz-transition: all .3s ease-out;
  -ms-transition: all .3s ease-out;
  -o-transition: all .3s ease-out;
  transition: all .3s ease-out;
  -webkit-transform: translatez(-25px);
  -moz-transform: translatez(-25px);
  -o-transform: translatez(-25px);
  transform: translatez(-25px);
  -webkit-transform-style: preserve-3d;
  -moz-transform-style: preserve-3d;
  -ms-transform-style: preserve-3d;
  -o-transform-style: preserve-3d;
  transform-style: preserve-3d;
  pointer-events: none;
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  width: 100%;
  height: 100%;
}

.front {
  -webkit-transform: rotatex(0deg) translatez(25px);
  -moz-transform: rotatex(0deg) translatez(25px);
  -o-transform: rotatex(0deg) translatez(25px);
  transform: rotatex(0deg) translatez(25px);
}

.back {
  -webkit-transform: rotatex(-90deg) translatez(25px);
  -moz-transform: rotatex(-90deg) translatez(25px);
  -o-transform: rotatex(-90deg) translatez(25px);
  transform: rotatex(-90deg) translatez(25px);
  color: #FFE7C4;
}

.front, .back {
  display: block;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  background: black;
  padding: 15px 10px;
  color: white;
  pointer-events: none;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}


すると冒頭に載せたDemoのようにくるっと回るメニューバーの完成!いつか使ってみよう。