ハンバーガーボタン

目次

作りたいもの:クリックすると×になるハンバーガーボタン(ボタンのみ)

See the Pen Untitled by Tomo56 (@tomokoro) on CodePen.

HTML

<div class="p-header__hamburger js-hamburger">
   <span></span>
   <span></span>
   <span></span>
</div>

CSS

.p-header__hamburger {
    width: 50px;
    height: 50px;
    position: relative;
    background-color: #000;
    cursor: pointer;
}

.p-header__hamburger span {
    display: inline-block;
    width: 30px;
    height: 2px;
    background-color: #fff;
    border-radius: 5px;
    transition: 0.3s ease;
}

.p-header__hamburger span:nth-child(1) {
    position: absolute;
    top: 15px;
    left:10px;
}

.p-header__hamburger span:nth-child(2) {
    position: absolute;
    top: 25px;
    left:10px;
}

.p-header__hamburger span:nth-child(3) {
    position: absolute;
    top: 35px;
    left:10px;
}

/* ハンバーガーメニューを押すと×になる動き*/
/*activeクラスを付ける*/
.p-header__hamburger.active span:nth-of-type(1) {
    transform: translateY(6px)      rotate(-45deg);
    transition: 0.3s;
    top:18px;
}

.p-header__hamburger.active span:nth-of-type(2) {
  opacity: 0;
  transition: 0.3s;
}

.p-header__hamburger.active span:nth-of-type(3){
    transform: translateY(-6px) rotate(45deg);
    transition: 0.3s;
    top:30px;
}

JavaScript(jQuery)

$(function () {
  $(".js-hamburger").click(function () {
    $(this).toggleClass("active");
  });
});

作りたいもの:ハンバーガーボタンと右端から出てくるドロワーメニュー(jQuery使用)

See the Pen Untitled by Tomo56 (@tomokoro) on CodePen.

HTML

<header class="p-header">
    <div class="p-header__inner">
      <h1 class="p-header__logo">
        <a href="index.html">MyCode</a>
      </h1>
      <div class="p-header__hamburger u-sp js-hamburger">
        <span class="c-hamburger"></span>
        <span class="c-hamburger"></span>
        <span class="c-hamburger"></span>
      </div>
      <nav class="p-header__nav p-sp-nav u-sp">
        <ul class="p-sp-nav__items">
          <li class="p-sp-nav__item"><a href="index.html">メニュー1</a></li>
          <li class="p-sp-nav__item"><a href="#">メニュー2</a></li>
          <li class="p-sp-nav__item"><a href="#">メニュー3</a></li>
        </ul>
      </nav>
      <nav class="p-header__nav p-pc-nav u-pc">
        <ul class="p-pc-nav__items">
          <li class="p-pc-nav__item"><a href="index.html">メニュー1</a></li>
          <li class="p-pc-nav__item"><a href="#">メニュー2</a></li>
          <li class="p-pc-nav__item"><a href="#">メニュー3</a></li>
        </ul>
      </nav>
    </div>
  </header>

CSS


.p-header {
  height: 60px;
  background-color: #fff;
}
@media screen and (min-width: 768px) {
  .p-header {
    height: 90px;
  }
}

.p-header__inner {
  height: inherit;
/*   padding: 20px ; */
  padding: 20px 0 20px 20px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-pack: justify;
      -ms-flex-pack: justify;
          justify-content: space-between;
}
@media screen and (min-width: 768px) {
  .p-header__inner {
  padding: 20px;
  }
}

.p-header__logo {
  width: 107px;
  font-size: 24px;
  font-weight: bold;
}

.p-header__hamburger {
  cursor: pointer;
  position: relative;
  background-color: #DD1B57;
  width: 60px;
  height: 60px;
  z-index: 100;
}

.p-header__hamburger.active .c-hamburger:nth-child(1) {
  top: 28px;
  -webkit-transform: rotate(35deg);
          transform: rotate(35deg);
}

.p-header__hamburger.active .c-hamburger:nth-child(2) {
  opacity: 0;
}

.p-header__hamburger.active .c-hamburger:nth-child(3) {
  top: 27px;
  -webkit-transform: rotate(-35deg);
          transform: rotate(-35deg);
}

