コピペで完結!ドロワーメニュー#01【上・下・左・右からスライド】

html/css/js

ドロワーメニュー(上下左右スライド)

上・下・左・右からスライドする「ドロワーメニュー」を、HTML / CSS / JS だけで実装します。
各方向ごとにデモタブ切替のコードブロック(コピー可)を用意しました。

コードについて 本記事のコードは ChatGPT による生成をベースに調整しています。ご利用の環境でテストの上ご使用ください。
免責 本コードの利用に伴う不具合・損害について、当サイトは責任を負いません。自己責任にてご利用ください。

ドロワーメニュー(上からスライド)

画面上端から現れるドロワー。背景クリック/ESC キー/閉じるで閉じます。

コード(コピー可)

/* =========================
   上スライド:CSS(行コメント付き)
   ========================= */

/* 開くボタンの見た目 */
.cp-drawer .btn{
  background:#0b6bff;   /* ボタンの背景色(アクセントカラー) */
  color:#fff;           /* 文字色は白 */
  border:none;          /* 枠線なしでフラットに */
  border-radius:12px;   /* 角丸 */
  padding:10px 14px;    /* クリックしやすい余白 */
  cursor:pointer;       /* カーソルはポインタに */
}

/* 暗幕レイヤー(初期は非表示) */
.cp-drawer .layer{
  position:fixed;                 /* 画面全体に固定配置 */
  inset:0;                        /* top/right/bottom/left を 0 に(全画面) */
  background:rgba(2,8,23,.5);     /* 半透明の黒っぽい色で背面を暗くする */
  opacity:0;                      /* 初期は透明 */
  pointer-events:none;            /* 初期はクリックを受けない */
  transition:.2s;                 /* フェードのトランジション */
  z-index:9999;                   /* パネルの背面・前景の兼ね合いで十分高く */
}

/* data-open="1" になったら暗幕を有効化(表示&クリック可) */
.cp-drawer[data-open="1"] .layer{
  opacity:1;            /* 不透明に */
  pointer-events:auto;  /* クリック可能に(背景クリックで閉じる用) */
}

/* ドロワーパネル本体(上からスライドイン) */
.cp-drawer .panel{
  position:fixed;                          /* 画面に固定配置 */
  left:0; right:0; top:0;                  /* 画面上端に横いっぱい */
  background:#fff;                         /* パネルの背景は白 */
  color:#0f172a;                           /* テキスト色(濃紺系) */
  max-height:min(80vh,560px);              /* 高さ上限(ビューポート80%か560pxの小さい方) */
  overflow:auto;                           /* コンテンツが溢れたら内部スクロール */
  transform:translateY(-100%);             /* 初期は画面の上外へ待避 */
  transition:transform .24s ease-out;      /* スライドアニメーション */
  border-bottom:1px solid #e5e7eb;         /* 下端にボーダー */
  box-shadow:0 8px 24px rgba(2,8,23,.18);  /* 下方向へ落ちる影感 */
  z-index:10000;                           /* 暗幕より前面に配置 */
}

/* data-open="1" でスライドイン(画面内へ) */
.cp-drawer[data-open="1"] .panel{
  transform:translateY(0);
}

/* パネルのヘッダー(タイトル&閉じるボタンの横並び) */
.cp-drawer .panel header{
  display:flex;                             /* 横並び */
  align-items:center;                       /* 垂直中央揃え */
  justify-content:space-between;            /* 左右に振り分け */
  padding:14px 16px;                        /* 内側余白 */
  border-bottom:1px solid #e5e7eb;          /* 下線で区切り */
  position:sticky; top:0;                   /* スクロールしても上部に固定 */
  background:#fff;                          /* 固定時に下が透けないよう白背景 */
  z-index:1;                                /* ヘッダーを一段前面に */
}

/* 見出しの最小体裁 */
.cp-drawer .panel header h3{
  font-size:16px;                           /* コンパクトなサイズ */
  margin:0;                                 /* 余白リセット */
}

/* 閉じるボタンの見た目 */
.cp-drawer .panel .close{
  appearance:none;                          /* ブラウザのデフォルト外観を無効化 */
  background:#fff;                          /* 背景は白 */
  border:1px solid #e5e7eb;                 /* 薄い枠線 */
  border-radius:10px;                        /* 角丸 */
  padding:6px 10px;                          /* クリックしやすい余白 */
  cursor:pointer;                            /* カーソルはポインタ */
}

