【ソースコード】中級編:ストアサイト(カフェ)/パララックス

目次

※コーディングの解説はCSSのコメントを参照

ディレクトリ構成

    
store
 ├─img
 │  ├─favicon.ico
 │  ├─logo.svg
 │  ├─about.jpg
 │  ├─location.jpg
 │  ├─mainvisual.jpg
 │  └─menu.jpg
 │
 ├─css
 │  └─style.css
 │
 ├─js
 │  └─main.js
 │
 └─index.html
    
  

HTML(index.html)

別タブで開く
index.html

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <title>COFFEE</title>
    <meta name="description" content="テキストテキストテキストテキストテキストテキストテキストテキス">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="img/favicon.ico">
    <link rel="stylesheet" href="https://unpkg.com/ress/dist/ress.min.css">
    <link rel="stylesheet" href="css/style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="js/main.js"></script>
  </head>

  <body>
    <header id="header">
      <nav>
        <ul>
          <li><a href="#menu">MENU</a></li>
          <li><a href="#about">ABOUT</a></li>
          <li><a href="#location">LOCATION</a></li>
        </ul>
      </nav>
      <h1 class="site-title"><img src="img/logo.svg" alt="COFFEE"></h1>
    </header>

    <main>
      <section id="menu">
        <div class="menu-img fixed-bg">
          <h2 class="sec-title">MENU</h2>
        </div>

        <div class="menu-content wrapper">
          <div class="menu-item">
            <h3 class="item-title">COFFEE</h3>
            <dl>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
            </dl>
          </div>
          <div class="menu-item">
            <h3 class="item-title">FOOD</h3>
            <dl class="food">
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
            </dl>
            <h3 class="item-title">OTHER</h3>
            <dl>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
              <dt>XXXXXXXX</dt>
              <dd>&yen;500</dd>
            </dl>
          </div>
        </div>
      </section>

      <section id="about">
        <div class="about-img fixed-bg">
          <h2 class="sec-title">ABOUT</h2>
        </div>

        <div class="about-content wrapper">
          <div class="about-item">
            <h3 class="item-title">COFFEE</h3>
            <ul>
              <li>
                テキストテキストテキストテキストテキストテキストテキストテキスト
                テキストテキストテキストテキストテキストテキストテキストテキスト
                テキストテキストテキストテキストテキストテキストテキストテキスト
                テキストテキストテキストテキストテキストテキストテキストテキスト
              </li>
              <li>
                テキストテキストテキストテキストテキストテキストテキストテキスト
                テキストテキストテキストテキストテキストテキストテキストテキスト
                テキストテキストテキストテキストテキストテキストテキストテキスト
                テキストテキストテキストテキストテキストテキストテキストテキスト
              </li>
              <li>
                テキストテキストテキストテキストテキストテキストテキストテキスト
                テキストテキストテキストテキストテキストテキストテキストテキスト
                テキストテキストテキストテキストテキストテキストテキストテキスト
                テキストテキストテキストテキストテキストテキストテキストテキスト
              </li>
              <li>
                テキストテキストテキストテキストテキストテキストテキストテキスト
                テキストテキストテキストテキストテキストテキストテキストテキスト
                テキストテキストテキストテキストテキストテキストテキストテキスト
                テキストテキストテキストテキストテキストテキストテキストテキスト
              </li>
            </ul>
            <a class="btn" href="#"><span>Read More</span></a>
          </div>
        </div>
      </section>

      <section id="location">
        <div class="location-img fixed-bg">
          <h2 class="sec-title">LOCATION</h2>
        </div>

        <div class="location-content wrapper">
          <div class="location-item">
            <h3 class="item-title">OUR STORE</h3>
            <div class="item-map">
              <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d4585.299293219977!2d139.7062494041788!3d35.64507269733178!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x60188b40ba660b1b%3A0x8858088b6942f55f!2z44CSMTUwLTAwMjIg5p2x5Lqs6YO95riL6LC35Yy65oG15q-U5a-_5Y2X!5e0!3m2!1sja!2sjp!4v1592748907506!5m2!1sja!2sjp"></iframe>
            </div>
            <div class="item-info">
              <p>
                X-XX-XX, Ebisuminami, Shibuya-ku, Tokyo 150-0022<br>
                東京都渋谷区恵比寿南X-XX-XX
              </p>
              <p>
                Open 7 days a Week<br>
                9:00am to 10:00pm
              </p>
              <p>Tel : XX-XXXX-XXXX</p>
            </div>
          </div>
        </div>
      </section>
    </main>

    <footer id="footer">
      <p>&copy; COFFEE</p>
    </footer>

  </body>
