見出し画像

40代高卒者が製造業からIT転職する【ハンバーガーメニュー】

こんにちは、フローです。

今回は、前回紹介したサンプルページがスマホ対応していなかったので、

ハンバーガーメニューを追加したサンプルページを作りました。

私もそうでしたが、初心者が一番初めに挫折するのが、このハンバーガーメニューだと思います。

目次

ハンバーガーメニューの例

まずは、完成したものを載せておきます


では、早速書いて聞きましょう

HTML

<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>サンプルサイト</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css"
    integrity="sha256-l85OmPOjvil/SOvVt3HnSSjzF1TUMyT9eV0c2BzEGzU=" crossorigin="anonymous" />
  <link rel="stylesheet" href="style.css">
</head>

<body>

  <header>
    <a href="#" class="logo">ロゴ</a>
    <nav>
      <ul class="nav-list">
        <li><a href="#">製品</a></li>
        <li><a href="#">サポート</a></li>
        <li><a href="#">お問い合わせ</a></li>
      </ul>

      <div class="hamburger">
        <span class="bar"></span>
        <span class="bar"></span>
        <span class="bar"></span>
      </div>
    </nav>
  </header>

  <main>
    <section class="hero">
      <div class="hero-text">
        <h1>タイトル</h1>
        <p>キャッチフレーズや短い説明文</p>
        <a href="#" class="cta-button">もっと詳しく</a>
      </div>
    </section>
  </main>
  <section class="products">
    <h2>製品一覧</h2>
    <div class="product-card">
      <img src="product1.jpg" alt="製品1">
      <h3>製品1</h3>
      <p>製品1に関する簡単な説明文</p>
      <a href="#" class="cta-button">詳細を見る</a>
    </div>
    <div class="product-card">
      <img src="product2.jpg" alt="製品2">
      <h3>製品2</h3>
      <p>製品2に関する簡単な説明文</p>
      <a href="#" class="cta-button">詳細を見る</a>
    </div>
    <div class="product-card">
      <img src="product3.jpg" alt="製品3">
      <h3>製品3</h3>
      <p>製品3に関する簡単な説明文</p>
      <a href="#" class="cta-button">詳細を見る</a>
    </div>
  </section>

  <section class="contact">
    <h2>お問い合わせ</h2>
    <form>
      <label for="name">お名前</label>
      <input type="text" id="name" name="name">
      <label for="email">メールアドレス</label>
      <input type="email" id="email" name="email">

      <label for="message">メッセージ</label>
      <textarea id="message" name="message"></textarea>

      <button type="submit" class="cta-button">送信する</button>

    </form>
  </section>
  <footer>
    <p>© 2023 サンプルサイト. All rights reserved.</p>
  </footer>

  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="main.js"></script>
</body>

</html>

前回と違うところは、ハンバーガーメニュー用の記述が追加されているところです。

  <header>
    <a href="#" class="logo">ロゴ</a>
    <nav>
      <ul class="nav-list">
        <li><a href="#">製品</a></li>
        <li><a href="#">サポート</a></li>
        <li><a href="#">お問い合わせ</a></li>
      </ul>

      <div class="hamburger">
        <span class="bar"></span>
        <span class="bar"></span>
        <span class="bar"></span>
      </div>
    </nav>
  </header>

この、ヘッダー部分です。

CSS の例

CSS

/* 基本スタイル */
* {
  box-sizing: border-box;
}

body {
  font-family: Arial, sans-serif;
  line-height: 1.6;
  margin: 0;
  padding: 0;
}

/* ヘッダーセクション */
header {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 30px;
  background-color: #222;
}

header .logo {
  color: #fff;
  font-size: 24px;
  font-weight: bold;
  text-decoration: none;
  margin-right: auto;
}

/* スマホ表示用のスタイル */
@media (max-width: 767px) {
  header {
    padding: 15px;
    /* 小さい画面に対してパディングを減らします。 */
  }

  header .logo {
    font-size: 18px;
    /* 小さい画面に対してロゴのフォントサイズを減らします。 */
  }
}

/* ナビゲーション */
nav {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  z-index: 10;
}

nav ul {
  display: flex;
  margin: 0;
  padding: 0;
  list-style: none;
}

nav li {
  margin-left: 30px;
}

nav a {
  color: #fff;
  font-size: 18px;
  text-decoration: none;
}

nav a:hover {
  color: #ccc;
}

/* スマホ表示用のスタイル */
@media (max-width: 767px) {
  .nav-list {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: fixed;
    top: 0;
    right: 0;
    width: 100%;
    height: 100%;
    padding: 80px 0;
    background-color: rgba(0, 0, 0, 0);
    opacity: 0;
    visibility: hidden;
    z-index: 0;
    transition: all 0.3s ease-in-out;
  }

  .nav-list li {
    margin-bottom: 40px;
  }

  .nav-list a {
    color: #fff;
    font-size: 24px;
    text-transform: uppercase;
  }

  .nav-list.show {
    opacity: 0.9;
    visibility: visible;
    z-index: 1;
  }

  nav ul.show {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    background-color: #222;
    flex-direction: column;
  }

  nav ul.show {
    display: flex;
  }

  nav li {
    margin-left: 0;
    padding: 15px;
  }
}

/* ハンバーガーアイコン */
.hamburger {
  display: none;
  position: relative;
  z-index: 1;
  cursor: pointer;
}

.hamburger .bar {
  display: block;
  width: 25px;
  height: 3px;
  margin: 5px 0;
  background-color: #f0f0f0;
  border-radius: 5px;
  transition: all 0.3s ease-in-out;
}