/* ナビゲーション領域の外側余白 */
.cp-drawer nav{
  padding:10px 12px;
}

/* ナビゲーションの各リンク */
.cp-drawer nav a{
  display:block;                             /* ブロック化して当たり判定を広く */
  padding:10px 12px;                         /* 内側余白 */
  border-radius:10px;                        /* 角丸でチップ風 */
  color:#0f172a;                             /* 文字色 */
  text-decoration:none;                      /* 下線なし */
}

/* リンクのホバー時の背景 */
.cp-drawer nav a:hover{
  background:#f8fafc;
}

/* フォーカスリング(アクセシビリティ向上) */
.cp-drawer .btn:focus,
.cp-drawer .close:focus,
.cp-drawer .panel:focus{
  outline:2px solid #93c5fd;                /* 水色のアウトライン */
  outline-offset:2px;                        /* 少し離して見やすく */
}

ドロワーメニュー(下からスライド)

画面下端から現れるドロワー。背景クリック/ESC キー/閉じるで閉じます。

コード(コピー可)

/* =========================================================
   下スライド:CSS(詳細コメント付き・そのままコピペ可)
   ========================================================= */

/* 「開く」ボタンのスタイル */
.cp-drawer .btn{
  background:#0b6bff;   /* アクセントカラー(サイト基調色に合わせて変更可) */
  color:#fff;           /* テキストは白でコントラスト確保 */
  border:none;          /* デフォルト枠線は消す */
  border-radius:12px;   /* 角を丸くしてタップしやすく */
  padding:10px 14px;    /* 触りやすいタッチ余白 */
  cursor:pointer;       /* ポインタ表示 */
}

/* 暗幕レイヤー(初期は非表示) */
.cp-drawer .layer{
  position:fixed;                 /* 画面全体に固定配置 */
  inset:0;                        /* top/right/bottom/left を 0 に(全画面) */
  background:rgba(2,8,23,.5);     /* 半透明ダークで背面を抑制 */
  opacity:0;                      /* 初期は透明(非表示) */
  pointer-events:none;            /* 初期はクリック不可 */
  transition:.2s;                 /* フェード速度(好みで) */
  z-index:9999;                   /* パネルのすぐ下になるよう高めに設定 */
}

/* data-open="1" で暗幕を表示&クリック可 */
.cp-drawer[data-open="1"] .layer{
  opacity:1;            /* フェードイン */
  pointer-events:auto;  /* クリックを拾えるように(背景クリックで閉じる) */
}

/* ドロワーパネル本体(下からスライドイン) */
.cp-drawer .panel{
  position:fixed;                          /* 画面に固定 */
  left:0; right:0; bottom:0;               /* 画面下端に横いっぱいで固定 */
  background:#fff;                         /* 白背景 */
  color:#0f172a;                           /* テキスト色 */
  max-height:min(80vh,560px);              /* 上限高さ(ビューポート80% or 560px) */
  overflow:auto;                           /* コンテンツが溢れたら内部スクロール */
  transform:translateY(100%);              /* 初期は画面の下外へ待避 */
  transition:transform .24s ease-out;      /* スライドアニメーション */
  border-top:1px solid #e5e7eb;            /* 上端に薄いボーダー */
  box-shadow:0 -8px 24px rgba(2,8,23,.18); /* 上方向へ落ちる影で浮かせる */
  z-index:10000;                           /* 暗幕より前面に */
}

/* data-open="1" で画面内へスライドイン */
.cp-drawer[data-open="1"] .panel{
  transform:translateY(0);
}

/* パネル内ヘッダー(タイトル+閉じるボタン) */
.cp-drawer .panel header{
  display:flex;                             /* 横並び */
  align-items:center;                       /* 垂直中央 */
  justify-content:space-between;            /* タイトル左/ボタン右 */
  padding:14px 16px;                        /* 内側余白 */
  border-bottom:1px solid #e5e7eb;          /* 下線で区切り */
  position:sticky; top:0;                   /* スクロールしても上部に固定 */
  background:#fff;                          /* 固定時に背景透けを防ぐ */
  z-index:1;                                /* 内容より前に */
}

/* 見出しの体裁(リセット+最小サイズ) */
.cp-drawer .panel header h3{
  font-size:16px;
  margin:0;
}

/* 閉じるボタンの見た目(控えめ) */
.cp-drawer .panel .close{
  appearance:none;                          /* ブラウザデフォルト外観を無効化 */
  background:#fff;
  border:1px solid #e5e7eb;
  border-radius:10px;
  padding:6px 10px;
  cursor:pointer;
}

