我正在努力制作一种我已经适应的菜单,以便在移动设备上使用汉堡菜单.

以下是原始的HTML、CSS(它使用TailWind)和JavaScript:

  document.addEventListener('DOMContentLoaded', function() {
    const menuToggle = document.getElementById('menu-toggle');
    const menu = document.querySelector('.menu');

    menuToggle.addEventListener('click', function() {
      menu.classList.toggle('show');
    });
  });
  </body>
.mega-menu {
    display: none;
    left: 0;
    position: absolute;
    text-align: left;
    width: 100%;
}

/* #hoverable Class Styles */
.hoverable {
    position: static;
}

.hoverable > a:after {
    content: "\25BC";
    font-size: 10px;
    padding-left: 6px;
    position: relative;
    top: -1px;
}

.hoverable:hover .mega-menu {
    display: block;
}

/* #toggle Class Styles */
.toggleable > label:after {
    content: "\25BC";
    font-size: 10px;
    padding-left: 6px;
    position: relative;
    top: -1px;
}

.toggle-input {
    display: none;
}

.toggle-input:not(checked) ~ .mega-menu {
    display: none;
}

.toggle-input:checked ~ .mega-menu {
    display: block;
}

.toggle-input:checked + label {
    color: white;
    background: rgb(3, 102, 114);

    /* @apply bg-teal-700 */
}

.toggle-input:checked ~ label:after {
    content: "\25B2";
    font-size: 10px;
    padding-left: 6px;
    position: relative;
    top: -1px;
}

/* Responsive styles */
@media (max-width: 1023px) {
    .menu {
        display: none;
        flex-direction: column;
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        background-color: white;
    }

    .menu.show {
        display: flex;
    }

    .menu > ul > li {
        width: 100%;
    }

    .menu > ul > li > a {
        display: block;
    }

    .mega-menu {
        position: static;
        width: 100%;
    }
}

.mobile-menu {
    display: none;
}

@media (max-width: 1023px) {
    .mobile-menu {
        display: flex;
        flex-direction: column;
        background-color: white;
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        z-index: 10;
        padding: 1rem;
    }
}
<head>
        <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/1.1.2/tailwind.min.css'>