/* p-sp-nav */
.p-sp-nav {
  position: fixed;
  z-index: 2;
  top: 0;
  left: 0;
  color: #fff;
  background: #DD1B57;
  -webkit-transform: translateX(200%);
          transform: translateX(200%);
  -webkit-transition: all 0.6s ease-in-out;
  transition: all 0.6s ease-in-out;
  width: 53%;
  height: 100%;
  overflow-y: scroll;
  padding-top: 96px;
  padding-top: 6rem;
}

.p-sp-nav__items {
  width: 53%;
}

.p-sp-nav__item {
  width: 53%;
  font-size: 16px;
  line-height: 3.625;
  letter-spacing: 0.05em;
  font-family: "Noto Sans JP", sans-serif;
  list-style: none;
}

.p-sp-nav__item a {
  display: inline-block;
  white-space: nowrap;
  color: #fff;
  padding: 14px 30px;
}

.p-sp-nav.active {
  -webkit-transform: translateX(100%);
          transform: translateX(100%);
}
/* p-pc-nav */

.p-pc-nav__items {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
}

.p-pc-nav__item {
  font-size: 15px;
  line-height: 1;
  letter-spacing: -0.005em;
  font-family: "Roboto", sans-serif;
  font-weight: 700;
  list-style: none;
}

.p-pc-nav__item:hover {
  color: #DD1B57;
  -webkit-transform: 0.3s;
          transform: 0.3s;
  cursor: pointer;
}

.p-pc-nav__item a {
  display: inline-block;
  padding: 34px 13px;
}

.p-pc-nav__item a.active {
  color: #DD1B57;
}

/* c-hamburger */
.c-hamburger {
  display: block;
  position: absolute;
  width: 26px;
  height: 2px;
  border-radius: 0.3125rem;
  background: #fff;
  -webkit-transition: all 0.4s ease-in-out;
  transition: all 0.4s ease-in-out;
  z-index: 100;
}

.c-hamburger:nth-child(1) {
  top: 20px;
  left: 16px;
}

.c-hamburger:nth-child(2) {
  top: 28px;
  left: 16px;
}

.c-hamburger:nth-child(3) {
  top: 36px;
  left: 16px;
}

.c-hamburger.active:nth-child(1) {
  top: 30px;
  -webkit-transform: rotate(45deg);
          transform: rotate(45deg);
}

.c-hamburger.active:nth-child(2) {
  opacity: 0;
}

.c-hamburger.active:nth-child(3) {
  top: 26px;
  -webkit-transform: rotate(-45deg);
          transform: rotate(-45deg);
}

/* sp/pc表示切り替え */
.u-sp {
  display: block;
}
@media screen and (min-width: 768px) {
  .u-sp {
    display: none;
  }
}

.u-pc {
  display: none;
}
@media screen and (min-width: 768px) {
  .u-pc {
    display: block;
  }
}

JavaScript(jQuery)

jQuery(function ($) { 
  $('.js-hamburger').on('click', function () {
     $(this).toggleClass('active');
     $('.p-sp-nav').toggleClass('active');
    });
    
    $('.p-sp-nav').on('click', function () {
    $(this).removeClass('active');
    $('.js-hamburger').removeClass('active');
    
    });
  });  

作りたいもの:ハンバーガーボタンと左端から出てくるドロワーメニュー

See the Pen Untitled by Tomo56 (@tomokoro) on CodePen.

HTML

<div class="l-header p-header">
    <div class="p-header__inner l-inner">
      <h1 class="p-header__logo">
        <a href="index.html">MyCode</a>
      </h1>
      <div class="p-header__nav  js-nav">
        <button class="p-header__btn">
          <span class="p-header__bar"></span>
          <span class="p-header__bar"></span>
          <span class="p-header__bar"></span>
        </button>
      </div>
    </div>
    <nav class="p-nav">
      <ul class="p-nav__items">
        <li class="p-nav__item"><a href="#pickup">メニュー1</a></li>
        <li class="p-nav__item"><a href="#feature">メニュー2</a></li>
        <li class="p-nav__item"><a href="#contact">メニュー3</a></li>
      </ul>
    </nav>
  </div>

CSS

.p-header {
  height: 80px;
  position: relative;
}