</html>

CSS(style.css)

別タブで開く
style.css

@charset "UTF-8";

html {
  font-size: 100%;
}
body {
  font-family: "Helvetica Neue", "Arial", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Meiryo", sans-serif;
  font-size: 0.9rem;
  line-height: 1.7;
}
a {
  text-decoration: none;
}
img {
  max-width: 100%;
}
li {
  list-style: none;
}
/*
filterのdrop-shadowでまわりをぼやっとさせる
*/
.site-title {
  padding: 0 16px;
  text-align: center;
  position: absolute;
  top: 45%;
  left: 0;
  right: 0;
  filter: drop-shadow(1px 1px 10px #c0c0c0);
}
.sec-title {
  width: 100%;
  color: #fff;
  text-align: center;
  font-size: 4.5rem;
  filter: drop-shadow(1px 1px 10px #c0c0c0);
  position: absolute;
  top: 30%;
}
/*
display: inline-block;
下線をテキスト幅にあわせる
*/
.item-title {
  font-size: 2.5rem;
  display: inline-block;
  border-bottom: solid 6px #e03131;
  margin-bottom: 70px;
}
/*
「background-attachment: fixed;」で背景を固定する
※background-attachment: fixed;とbackground-size: cover;
を同時に使用した場合、iOSでは正しく動作しない
*/
.fixed-bg {
  height: 300px;
  background-attachment: fixed;
  background-size: cover;
  background-position: center;
}
/*
中身のコンテンツ幅を指定するための共通クラス
*/
.wrapper {
  max-width: 1000px;
  padding: 90px 16px 150px 16px;
  margin: 0 auto;
  text-align: center;
}

/*-------------------------------------------
ヘッダー
-------------------------------------------*/
/*
min-height: 100vh;
メインビジュアルを画面の高さにあわせる
background~プロパティでメインビジュアルの表示方法を設定
*/
#header {
  width: 100%;
  min-height: 100vh;
  background-image: url(../img/mainvisual.jpg);
  background-size: cover;
  background-position: center top;
  background-repeat: no-repeat;
  padding: 20px 40px;
  position: relative;
}
nav ul {
  display: flex;
  justify-content: flex-end;
}
nav li {
  margin-left: 30px;
}
nav li a {
  color: #fff;
  font-weight: bold;
  transition: all  0.3s ease;
  filter: drop-shadow(1px 1px 2px #121212);
}
nav li a:hover {
  color: #e03131;
}

/*-------------------------------------------
Menu
-------------------------------------------*/
#menu {
  margin-top: 20px;
}
#menu .menu-img {
  background-image: url(../img/menu.jpg);
  position: relative;
}
#menu .menu-content {
  display: flex;
}
#menu .menu-item {
  width: 50%;
  padding: 0 45px;
}
#menu .menu-item:first-child {
  border-right: solid 1px #000;
}
#menu .menu-item dl {
  display: flex;
  flex-wrap: wrap;
}
#menu .menu-item dt {
  width: 87%;
  text-align: left;
  border-bottom: dotted 1px #000;
  margin-bottom: 25px;
}
#menu .menu-item dd {
  width: 13%;
  text-align: right;
  padding-top: 8px;
}
#menu .menu-item dl.food {
  margin-bottom: 55px;
}