</head>
<body>
<nav class="navbar relative bg-white border-b-2 border-gray-300 text-gray-900" style="z-index: 100;">
            <div class="container mx-auto flex justify-between">
                <div class="relative block p-4 lg:p-6 text-xl text-teal-600 font-bold">
                    <a href="#" class="hover:text-teal-900">Logo</a>
                </div>
                <button class="lg:hidden text-teal-600 hover:text-teal-900 focus:outline-none focus:text-teal-900" id="menu-toggle" aria-label="Menu Toggle">
                    <svg class="h-6 w-6 fill-current" viewBox="0 0 24 24">
                        <path d="M4 5h16a1 1 0 0 1 0 2H4a1 1 0 1 1 0-2zm0 6h16a1 1 0 0 1 0 2H4a1 1 0 0 1 0-2zm0 6h16a1 1 0 0 1 0 2H4a1 1 0 0 1 0-2z"/>
                    </svg>
                </button>
                <div class="menu hidden lg:flex">
                    <ul class="flex">
                        <!--Regular Link-->
                        <li class="hover:bg-teal-700 hover:text-white">
                            <a href="#" class="relative block py-6 px-2 lg:p-6 text-sm lg:text-base font-bold">navlink</a>
                        </li>
                        <!--Hoverable Link-->
                        <li class="hoverable hover:bg-teal-700 hover:text-white">
                            <a href="#" class="relative block py-6 px-4 lg:p-6 text-sm lg:text-base font-bold hover:bg-teal-700 hover:text-white">Hover to show</a>
                            <div class="p-6 mega-menu mb-16 sm:mb-0 shadow-xl bg-teal-700">
                                <div class="container mx-auto w-full flex flex-wrap justify-between mx-2">
                                    <div class="w-full text-white mb-8 text-center">
                                        <h2 class="font-bold text-2xl">Listen, why don’t you wait out by the speeder.</h2>
                                        <p>our droids. They’ll have to wait outside. We don’t want them here.</p>
                                    </div>
                                    <ul class="px-4 w-full sm:w-1/2 lg:w-1/4 border-gray-600 border-b sm:border-r lg:border-b-0 pb-6 pt-6 lg:pt-3">
                                        <div class="flex items-center">
                                            <svg class="h-8 mb-3 mr-3 fill-current text-white" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M3 6c0-1.1.9-2 2-2h8l4-4h2v16h-2l-4-4H5a2 2 0 0 1-2-2H1V6h2zm8 9v5H8l-1.67-5H5v-2h8v2h-2z"/>
                                            </svg>
                                            <h3 class="font-bold text-xl text-white text-bold mb-2">Tatooine</h3>
                                        </div>
                                        <p class="text-gray-100 text-sm">Thul klivian doldur thisspiasian calrissian. Garindan d8 aurra twi'lek tund polis gen'dai sola tarpals.</p>
                                        <div class="flex items-center py-3">
                                            <svg class="h-6 pr-3 fill-current text-teal-300" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M20 10a10 10 0 1 1-20 0 10 10 0 0 1 20 0zm-2 0a8 8 0 1 0-16 0 8 8 0 0 0 16 0zm-8 2H5V8h5V5l5 5-5 5v-3z"/>
                                            </svg>
                                            <a href="#" class="text-white bold border-b-2 border-teal-300 hover:text-teal-900">Find out more...</a>
                                        </div>
                                    </ul>
                                    <ul class="px-4 w-full sm:w-1/2 lg:w-1/4 border-gray-600 border-b sm:border-r-0 lg:border-r lg:border-b-0 pb-6 pt-6 lg:pt-3">
                                        <div class="flex items-center">
                                            <svg class="h-8 mb-3 mr-3 fill-current text-white" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M4.13 12H4a2 2 0 1 0 1.8 1.11L7.86 10a2.03 2.03 0 0 0 .65-.07l1.55 1.55a2 2 0 1 0 3.72-.37L15.87 8H16a2 2 0 1 0-1.8-1.11L12.14 10a2.03 2.03 0 0 0-.65.07L9.93 8.52a2 2 0 1 0-3.72.37L4.13 12zM0 4c0-1.1.9-2 2-2h16a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V4z"/>
                                            </svg>
                                            <h3 class="font-bold text-xl text-white text-bold mb-2">Cantonica</h3>
                                        </div>
                                        <p class="text-gray-100 text-sm">Thul klivian doldur thisspiasian calrissian. Garindan d8 aurra twi'lek tund polis gen'dai sola tarpals.</p>
                                        <div class="flex items-center py-3">
                                            <svg class="h-6 pr-3 fill-current text-teal-300" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M20 10a10 10 0 1 1-20 0 10 10 0 0 1 20 0zm-2 0a8 8 0 1 0-16 0 8 8 0 0 0 16 0zm-8 2H5V8h5V5l5 5-5 5v-3z"/>
                                            </svg>
                                            <a href="#" class="text-white bold border-b-2 border-teal-300 hover:text-teal-900">Find out more...</a>
                                        </div>
                                    </ul>
                                    <ul class="px-4 w-full sm:w-1/2 lg:w-1/4 border-gray-600 border-b sm:border-b-0 sm:border-r md:border-b-0 pb-6 pt-6 lg:pt-3">
                                        <div class="flex items-center">
                                            <svg class="h-8 mb-3 mr-3 fill-current text-white" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M2 4v14h14v-6l2-2v10H0V2h10L8 4H2zm10.3-.3l4 4L8 16H4v-4l8.3-8.3zm1.4-1.4L16 0l4 4-2.3 2.3-4-4z"/>
                                            </svg>
                                            <h3 class="font-bold text-xl text-white text-bold mb-2">Yavin 4</h3>
                                        </div>
                                        <p class="text-gray-100 text-sm">Thul klivian doldur thisspiasian calrissian. Garindan d8 aurra twi'lek tund polis gen'dai sola tarpals.</p>
                                        <div class="flex items-center py-3">
                                            <svg class="h-6 pr-3 fill-current text-teal-300" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M20 10a10 10 0 1 1-20 0 10 10 0 0 1 20 0zm-2 0a8 8 0 1 0-16 0 8 8 0 0 0 16 0zm-8 2H5V8h5V5l5 5-5 5v-3z"/>
                                            </svg>
                                            <a href="#" class="text-white bold border-b-2 border-teal-300 hover:text-teal-900">Find out more...</a>
                                        </div>
                                    </ul>
                                    <ul class="px-4 w-full sm:w-1/2 lg:w-1/4 border-gray-600 pb-6 pt-6 lg:pt-3">
                                        <div class="flex items-center">
                                            <svg class="h-8 mb-3 mr-3 fill-current text-white" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M9 12H1v6a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-6h-8v2H9v-2zm0-1H0V5c0-1.1.9-2 2-2h4V2a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v1h4a2 2 0 0 1 2 2v6h-9V9H9v2zm3-8V2H8v1h4z"/>
                                            </svg>
                                            <h3 class="font-bold text-xl text-white text-bold mb-2">Alderaan</h3>
                                        </div>
                                        <p class="text-gray-100 text-sm">Thul klivian doldur thisspiasian calrissian. Garindan d8 aurra twi'lek tund polis gen'dai sola tarpals.</p>
                                        <div class="flex items-center py-3">
                                            <svg class="h-6 pr-3 fill-current text-teal-300" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M20 10a10 10 0 1 1-20 0 10 10 0 0 1 20 0zm-2 0a8 8 0 1 0-16 0 8 8 0 0 0 16 0zm-8 2H5V8h5V5l5 5-5 5v-3z"/>
                                            </svg>
                                            <a href="#" class="text-white bold border-b-2 border-teal-300 hover:text-teal-900">Find out more...</a>
                                        </div>
                                    </ul>
                                </div>
                            </div>
                        </li>
                        <!--end Hoverable Link-->
                    </ul>
                </div>
            </div>
        </nav>

