如何将现有代码适配iPhone 14 Pro实现右侧汉堡下拉菜单?
适配iPhone 14 Pro的响应式汉堡菜单CSS实现
前提假设
先假定你的HTML结构如下(需配合此CSS使用):
<header class="mobile-header"> <a href="#" class="logo">Your Logo</a> <!-- 用原生checkbox实现无JS基础交互 --> <input type="checkbox" id="hamburger-toggle" class="hamburger-toggle"> <label for="hamburger-toggle" class="hamburger-btn" aria-label="Toggle navigation menu"> <span class="hamburger-line"></span> <span class="hamburger-line"></span> <span class="hamburger-line"></span> </label> <nav class="mobile-menu"> <a href="#" class="menu-logo">Your Logo</a> <a href="#" class="menu-item">Home</a> <a href="#" class="menu-item">About Us</a> <a href="#" class="menu-item">Gallery</a> <a href="#" class="menu-item">Contact</a> <a href="#" class="menu-action-btn">Contact Us</a> </nav> </header>
核心CSS代码
/* 适配iPhone 14 Pro的viewport设置 */ @viewport { width: device-width; initial-scale: 1.0; } * { margin: 0; padding: 0; box-sizing: border-box; font-family: system-ui, -apple-system, sans-serif; } /* 头部容器:左侧Logo+右侧汉堡按钮布局 */ .mobile-header { position: fixed; top: 0; left: 0; width: 100%; padding: 16px 24px; background-color: #fff; display: flex; align-items: center; justify-content: space-between; box-shadow: 0 2px 8px rgba(0,0,0,0.1); z-index: 100; } .logo { font-size: 20px; font-weight: 700; color: #2d3748; text-decoration: none; } /* 隐藏原生checkbox控件 */ .hamburger-toggle { display: none; } /* 汉堡按钮样式 */ .hamburger-btn { display: flex; flex-direction: column; gap: 4px; cursor: pointer; background: transparent; border: none; } .hamburger-line { width: 28px; height: 3px; background-color: #2d3748; border-radius: 2px; transition: all 0.3s ease; } /* 菜单默认隐藏在屏幕右侧 */ .mobile-menu { position: fixed; top: 0; right: -100%; width: 100%; height: 100vh; background-color: #fff; padding: 24px; display: flex; flex-direction: column; gap: 16px; transition: right 0.3s ease; z-index: 99; } /* checkbox选中时,菜单滑入屏幕 */ .hamburger-toggle:checked ~ .mobile-menu { right: 0; } /* 汉堡按钮展开后的动画效果 */ .hamburger-toggle:checked ~ .hamburger-btn .hamburger-line:nth-child(1) { transform: rotate(45deg) translate(5px, 5px); } .hamburger-toggle:checked ~ .hamburger-btn .hamburger-line:nth-child(2) { opacity: 0; } .hamburger-toggle:checked ~ .hamburger-btn .hamburger-line:nth-child(3) { transform: rotate(-45deg) translate(7px, -6px); } /* 菜单内部元素样式 */ .menu-logo { font-size: 24px; font-weight: 700; color: #2d3748; text-decoration: none; margin-bottom: 24px; } .menu-item { font-size: 18px; color: #4a5568; text-decoration: none; padding: 12px 0; border-bottom: 1px solid #e2e8f0; } .menu-action-btn { font-size: 16px; font-weight: 600; color: #fff; background-color: #2b6cb0; text-decoration: none; padding: 14px; border-radius: 8px; text-align: center; margin-top: 24px; } /* 针对iPhone 14 Pro的专属适配优化 */ @media screen and (width: 393px) and (height: 852px) { .mobile-header { padding: 18px 28px; } .mobile-menu { padding: 28px; } .menu-item { font-size: 19px; padding: 14px 0; } }
关键说明
- 依赖原生checkbox状态实现无JS的展开/收起交互,兼容性拉满
- 针对iPhone 14 Pro的393px宽度做了细节优化,匹配设备显示效果
- 汉堡按钮带有平滑旋转动画,提升交互质感
- 菜单展开后全屏覆盖,符合移动端导航的常规设计逻辑
内容的提问来源于stack exchange,提问作者Harun
相关产品推荐
相关产品推荐