.p-header__inner {
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  height: inherit;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  padding: 0 20px;
}

.p-header__logo {
  font-size: 24px;
  font-weight: bold;
}

.p-header__btn {
  position: relative;
  height: 40px;
  width: 40px;
  z-index: 10;
}

.p-header__bar {
  position: absolute;
  background-color: #121212;
  content: "";
  display: block;
  height: 2px;
  width: 30px;
}

.p-header__bar:nth-child(1) {
  top: 5px;
  left: 3px;
}

.p-header__bar:nth-child(2) {
  top: 15px;
  left: 3px;
}

.p-header__bar:nth-child(3) {
  top: 25px;
  left: 3px;
}

.p-header__bar.active:nth-child(1) {
  background-color: rgba(0, 0, 0, .8);
  -webkit-transform: translateY(10px) rotate(-315deg);
  transform: translateY(10px) rotate(-315deg);
  -webkit-transition: 0.4s;
  transition: 0.4s;
}

.p-header__bar.active:nth-child(2) {
  opacity: 0;
}

.p-header__bar.active:nth-child(3) {
  background-color: rgba(0, 0, 0, .8);
  -webkit-transform: translateY(-10px) rotate(315deg);
  transform: translateY(-10px) rotate(315deg);
  -webkit-transition: 0.4s;
  transition: 0.4s;
}

.p-header__nav {
  position: relative;
}

/* p-nav */
.p-nav {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  opacity: 0;
  width: 100%;
  height: 100vh;
  z-index: 1;
  transition: all 0.5s;
}

.p-nav::before {
  background-color: rgba(0, 0, 0, .5);
  content: "";
  height: 100%;
  left: 0;
  position: absolute;
  right: 0;
  top: 0;
  -webkit-transition: all 0.5s;
  transition: all 0.5s;
  width: 100%;
  z-index: -1;
}

.p-nav__items {
  background-color: #121212;
  height: 100vh;
  max-width: 300px;
  padding: 100px 20px;
  -webkit-transform: translateX(-100%);
  transform: translateX(-100%);
  -webkit-transition: all 0.4s;
  transition: all 0.4s;
}

.p-nav__item a {
  display: block;
  padding: 15px 10px;
  color: #fff;
  text-decoration: none;
}

.p-nav__item {
  border-top: 1px solid #fff;
  color: #fff;
  text-transform: uppercase;
  list-style: none;
}

.p-nav__item:last-child {
  border-bottom: 1px solid #fff;
}

.p-nav.open {
  opacity: 1;
}

.p-nav__items.open {
  -webkit-transform: translateX(0);
  transform: translateX(0);
  -webkit-transition: all 0.5s;
  transition: all 0.5s;
}

jQuery

jQuery(function ($) {

  $('.js-nav').on('click', function () {
    $('.p-nav').toggleClass('open');
    $('.p-nav__items').toggleClass('open');
  });
  $('.p-header__btn').on('click', function () {
    $('.p-header__bar').toggleClass('active');
  }); 
}); 

作りたいもの:ハンバーガーボタンと中央からふわっと出てくるドロワーメニュー

See the Pen Untitled by Tomo56 (@tomokoro) on CodePen.

HTML

<header class="p-header js-header">
    <div class="p-header__inner">
      <h1 class="p-header__site-name">
        <a href="index.html">MyCode</a>
      </h1>
        <div class="p-header__hamburger u-sp js-hamburger">
          <span></span>
          <span></span>
          <span></span>
        </div>
        <nav class="p-header__nav p-pc-nav u-pc">
          <ul class="p-pc-nav__items">
            <li class="p-pc-nav__item"><a href="#vision">メニュー1</a></li>
            <li class="p-pc-nav__item"><a href="#service">メニュー2</a></li>
            <li class="p-pc-nav__item"><a href="#news">メニュー3</a></li>
          </ul>
        </nav>
    </div>
    <nav class="p-header__nav p-sp-nav js-drawer">
      <div class="p-sp-nav__inner">
        <ul class="p-sp-nav__items">
          <li class="p-sp-nav__item"><a href="#vision">メニュー1</a></li>
          <li class="p-sp-nav__item"><a href="#service">メニュー2</a></li>
          <li class="p-sp-nav__item"><a href="#news">メニュー3</a></li>
        </ul>
      </div>
    </nav>
  </header>