我正在为如何让手机版更整洁而苦苦挣扎.理想情况下,我希望菜单项垂直堆叠,而不是彼此相邻,整个菜单飞出而不是下拉.

任何帮助都将不胜感激.

推荐答案

您可以从链接的<ul>个支架上的显示挠性开始.在TailWind中,你可以使用md:(例如)来制作响应式的风格.

<head>
        <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/1.1.2/tailwind.min.css'>

</head>
<body>
<nav class="navbar relative bg-white border-b-2 border-gray-300 text-gray-900" style="z-index: 100;">
            <div class="container mx-auto flex justify-between">
                <div class="relative block p-4 lg:p-6 text-xl text-teal-600 font-bold">
                    <a href="#" class="hover:text-teal-900">Logo</a>
                </div>
                <button class="lg:hidden text-teal-600 hover:text-teal-900 focus:outline-none focus:text-teal-900" id="menu-toggle" aria-label="Menu Toggle">
                    <svg class="h-6 w-6 fill-current" viewBox="0 0 24 24">
                        <path d="M4 5h16a1 1 0 0 1 0 2H4a1 1 0 1 1 0-2zm0 6h16a1 1 0 0 1 0 2H4a1 1 0 0 1 0-2zm0 6h16a1 1 0 0 1 0 2H4a1 1 0 0 1 0-2z"/>
                    </svg>
                </button>
                <div class="menu hidden lg:flex">
                    <ul class="md:flex">
                        <!--Regular Link-->
                        <li class="hover:bg-teal-700 hover:text-white">
                            <a href="#" class="relative block py-6 px-2 lg:p-6 text-sm lg:text-base font-bold">navlink</a>
                        </li>
                        <!--Hoverable Link-->
                        <li class="hoverable hover:bg-teal-700 hover:text-white">
                            <a href="#" class="relative block py-6 px-4 lg:p-6 text-sm lg:text-base font-bold hover:bg-teal-700 hover:text-white">Hover to show</a>
                            <div class="p-6 mega-menu mb-16 sm:mb-0 shadow-xl bg-teal-700">
                                <div class="container mx-auto w-full flex flex-wrap justify-between mx-2">
                                    <div class="w-full text-white mb-8 text-center">
                                        <h2 class="font-bold text-2xl">Listen, why don’t you wait out by the speeder.</h2>
                                        <p>our droids. They’ll have to wait outside. We don’t want them here.</p>
                                    </div>
                                    <ul class="px-4 w-full sm:w-1/2 lg:w-1/4 border-gray-600 border-b sm:border-r lg:border-b-0 pb-6 pt-6 lg:pt-3">
                                        <div class="flex items-center">
                                            <svg class="h-8 mb-3 mr-3 fill-current text-white" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M3 6c0-1.1.9-2 2-2h8l4-4h2v16h-2l-4-4H5a2 2 0 0 1-2-2H1V6h2zm8 9v5H8l-1.67-5H5v-2h8v2h-2z"/>
                                            </svg>
                                            <h3 class="font-bold text-xl text-white text-bold mb-2">Tatooine</h3>
                                        </div>
                                        <p class="text-gray-100 text-sm">Thul klivian doldur thisspiasian calrissian. Garindan d8 aurra twi'lek tund polis gen'dai sola tarpals.</p>
                                        <div class="flex items-center py-3">
                                            <svg class="h-6 pr-3 fill-current text-teal-300" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M20 10a10 10 0 1 1-20 0 10 10 0 0 1 20 0zm-2 0a8 8 0 1 0-16 0 8 8 0 0 0 16 0zm-8 2H5V8h5V5l5 5-5 5v-3z"/>
                                            </svg>
                                            <a href="#" class="text-white bold border-b-2 border-teal-300 hover:text-teal-900">Find out more...</a>
                                        </div>
                                    </ul>
                                    <ul class="px-4 w-full sm:w-1/2 lg:w-1/4 border-gray-600 border-b sm:border-r-0 lg:border-r lg:border-b-0 pb-6 pt-6 lg:pt-3">
                                        <div class="flex items-center">
                                            <svg class="h-8 mb-3 mr-3 fill-current text-white" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M4.13 12H4a2 2 0 1 0 1.8 1.11L7.86 10a2.03 2.03 0 0 0 .65-.07l1.55 1.55a2 2 0 1 0 3.72-.37L15.87 8H16a2 2 0 1 0-1.8-1.11L12.14 10a2.03 2.03 0 0 0-.65.07L9.93 8.52a2 2 0 1 0-3.72.37L4.13 12zM0 4c0-1.1.9-2 2-2h16a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V4z"/>
                                            </svg>
                                            <h3 class="font-bold text-xl text-white text-bold mb-2">Cantonica</h3>
                                        </div>
                                        <p class="text-gray-100 text-sm">Thul klivian doldur thisspiasian calrissian. Garindan d8 aurra twi'lek tund polis gen'dai sola tarpals.</p>
                                        <div class="flex items-center py-3">
                                            <svg class="h-6 pr-3 fill-current text-teal-300" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M20 10a10 10 0 1 1-20 0 10 10 0 0 1 20 0zm-2 0a8 8 0 1 0-16 0 8 8 0 0 0 16 0zm-8 2H5V8h5V5l5 5-5 5v-3z"/>
                                            </svg>
                                            <a href="#" class="text-white bold border-b-2 border-teal-300 hover:text-teal-900">Find out more...</a>
                                        </div>
                                    </ul>
                                    <ul class="px-4 w-full sm:w-1/2 lg:w-1/4 border-gray-600 border-b sm:border-b-0 sm:border-r md:border-b-0 pb-6 pt-6 lg:pt-3">
                                        <div class="flex items-center">
                                            <svg class="h-8 mb-3 mr-3 fill-current text-white" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M2 4v14h14v-6l2-2v10H0V2h10L8 4H2zm10.3-.3l4 4L8 16H4v-4l8.3-8.3zm1.4-1.4L16 0l4 4-2.3 2.3-4-4z"/>
                                            </svg>
                                            <h3 class="font-bold text-xl text-white text-bold mb-2">Yavin 4</h3>
                                        </div>
                                        <p class="text-gray-100 text-sm">Thul klivian doldur thisspiasian calrissian. Garindan d8 aurra twi'lek tund polis gen'dai sola tarpals.</p>
                                        <div class="flex items-center py-3">
                                            <svg class="h-6 pr-3 fill-current text-teal-300" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M20 10a10 10 0 1 1-20 0 10 10 0 0 1 20 0zm-2 0a8 8 0 1 0-16 0 8 8 0 0 0 16 0zm-8 2H5V8h5V5l5 5-5 5v-3z"/>
                                            </svg>
                                            <a href="#" class="text-white bold border-b-2 border-teal-300 hover:text-teal-900">Find out more...</a>
                                        </div>
                                    </ul>
                                    <ul class="px-4 w-full sm:w-1/2 lg:w-1/4 border-gray-600 pb-6 pt-6 lg:pt-3">
                                        <div class="flex items-center">
                                            <svg class="h-8 mb-3 mr-3 fill-current text-white" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M9 12H1v6a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-6h-8v2H9v-2zm0-1H0V5c0-1.1.9-2 2-2h4V2a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v1h4a2 2 0 0 1 2 2v6h-9V9H9v2zm3-8V2H8v1h4z"/>
                                            </svg>
                                            <h3 class="font-bold text-xl text-white text-bold mb-2">Alderaan</h3>
                                        </div>
                                        <p class="text-gray-100 text-sm">Thul klivian doldur thisspiasian calrissian. Garindan d8 aurra twi'lek tund polis gen'dai sola tarpals.</p>
                                        <div class="flex items-center py-3">
                                            <svg class="h-6 pr-3 fill-current text-teal-300" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                                                <path d="M20 10a10 10 0 1 1-20 0 10 10 0 0 1 20 0zm-2 0a8 8 0 1 0-16 0 8 8 0 0 0 16 0zm-8 2H5V8h5V5l5 5-5 5v-3z"/>
                                            </svg>
                                            <a href="#" class="text-white bold border-b-2 border-teal-300 hover:text-teal-900">Find out more...</a>
                                        </div>
                                    </ul>
                                </div>
                            </div>
                        </li>
                        <!--end Hoverable Link-->
                    </ul>
                </div>
            </div>
        </nav>