/* ナビ領域の外側余白 */
.cp-drawer nav{
  padding:10px 12px;
}

/* ナビリンク(大きめタップ領域) */
.cp-drawer nav a{
  display:block;
  padding:10px 12px;
  border-radius:10px;
  color:#0f172a;
  text-decoration:none;
}

/* ホバー背景で可読性と選択性を上げる */
.cp-drawer nav a:hover{
  background:#f8fafc;
}

/* アクセシビリティ:フォーカスリングを明示 */
.cp-drawer .btn:focus,
.cp-drawer .close:focus,
.cp-drawer .panel:focus{
  outline:2px solid #93c5fd;   /* 水色のアウトライン */
  outline-offset:2px;           /* 要素から少し離す */
}

ドロワーメニュー(左からスライド)

画面左端から現れるドロワー。背景クリック/ESC キー/閉じるで閉じます。

コード(コピー可)

/* =========================================================
   左スライド:CSS(詳細コメント付き・そのままコピペ可)
   ========================================================= */

/* 「開く」ボタンのスタイル */
.cp-drawer .btn{
  background:#0b6bff;   /* アクセントカラー(必要に応じて変更) */
  color:#fff;           /* 文字色は白 */
  border:none;          /* 既定の枠線を消す */
  border-radius:12px;   /* 角丸でタップしやすく */
  padding:10px 14px;    /* 触りやすい余白 */
  cursor:pointer;       /* ポインタを表示 */
}

/* 暗幕レイヤー(初期は非表示) */
.cp-drawer .layer{
  position:fixed;                 /* 画面全体を覆う */
  inset:0;                        /* top/right/bottom/left 全て 0 */
  background:rgba(2,8,23,.5);     /* 半透明ダーク */
  opacity:0;                      /* 初期は透明 */
  pointer-events:none;            /* クリック不可(非表示扱い) */
  transition:.2s;                 /* フェード速度 */
  z-index:9999;                   /* パネルの背面直下に来るよう高めに */
}

/* data-open="1" で暗幕を表示&クリック可 */
.cp-drawer[data-open="1"] .layer{
  opacity:1;            /* フェードイン */
  pointer-events:auto;  /* クリックを拾えるようにする(背景クリックで閉じる) */
}

/* ドロワーパネル本体(左からスライドイン) */
.cp-drawer .panel{
  position:fixed;                          /* 画面に固定配置 */
  top:0; left:0;                            /* 左上を起点にする */
  height:100dvh;                            /* 全高(モバイルの動的ビューポート考慮) */
  width:min(86vw,360px);                    /* ビューポートの 86% を上限にしつつ 360px 目安 */
  max-width:420px;                          /* さらに最大幅を制限 */
  background:#fff;                          /* 白背景 */
  color:#0f172a;                            /* テキスト色 */
  transform:translateX(-100%);              /* 初期は画面の左外へ退避 */
  transition:transform .24s ease-out;       /* スライドアニメーション */
  border-right:1px solid #e5e7eb;           /* 右端に薄いボーダー */
  box-shadow:6px 0 20px rgba(2,8,23,.15);   /* 右方向へ落ちる影で浮かせる */
  z-index:10000;                            /* 暗幕より前面 */
  display:flex; flex-direction:column;      /* 中身は縦並び */
}

/* data-open="1" で画面内へスライドイン */
.cp-drawer[data-open="1"] .panel{
  transform:translateX(0);
}

/* パネル内ヘッダー(タイトル+閉じるボタン) */
.cp-drawer .panel header{
  display:flex; align-items:center; justify-content:space-between; /* 横並び配置 */
  padding:14px 16px;                       /* 内側余白 */
  border-bottom:1px solid #e5e7eb;         /* 下罫線で区切り */
  background:#fff;                         /* 背景透け防止 */
  z-index:1;
}

/* 見出しの体裁(リセット+最小サイズ) */
.cp-drawer .panel header h3{
  font-size:16px;
  margin:0;
}

/* 閉じるボタンの見た目(控えめ) */
.cp-drawer .panel .close{
  appearance:none; background:#fff;
  border:1px solid #e5e7eb; border-radius:10px;
  padding:6px 10px; cursor:pointer;
}

/* ナビ領域の外側余白 */
.cp-drawer nav{ padding:10px 12px }

/* ナビリンク(大きめタップ領域) */
.cp-drawer nav a{
  display:block;
  padding:10px 12px;
  border-radius:10px;
  color:#0f172a;
  text-decoration:none;
}