CSS

.p-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  height: 75px;
  background-color: #F7F7EE;
}

.p-header__inner {
  height: inherit;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-pack: justify;
      -ms-flex-pack: justify;
          justify-content: space-between;
  padding: 12px 15px;
}
@media screen and (min-width: 768px) {
  .p-header__inner {
    max-width: 75rem;
    margin-left: auto;
    margin-right: auto;
    padding-left: 15px;
    padding-right: 15px;
  }
}

.p-header__site-name a {
  font-size: 24px;
  font-weight: 700;
  line-height: 1.3333333333;
/*   padding: 20px 0; */
  font-family: "'Roboto', sans-serif";
  letter-spacing: -0.08em;
}
@media screen and (min-width: 768px) {
  .p-header__site-name a {
    letter-spacing: -0.05em;
  }
}

.p-header__hamburger {
  width: 25px;
  height: 16px;
  position: fixed;
  top: 30px;
  right: 15px;
  z-index: 9999;
  cursor: pointer;
}

.p-header__hamburger.is-active {
  right: 20px;
}

.p-header__hamburger span {
  position: absolute;
  display: inline-block;
  width: 25px;
  height: 2px;
  background-color: #2B3F2F;
  transition: translate 0.4s ease-in-out;
}

.p-header__hamburger span:nth-child(1) {
  top: 0;
}

.p-header__hamburger span:nth-child(2) {
  top: 8px;
}

.p-header__hamburger span:nth-child(3) {
  top: 16px;
}

.p-header__hamburger.is-active span:nth-of-type(1) {
  -webkit-transform: translateY(8px) rotate(-45deg);
          transform: translateY(8px) rotate(-45deg);
  -webkit-transition: rotate 0.4s;
  transition: translate 0.4s ease-in-out;
}

.p-header__hamburger.is-active span:nth-of-type(2) {
  opacity: 0;
}

.p-header__hamburger.is-active span:nth-of-type(3) {
  -webkit-transform: translateY(-8px) rotate(45deg);
          transform: translateY(-8px) rotate(45deg);
  -webkit-transition: rotate 0.4s;
  transition: translate 0.4s ease-in-out;
}

/* p-pc-nav */

.p-pc-nav__items {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
}

.p-pc-nav__item {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.3125;
  letter-spacing: 0.05em;
  list-style: none;
}

.p-pc-nav__item a {
  padding: 26px 32px;
}

/* p-sp-nav */

.p-sp-nav {
  position: relative;
  top: -75px;
  left: 0;
  width: 100%;
  height: 100vh;
  z-index: -100;
  display: none;
  opacity: 0;
  -webkit-transition: opacity 0.3s;
  transition: opacity 0.3s;
}

.p-sp-nav.is-open {
  display: block;
  opacity: 1;
  -webkit-transition: opacity 0.3s;
  transition: opacity 0.3s;
  z-index: 5000;
}

.p-sp-nav__inner {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  background-color: #F7F7EE;
  width: 100%;
  height: inherit;
}

.p-sp-nav__items {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
}

.p-sp-nav__item {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.875;
  letter-spacing: 0.05em;
  font-family: "'Roboto', sans-serif";
  list-style: none;
}

.p-sp-nav__item a {
  display: inline-block;
  padding: 25px 40px;
}

/* sp/pc表示切り替え */
.u-sp {
  display: block;
}
@media screen and (min-width: 768px) {
  .u-sp {
    display: none;
  }
}

.u-pc {
  display: none;
}
@media screen and (min-width: 768px) {
  .u-pc {
    display: block;
  }
}

jQuery

jQuery(function ($) { 

$(".js-hamburger, .js-drawer, .p-sp-nav__item a").click(function () {
    $(".p-header__hamburger").toggleClass("is-active");
    $(".p-sp-nav").toggleClass("is-open");
    $(".js-drawer").fadeIn();
    $("html").toggleClass("is-fixed");
  });

  $(".p-sp-nav__item a").click(function () {
    $(".p-header__hamburger").toggleClass("is-active");
    $(".p-sp-nav").toggleClass("is-open");
  });
  });
よかったらシェアしてね!
  • URLをコピーしました!
目次