点击此处查看更多信息:https://tailwindcss.com/docs/responsive-design

Javascript相关问答推荐

如何使用侧边滚动按钮具体滚动每4个格?

jQuery提交按钮重新加载页面,即使在WordPress中使用preventDefault()

我应该绑定不影响状态的函数吗?'

在Vite React库中添加子模块路径

单击ImageListItemBar的IconButton后,在Material—UI对话框中显示图像

为什么我的导航条打开状态没有在窗口addeventlistener(Reaction Js)中更新?

如何在ASP.NET JavaScript中使用Google Charts API仅对绘制为负方向的条形图移动堆叠条形图标签位置

在HTML语言中调用外部JavaScript文件中的函数

以Angular 实现ng-Circle-Progress时出错:模块没有导出的成员

在使用位板时,如何在Java脚本中判断Connect 4板中中柱的对称性?

使用Nuxt Apollo在Piniastore 中获取产品细节

Rxjs流中生成IMMER不能在对象上操作

将异步回调转换为异步生成器模式

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

如何在和IF语句中使用||和&;&;?

在Odoo中如何以编程方式在POS中添加产品

无法设置RazorPay订阅API项目价格

如何使用抽屉屏幕及其子屏幕/组件的上下文?

如何使用Cypress在IFRAME中打字

JSON Web令牌(JWT)错误:RSA密钥对的签名无效