← 動的パーツギャラリーへ戻る

パーツ番号 03-09-01 ・ JavaScript使用

左スライドインドロワーメニュー

ハンバーガーボタンをクリックすると左から半透明オーバーレイ付きのドロワーメニューが現れます。WordPressのスマホ表示グローバルナビの定番実装として使えます。

mobileclickjsoverlay

プレビュー

ソースコード

HTML

<div class="d03-09-01-stage">
          <header class="d03-09-01-bar">
            <button class="d03-09-01-btn" type="button" aria-label="メニューを開く"><span></span><span></span><span></span></button>
            <span class="d03-09-01-logo">SHOP</span>
          </header>
          <div class="d03-09-01-overlay"></div>
          <nav class="d03-09-01-drawer">
            <a href="#" onclick="return false" class="active">ホーム</a>
            <a href="#" onclick="return false">商品一覧</a>
            <a href="#" onclick="return false">セール情報</a>
            <a href="#" onclick="return false">お知らせ</a>
            <a href="#" onclick="return false">お問い合わせ</a>
          </nav>
        </div>

CSS

.prev.d03-09-01{padding:0;display:block}
        .d03-09-01-stage{position:relative;width:100%;height:100%;min-height:260px;overflow:hidden;background:#f8fafc}
        .d03-09-01-bar{display:flex;align-items:center;gap:12px;height:52px;padding:0 16px;background:#fff;box-shadow:0 1px 4px rgba(0,0,0,.08);position:relative;z-index:3}
        .d03-09-01-btn{width:36px;height:36px;border:none;background:transparent;cursor:pointer;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:5px}
        .d03-09-01-btn span{display:block;width:20px;height:2px;background:#111827;border-radius:2px;transition:transform .3s ease,opacity .3s ease}
        .d03-09-01-stage.open .d03-09-01-btn span:nth-child(1){transform:translateY(7px) rotate(45deg)}
        .d03-09-01-stage.open .d03-09-01-btn span:nth-child(2){opacity:0}
        .d03-09-01-stage.open .d03-09-01-btn span:nth-child(3){transform:translateY(-7px) rotate(-45deg)}
        .d03-09-01-logo{font-weight:800;letter-spacing:.1em;color:#111827;font-size:14px}
        .d03-09-01-overlay{position:absolute;inset:0;background:rgba(15,23,42,.4);opacity:0;visibility:hidden;transition:opacity .3s ease,visibility .3s ease;z-index:1}
        .d03-09-01-stage.open .d03-09-01-overlay{opacity:1;visibility:visible}
        .d03-09-01-drawer{position:absolute;top:0;left:0;bottom:0;width:66%;max-width:210px;background:#fff;box-shadow:4px 0 16px rgba(0,0,0,.12);display:flex;flex-direction:column;padding:66px 20px 20px;gap:4px;transform:translateX(-105%);transition:transform .35s cubic-bezier(.4,0,.2,1);z-index:2}
        .d03-09-01-stage.open .d03-09-01-drawer{transform:translateX(0)}
        .d03-09-01-drawer a{color:#1f2937;text-decoration:none;font-size:13px;font-weight:600;padding:10px 4px;border-bottom:1px solid #e5e7eb}
        .d03-09-01-drawer a.active{color:#2563eb}
        .d03-09-01-drawer a:hover{color:#2563eb}

JavaScript

(function(){
        var stage=document.querySelector('.d03-09-01-stage');
        if(!stage)return;
        stage.querySelector('.d03-09-01-btn').addEventListener('click',function(){
          stage.classList.toggle('open');
        });
        stage.querySelector('.d03-09-01-overlay').addEventListener('click',function(){
          stage.classList.remove('open');
        });
      })();

クラス名 .d03-09-01 は本ライブラリ内での識別用です。複数のパーツを同じページで使う場合は、重複しないよう名前を変更してください。