ハンバーガーメニュー|書きかけ

実際のコード

<style>
        body {
            margin: 0;
            padding: 20px;
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            overflow-x: hidden;
        }

        .menu-button {
            width: 40px;
            height: 30px;
            position: relative;
            cursor: pointer;
            background: none;
            border: none;
            padding: 10px;
            border-radius: 4px;
            transition: background-color 0.3s ease;
            z-index: 10001;
        }

        .menu-button:hover {
            background-color: rgba(0, 0, 0, 0.1);
        }

        .menu-button span {
            display: block;
            width: 100%;
            height: 3px;
            background-color: black;
            border-radius: 2px;
            position: absolute;
            left: 0;
            transition: all 0.3s ease;
        }

        .menu-button span:first-child {
            top: 8px;
        }

        .menu-button span:last-child {
            bottom: 8px;
        }

        /* ×状態のスタイル */
        .menu-button.active span:first-child {
            top: 50%;
            transform: translateY(-50%) rotate(45deg);
        }

        .menu-button.active span:last-child {
            bottom: 50%;
            transform: translateY(50%) rotate(-45deg);
        }

        /* オーバーレイメニュー */
        .menu-overlay {
            position: fixed;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100vh;
            background-color: rgba(0, 0, 0, 0.75);
            z-index: 10000;
            transition: left 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: flex-start;
            padding-left: 50px;
        }

        .menu-overlay.active {
            left: 0;
        }

        .menu-content {
            padding: 40px 30px;
            border-radius: 8px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
            max-width: 300px;
            width: 100%;
        }

        .menu-content ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .menu-content li {
            padding: 12px 0;
            border-bottom: 1px solid #eee;
        }

        .menu-content li:last-child {
            border-bottom: none;
        }

        .menu-content a {
            text-decoration: none;
            color: #333;
            font-size: 16px;
            transition: color 0.3s ease;
        }

        .menu-content a:hover {
            color: #007bff;
        }

        /* メインコンテンツ */
        .main-content {
            margin-top: 30px;
        }

        .main-content h1 {
            color: #333;
        }

        .main-content p {
            line-height: 1.6;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="menu-button" id="menuButton">
        <span></span>
        <span></span>
    </div>

    <div class="menu-overlay" id="menuOverlay">
        <div class="menu-content">
            <ul>
                <li><a href="#home">ホーム</a></li>
                <li><a href="#about">会社概要</a></li>
                <li><a href="#services">サービス</a></li>
                <li><a href="#portfolio">実績</a></li>
                <li><a href="#contact">お問い合わせ</a></li>
            </ul>
        </div>
    </div>

    <script>
        const menuButton = document.getElementById('menuButton');
        const menuOverlay = document.getElementById('menuOverlay');

        menuButton.addEventListener('click', function(e) {
            e.stopPropagation();
            this.classList.toggle('active');
            menuOverlay.classList.toggle('active');
        });

        // オーバーレイをクリックしたときにメニューを閉じる
        menuOverlay.addEventListener('click', function(e) {
            if (e.target === this) {
                closeMenu();
            }
        });

        // ESCキーでメニューを閉じる
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape' && menuOverlay.classList.contains('active')) {
                closeMenu();
            }
        });

        function closeMenu() {
            menuButton.classList.remove('active');
            menuOverlay.classList.remove('active');
        }
    </script>

スポンサーリンク

コメント

タイトルとURLをコピーしました