.hamburger.active .bar:first-child {
  transform: translateY(8px) rotate(45deg);
}

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

.hamburger.active .bar:last-child {
  transform: translateY(-8px) rotate(-45deg);
}

/* レスポンシブデザイン */
@media screen and (max-width: 768px) {
  .show li {
    margin-bottom: 20px;
  }

  .hamburger {
    display: block;
  }

  .show {
    padding-top: 120px;
  }
}

/* ヒーローセクション */
.hero {
  position: relative;
  background-image: url("img/greyson-joralemon-9IBqihqhuHc-unsplash.jpg");
  /* ここに画像のパスを指定 */
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}

/* ヒーローセクションの疑似要素にフィルターを適用 */
.hero:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  /* ここの0.3は不透明度を調整する値です。必要に応じて変更してください */

}

.hero-text {
  color: #fff;
  position: relative;
  z-index: 0;
}

.hero-text h1 {
  font-size: 48px;
  margin-bottom: 10px;
}

.hero-text p {
  font-size: 24px;
  margin-bottom: 30px;
}

.cta-button {
  display: inline-block;
  background-color: #ff7f50;
  color: #fff;
  padding: 12px 24px;
  font-size: 18px;
  text-decoration: none;
  border-radius: 4px;
}

.cta-button:hover {
  background-color: #f4511e;
  text-decoration: none;
}

/* その他のスタイルはここに追加 */


/* 製品セクション */
.products {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  grid-gap: 30px;
  padding: 60px 30px;
}

.product-card {
  background-color: #f4f4f4;
  padding: 20px;
  text-align: center;
}

.product-card img {
  max-width: 100%;
  height: auto;
}

.product-card h3 {
  margin-top: 20px;
  margin-bottom: 10px;
}

.product-card p {
  margin-bottom: 20px;
}

/* お問い合わせセクション */
.contact {
  background-color: #333;
  color: #fff;
  padding: 60px 30px;
}

.contact form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  max-width: 1200px;
  /* MacBook 14-16インチに対応 */
  margin: 0 auto;
}

.contact label {
  display: block;
  margin-bottom: 5px;
}

.contact input,
.contact textarea {
  background-color: #555;
  border: none;
  color: #fff;
  padding: 10px;
  width: 100%;
  resize: none;
}

.contact .input-field {
  flex: 1;
  margin-bottom: 20px;
}

.contact textarea {
  flex-basis: 100%;
  margin-bottom: 20px;
}

.contact input[type="submit"] {
  background-color: #f4f4f4;
  color: #333;
  cursor: pointer;
  width: 50%;
  /* 送信ボタンの幅を調整 */
  margin: 0 auto;
}

.contact input[type="submit"]:hover {
  background-color: #ddd;
}

/* フッターセクション */
footer {
  background-color: #222;
  color: #fff;
  padding: 20px;
  text-align: center;
}

長いですね。でも、こんなものです。

ハンバーガーメニュー用に色々と追加しています。

/* ナビゲーション */
nav {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  z-index: 10;
}

nav ul {
  display: flex;
  margin: 0;
  padding: 0;
  list-style: none;
}

nav li {
  margin-left: 30px;
}

nav a {
  color: #fff;
  font-size: 18px;
  text-decoration: none;
}

nav a:hover {
  color: #ccc;
}

/* スマホ表示用のスタイル */
@media (max-width: 767px) {
  .nav-list {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: fixed;
    top: 0;
    right: 0;
    width: 100%;
    height: 100%;
    padding: 80px 0;
    background-color: rgba(0, 0, 0, 0);
    opacity: 0;
    visibility: hidden;
    z-index: 0;
    transition: all 0.3s ease-in-out;
  }

  .nav-list li {
    margin-bottom: 40px;
  }

  .nav-list a {
    color: #fff;
    font-size: 24px;
    text-transform: uppercase;
  }

  .nav-list.show {
    opacity: 0.9;
    visibility: visible;
    z-index: 1;
  }

  nav ul.show {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    background-color: #222;
    flex-direction: column;
  }

  nav ul.show {
    display: flex;
  }

  nav li {
    margin-left: 0;
    padding: 15px;
  }
}

/* ハンバーガーアイコン */
.hamburger {
  display: none;
  position: relative;
  z-index: 1;
  cursor: pointer;
}

.hamburger .bar {
  display: block;
  width: 25px;
  height: 3px;
  margin: 5px 0;
  background-color: #f0f0f0;
  border-radius: 5px;
  transition: all 0.3s ease-in-out;
}

.hamburger.active .bar:first-child {
  transform: translateY(8px) rotate(45deg);
}

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

.hamburger.active .bar:last-child {
  transform: translateY(-8px) rotate(-45deg);
}

/* レスポンシブデザイン */
@media screen and (max-width: 768px) {
  .show li {
    margin-bottom: 20px;
  }

  .hamburger {
    display: block;
  }

  .show {
    padding-top: 120px;
  }
}

こちらもも、ヘッダー部分を追加記述しています。

jQueryの例

jQuery

$(document).ready(function() {
  $('.hamburger').click(function() {
    $(this).toggleClass('active');
    $('.nav-list').toggleClass('show');
  });
});

$(window).resize(function() {
  if ($(window).width() > 768) {
    $('.nav-list').removeClass('show');
    $('.hamburger').removeClass('active');
  }
});

こちらも追加です。

まとめ

一気に説明すると、とても長くなってしまうので、今回はコード例だけにしておきます。

次回からは、ハンバーガーメニューの記述例をひとつずつ見ていきましょう。

レッツチャレンジ!

この記事が気に入ったらサポートをしてみませんか?