/* ホバー背景で可読性と選択性を上げる */
.cp-drawer nav a:hover{ background:#f8fafc }

/* アクセシビリティ:フォーカスリングを明示 */
.cp-drawer .btn:focus,
.cp-drawer .close:focus,
.cp-drawer .panel:focus{
  outline:2px solid #93c5fd;   /* 水色のアウトライン */
  outline-offset:2px;           /* 要素から少し離して視認性UP */
}

ドロワーメニュー(右からスライド)

画面右端から現れるドロワー。背景クリック/ESC キー/閉じるで閉じます。

コード(コピー可)

/* =========================================================
   右スライド:CSS(詳細コメント付き・そのままコピペ可)
   ========================================================= */

/* 「開く」ボタンの基本スタイル */
.cp-drawer .btn{
  background:#0b6bff;  /* アクセントカラー */
  color:#fff;          /* 白文字 */
  border:none;         /* デフォ枠線なし */
  border-radius:12px;  /* 角丸でタップしやすい */
  padding:10px 14px;   /* クリック領域を確保 */
  cursor:pointer;      /* ポインタ形状 */
}

/* 背景の暗幕(初期は非表示) */
.cp-drawer .layer{
  position:fixed;                /* 画面全体を覆う */
  inset:0;                       /* top/right/bottom/left を 0 に */
  background:rgba(2,8,23,.5);    /* 半透明の暗幕 */
  opacity:0;                     /* 初期は透明 */
  pointer-events:none;           /* クリック不可(実質非表示) */
  transition:.2s;                /* フェードイン/アウト */
  z-index:9999;                  /* パネルの直下に来るよう高めに */
}

/* data-open="1" 時に暗幕をアクティブ化(表示&クリック可) */
.cp-drawer[data-open="1"] .layer{
  opacity:1;
  pointer-events:auto;
}

/* ドロワーパネル本体(右からスライドイン) */
.cp-drawer .panel{
  position:fixed;                          /* 画面に固定配置 */
  top:0; right:0;                           /* 右上を起点にする */
  height:100dvh;                            /* 動的ビューポート高(モバイル考慮) */
  width:min(86vw,360px);                    /* SP で広がりすぎない幅 */
  max-width:420px;                          /* さらに最大幅を制限 */
  background:#fff;                          /* 白背景 */
  color:#0f172a;                            /* テキスト色 */
  transform:translateX(100%);               /* 初期は画面外(右) */
  transition:transform .24s ease-out;       /* スライド速度とイージング */
  border-left:1px solid #e5e7eb;            /* 左端に薄いボーダー */
  box-shadow:-6px 0 20px rgba(2,8,23,.15);  /* 左向きの影で浮かせる */
  z-index:10000;                            /* 暗幕より前面 */
  display:flex; flex-direction:column;      /* 中身を縦並びに */
}

/* data-open="1" で画面内へスライドイン */
.cp-drawer[data-open="1"] .panel{
  transform:translateX(0);
}

/* パネルヘッダー(タイトル+閉じる) */
.cp-drawer .panel header{
  display:flex; align-items:center; justify-content:space-between; /* 横並び配置 */
  padding:14px 16px;                        /* 内側余白 */
  border-bottom:1px solid #e5e7eb;          /* 下罫線 */
  background:#fff;                          /* 背景透け防止 */
  z-index:1;
}

/* 見出しの最小体裁 */
.cp-drawer .panel header h3{
  font-size:16px;
  margin:0;
}

/* 閉じるボタン(控えめな見た目) */
.cp-drawer .panel .close{
  appearance:none; background:#fff;
  border:1px solid #e5e7eb; border-radius:10px;
  padding:6px 10px; cursor:pointer;
}

/* ナビ領域 */
.cp-drawer nav{ padding:10px 12px }

/* ナビリンク:タップ領域を大きめに */
.cp-drawer nav a{
  display:block;
  padding:10px 12px;
  border-radius:10px;
  color:#0f172a;
  text-decoration:none;
}

/* ホバー時の薄いハイライト */
.cp-drawer nav a:hover{ background:#f8fafc }

/* アクセシビリティ:フォーカスリングを明示 */
.cp-drawer .btn:focus,
.cp-drawer .close:focus,
.cp-drawer .panel:focus{
  outline:2px solid #93c5fd;   /* 水色アウトライン */
  outline-offset:2px;           /* 要素から少し離す */
}

コメント