/*-------------------------------------------
About
-------------------------------------------*/
#about {
  margin-top: 20px;
}
#about .about-img {
  background-image: url(../img/about.jpg);
  position: relative;
}
#about ul {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  margin-bottom: 40px;
}
#about li {
  width: 50%;
  line-height: 2;
  text-align: left;
  padding: 2%;
}
#about .btn {
  width: 160px;
  height: 50px;
  line-height: 3.5;
  display: inline-block;
  color: #000;
  padding: 0;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
}
#about .btn span {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
}
/*
.btn::before 右の赤線
.btn::after 下の赤線
.btn span::before 左の赤線
.btn span::after 上の赤線
*/
#about .btn::before,
#about .btn::after {
  position: absolute;
  content: "";
  right: 0;
  bottom: 0;
  background: #e03131;
  transition: all 0.3s ease;
}
#about .btn span::before,
#about .btn span::after {
  position: absolute;
  content: "";
  left: 0;
  top: 0;
  background: #e03131;
  transition: all 0.3s ease;
}
#about .btn::before,
#about .btn span::before {
  width: 2px;
  height: 50%;
}
#about .btn::after,
#about .btn span::after {
  width: 20%;
  height: 2px;
}
/*
ホバー時の縦の動作
右の赤線と左の赤線の高さを100%にする
*/
#about .btn:hover::before,
#about .btn span:hover::before {
  height: 100%;
}
/*
ホバー時の横の動作
下の赤線と上の赤線の横幅を100%にする
*/
#about .btn:hover::after,
#about .btn span:hover::after {
  width: 100%;
}

/*-------------------------------------------
Location
-------------------------------------------*/
#location {
  margin-top: 20px;
}
#location .location-img {
  background-image: url(../img/location.jpg);
  position: relative;
}
#location .item-map {
  /* グーグルマップをグレースケールにする */
  filter: grayscale(1);
  margin-bottom: 20px;
}
/* グーグルマップのサイズを設定 */
#location .item-map iframe {
  width: 100%;
  height: 400px;
  border: 0;
}
#location .item-info {
  text-align: left;
}
#location .item-info p {
  margin-bottom: 10px;
}

/*-------------------------------------------
フッター
-------------------------------------------*/
#footer {
  font-size: 0.5rem;
  padding: 10px 0;
  text-align: center;
}

/*-------------------------------------------
SP
-------------------------------------------*/
@media screen and (max-width: 767px) {
  .site-title {
    top: 42%;
  }
  .sec-title {
    font-size: 2rem;
    top: 26%;
  }
  .item-title {
    font-size: 1.25rem;
    margin-bottom :25px;
  }
  .fixed-bg {
    height: 94px;
    /* スマホ時は背景をスクロールするように変更 */
    background-attachment: scroll;
  }
  .wrapper {
    padding: 30px 16px 60px 16px;
  }

  /*-------------------------------------------
  Menu
  -------------------------------------------*/
  #menu {
    margin-top: 10px;
  }
  #menu .menu-content {
    flex-direction: column;
  }
  #menu .menu-item {
    width: 100%;
    padding: 0;
  }
  #menu .menu-item:first-child {
    border-right: none;
  }
  #menu .menu-item dl {
    margin-bottom: 10px;
  }
  #menu .menu-item dl.food {
    margin-bottom: 10px;
  }

  /*-------------------------------------------
  About
  -------------------------------------------*/
  #about ul {
    flex-direction: column;
  }
  #about li {
    width: 100%;
  }
}


JavaScript(main.js)

別タブで開く
main.js

$(function(){
  /*=================================================
  スムーススクロール
  ===================================================*/
  // ページ内のリンクをクリックした時に動作する
  $('a[href^="#"]').click(function(){
    // リンクを取得
    let href= $(this).attr("href");
    // ジャンプ先のid名をセット
    let target = $(href == "#" || href == "" ? 'html' : href);
    // トップからジャンプ先の要素までの距離を取得
    let position = target.offset().top;
    // animateでスムーススクロールを行う
    // 600はスクロール速度で単位はミリ秒
    $("html, body").animate({scrollTop:position}, 600, "swing");
    return false;
  });
})