能用导航菜单 单弹 双弹 缩小显手机端 原生css js
单弹 例1
代码1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>现代响应式导航栏</title>
<style>
:root {
--primary-color: #2196F3;
--text-color: #ffffff;
--bg-color: #1a1a1a;
--hover-color: #1976D2;
--transition: all 0.3s ease;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
margin: 0;
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
line-height: 1.6;
background-color: #f5f5f5;
}
.navbar {
display: flex;
align-items: center;
background-color: var(--bg-color);
padding: 1rem 5%;
position: sticky;
top: 0;
z-index: 1000;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.navbar-brand {
color: var(--text-color);
font-size: 1.5rem;
font-weight: 600;
text-decoration: none;
display: flex;
align-items: center;
transition: var(--transition);
}
.navbar-brand:hover {
color: var(--primary-color);
}
.navbar-menu {
list-style: none;
display: flex;
margin: 0;
padding: 0;
align-items: center;
}
.navbar-item {
margin-left: 2rem;
position: relative;
}
.navbar-link {
color: var(--text-color);
text-decoration: none;
font-size: 1rem;
padding: 0.5rem 0;
transition: var(--transition);
position: relative;
}
.navbar-link::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 0;
height: 2px;
background-color: var(--primary-color);
transition: var(--transition);
}
.navbar-link:hover {
color: var(--primary-color);
}
.navbar-link:hover::after {
width: 100%;
}
.navbar-toggle {
display: none;
flex-direction: column;
cursor: pointer;
padding: 0.5rem;
z-index: 100;
}
.bar {
width: 25px;
height: 2px;
background-color: var(--text-color);
margin: 3px 0;
transition: var(--transition);
border-radius: 2px;
}
/* 搜索框样式 */
.search-container {
display: flex;
align-items: center;
margin-left: 2rem;
}
.search-input {
padding: 0.5rem 1rem;
border: none;
border-radius: 20px;
background-color: rgba(255, 255, 255, 0.1);
color: var(--text-color);
font-size: 0.9rem;
transition: var(--transition);
width: 200px;
}
.search-input::placeholder {
color: rgba(255, 255, 255, 0.6);
}
.search-input:focus {
outline: none;
background-color: rgba(255, 255, 255, 0.2);
width: 250px;
}
/* 修改弹出菜单样式 */
.dropdown {
position: relative;
}
.dropdown-menu {
position: absolute;
top: calc(100% + 10px); /* 增加一点间距 */
left: 50%;
transform: translateX(-50%) translateY(10px);
background-color: var(--bg-color);
min-width: 200px;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
opacity: 0;
visibility: hidden;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
z-index: 1000;
padding: 0.5rem 0;
border: 1px solid rgba(255, 255, 255, 0.1);
list-style: none;
}
/* 添加小三角形 */
.dropdown-menu::before {
content: '';
position: absolute;
top: -5px;
left: 50%;
transform: translateX(-50%) rotate(45deg);
width: 10px;
height: 10px;
background-color: var(--bg-color);
border-left: 1px solid rgba(255, 255, 255, 0.1);
border-top: 1px solid rgba(255, 255, 255, 0.1);
}
.dropdown:hover .dropdown-menu {
opacity: 1;
visibility: visible;
transform: translateX(-50%) translateY(0);
}
.dropdown-item {
display: flex;
align-items: center;
padding: 0.75rem 1.5rem;
color: var(--text-color);
text-decoration: none;
transition: all 0.2s ease;
font-size: 0.95rem;
white-space: nowrap;
}
.dropdown-item:hover {
background-color: rgba(255, 255, 255, 0.1);
color: var(--primary-color);
}
/* 修改箭头指示器 */
.dropdown .navbar-link {
display: flex;
align-items: center;
gap: 4px;
}
.dropdown .navbar-link::after {
content: '▾';
font-size: 12px;
transition: transform 0.3s ease;
position: static;
background: none;
width: auto;
height: auto;
margin-top: 2px;
}
.dropdown:hover .navbar-link::after {
transform: rotate(180deg);
}
/* 移动端样式优化 */
@media (max-width: 768px) {
.navbar {
padding: 1rem;
justify-content: space-between;
}
.navbar-menu {
display: none;
flex-direction: column; /* 垂直排列 */
position: absolute;
top: 100%;
left: 0;
right: 0;
background-color: var(--bg-color);
padding: 1rem 0;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
opacity: 0;
transform: translateY(-10px);
transition: var(--transition);
}
.navbar-item {
margin: 0;
width: 100%;
text-align: left;
padding: 0 1rem;
}
.navbar-link {
padding: 1rem 0;
display: block;
width: 100%;
}
.navbar-toggle {
display: flex;
}
.navbar-menu.active {
display: flex;
opacity: 1;
transform: translateY(0);
}
/* 汉堡菜单动画 */
.navbar-toggle.active .bar:nth-child(1) {
transform: rotate(45deg) translate(5px, 5px);
}
.navbar-toggle.active .bar:nth-child(2) {
opacity: 0;
}
.navbar-toggle.active .bar:nth-child(3) {
transform: rotate(-45deg) translate(5px, -5px);
}
/* 移动端下拉菜单样式优化 */
.dropdown-menu {
position: static;
transform: none;
background-color: transparent;
box-shadow: none;
opacity: 1;
visibility: visible;
display: none;
margin: 0;
padding: 0 0 0 1rem;
border: none;
border-radius: 0;
border-left: 2px solid rgba(255, 255, 255, 0.1);
min-width: auto;
width: 100%;
}
.dropdown-menu::before {
display: none;
}
.dropdown.active .dropdown-menu {
display: block;
animation: slideDown 0.3s ease forwards;
}
@keyframes slideDown {
from {
opacity: 0;
transform: translateY(-10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.dropdown-item {
padding: 0.75rem 1rem;
color: rgba(255, 255, 255, 0.8);
}
.dropdown-item:hover {
background-color: transparent;
color: var(--primary-color);
padding-left: 1.5rem;
}
/* 移动端箭头样式优化 */
.dropdown .navbar-link {
display: flex;
justify-content: space-between;
align-items: center;
padding-right: 1rem;
}
.dropdown .navbar-link::after {
margin: 0;
font-size: 1rem;
}
/* 移动端分隔线 */
.navbar-item:not(:last-child) {
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
/* 移动端搜索框位置调整 */
.navbar-item:last-child {
border-bottom: none;
padding: 1rem;
}
.search-container {
margin: 0;
width: 100%;
padding: 0;
}
.search-input {
width: 100%;
background-color: rgba(255, 255, 255, 0.1);
}
.search-input:focus {
width: 100%;
background-color: rgba(255, 255, 255, 0.15);
}
}
</style>
</head>
<body>
<nav class="navbar">
<a href="#" class="navbar-brand">Brand</a>
<div class="navbar-toggle" id="mobile-menu">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
<ul class="navbar-menu">
<li class="navbar-item"><a href="#" class="navbar-link">Home</a></li>
<li class="navbar-item dropdown">
<a href="#" class="navbar-link">Products</a>
<ul class="dropdown-menu">
<li><a href="#" class="dropdown-item">New Arrivals</a></li>
<li><a href="#" class="dropdown-item">Best Sellers</a></li>
<li><a href="#" class="dropdown-item">Categories</a></li>
<li><a href="#" class="dropdown-item">Special Offers</a></li>
</ul>
</li>
<li class="navbar-item"><a href="#" class="navbar-link">About</a></li>
<li class="navbar-item"><a href="#" class="navbar-link">Services</a></li>
<li class="navbar-item"><a href="#" class="navbar-link">Contact</a></li>
<li class="navbar-item">
<div class="search-container">
<input type="search" class="search-input" placeholder="Search...">
</div>
</li>
</ul>
</nav>
<script>
const mobileMenu = document.getElementById('mobile-menu');
const navbarMenu = document.querySelector('.navbar-menu');
// 移动端菜单切换
mobileMenu.addEventListener('click', () => {
mobileMenu.classList.toggle('active');
navbarMenu.classList.toggle('active');
});
// 优化移动端下拉菜单处理
const dropdowns = document.querySelectorAll('.dropdown');
dropdowns.forEach(dropdown => {
const link = dropdown.querySelector('.navbar-link');
const menu = dropdown.querySelector('.dropdown-menu');
link.addEventListener('click', (e) => {
if (window.innerWidth <= 768) {
e.preventDefault();
e.stopPropagation();
// 关闭其他打开的下拉菜单
dropdowns.forEach(otherDropdown => {
if (otherDropdown !== dropdown && otherDropdown.classList.contains('active')) {
otherDropdown.classList.remove('active');
otherDropdown.querySelector('.dropdown-menu').style.display = 'none';
}
});
// 切换当前下拉菜单
dropdown.classList.toggle('active');
if (dropdown.classList.contains('active')) {
menu.style.display = 'block';
} else {
menu.style.display = 'none';
}
}
});
});
// 点击导航链接时关闭菜单
document.querySelectorAll('.navbar-link:not(.dropdown .navbar-link)').forEach(link => {
link.addEventListener('click', () => {
mobileMenu.classList.remove('active');
navbarMenu.classList.remove('active');
});
});
// 点击外部时关闭菜单
document.addEventListener('click', (e) => {
if (!navbarMenu.contains(e.target) && !mobileMenu.contains(e.target)) {
mobileMenu.classList.remove('active');
navbarMenu.classList.remove('active');
dropdowns.forEach(dropdown => {
dropdown.classList.remove('active');
});
}
});
// 优化窗口大小改变时的处理
let resizeTimer;
window.addEventListener('resize', () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => {
if (window.innerWidth > 768) {
// 重置所有菜单状态
dropdowns.forEach(dropdown => {
dropdown.classList.remove('active');
const menu = dropdown.querySelector('.dropdown-menu');
menu.style.display = '';
});
mobileMenu.classList.remove('active');
navbarMenu.classList.remove('active');
}
}, 250);
});
</script>
</body>
</html>
双弹 例2
代码2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>现代响应式导航栏</title>
<style>
:root {
--primary-color: #1976D2;
--text-color: #2c3e50;
--bg-color: #ffffff;
--hover-color: #1565C0;
--border-color: #e0e0e0;
--dropdown-bg: #ffffff;
--dropdown-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
--transition: all 0.3s ease;
}
a {
text-decoration: none;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
margin: 0;
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
line-height: 1.6;
background-color: #f5f5f5;
}
.navbar {
display: flex;
align-items: center;
background-color: var(--bg-color);
padding: 1rem 5%;
position: sticky;
top: 0;
z-index: 1000;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}
.navbar-brand {
color: var(--text-color);
font-size: 1.5rem;
font-weight: 600;
text-decoration: none;
display: flex;
align-items: center;
transition: var(--transition);
}
.navbar-brand:hover {
color: var(--primary-color);
}
.navbar-menu {
list-style: none;
display: flex;
margin: 0;
padding: 0;
align-items: center;
}
.navbar-item {
margin-left: 2rem;
position: relative;
}
.navbar-link {
color: var(--text-color);
text-decoration: none;
font-size: 1rem;
padding: 0.5rem 0;
transition: var(--transition);
position: relative;
}
.navbar-link::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 0;
height: 2px;
background-color: var(--primary-color);
transition: var(--transition);
}
.navbar-link:hover {
color: var(--primary-color);
}
.navbar-link:hover::after {
width: 100%;
}
.navbar-toggle {
display: none;
flex-direction: column;
cursor: pointer;
padding: 0.5rem;
z-index: 100;
}
.bar {
width: 25px;
height: 2px;
background-color: var(--text-color);
margin: 3px 0;
transition: var(--transition);
border-radius: 2px;
}
/* 搜索框样式 */
.search-container {
display: flex;
align-items: center;
margin-left: 2rem;
}
.search-input {
padding: 0.5rem 1rem;
border: 1px solid var(--border-color);
border-radius: 20px;
background-color: #f5f5f5;
color: var(--text-color);
font-size: 0.9rem;
transition: var(--transition);
width: 200px;
}
.search-input::placeholder {
color: #9e9e9e;
}
.search-input:focus {
outline: none;
background-color: #ffffff;
border-color: var(--primary-color);
box-shadow: 0 0 0 3px rgba(25, 118, 210, 0.1);
width: 250px;
}
/* 修改弹出菜单样式 */
.dropdown {
position: relative;
}
.dropdown-menu {
position: absolute;
top: calc(100% + 10px);
left: 50%;
transform: translateX(-50%);
background-color: var(--dropdown-bg);
border-radius: 8px;
box-shadow: var(--dropdown-shadow);
opacity: 0;
visibility: hidden;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
z-index: 1000;
border: 1px solid var(--border-color);
/* 两列布局样式 */
display: flex;
gap: 3rem;
min-width: 480px;
padding: 2rem;
}
/* 添加小三角形 */
.dropdown-menu::before {
content: '';
position: absolute;
top: -5px;
left: 50%;
transform: translateX(-50%) rotate(45deg);
width: 10px;
height: 10px;
background-color: var(--dropdown-bg);
border-left: 1px solid var(--border-color);
border-top: 1px solid var(--border-color);
}
.dropdown:hover .dropdown-menu {
opacity: 1;
visibility: visible;
transform: translateX(-50%) translateY(0);
}
.dropdown-column {
flex: 1;
min-width: 200px;
}
.dropdown-header {
color: var(--primary-color);
font-size: 0.95rem;
font-weight: 600;
text-transform: uppercase;
margin-bottom: 1rem;
padding: 0;
letter-spacing: 0.5px;
}
.dropdown-list {
list-style: none;
padding: 0;
margin: 0;
}
.dropdown-item {
display: block;
padding: 0.75rem 1rem;
margin: 0.25rem 0;
border-radius: 6px;
color: var(--text-color);
transition: all 0.2s ease;
font-size: 0.95rem;
}
.dropdown-item:hover {
background-color: #f5f7fa;
transform: translateX(5px);
color: var(--primary-color);
}
/* 修改箭头指示器 */
.dropdown .navbar-link {
display: flex;
align-items: center;
gap: 4px;
}
.dropdown .navbar-link::after {
content: '▾';
font-size: 12px;
transition: transform 0.3s ease;
position: static;
background: none;
width: auto;
height: auto;
margin-top: 2px;
}
.dropdown:hover .navbar-link::after {
transform: rotate(180deg);
}
/* 移动端样式优化 */
@media (max-width: 768px) {
.navbar {
padding: 1rem;
justify-content: space-between;
}
.navbar-menu {
display: none;
flex-direction: column; /* 垂直排列 */
position: absolute;
top: 100%;
left: 0;
right: 0;
background-color: var(--bg-color);
padding: 1rem 0;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
opacity: 0;
transform: translateY(-10px);
transition: var(--transition);
}
.navbar-item {
margin: 0;
width: 100%;
text-align: left;
padding: 0 1rem;
}
.navbar-link {
padding: 1rem 0;
display: block;
width: 100%;
}
.navbar-toggle {
display: flex;
}
.navbar-menu.active {
display: flex;
opacity: 1;
transform: translateY(0);
}
/* 汉堡菜单动画 */
.navbar-toggle.active .bar:nth-child(1) {
transform: rotate(45deg) translate(5px, 5px);
}
.navbar-toggle.active .bar:nth-child(2) {
opacity: 0;
}
.navbar-toggle.active .bar:nth-child(3) {
transform: rotate(-45deg) translate(5px, -5px);
}
/* 移动端下拉菜单样式优化 */
.dropdown-menu {
position: static;
transform: none;
opacity: 1;
visibility: visible;
display: none;
box-shadow: none;
border: none;
border-radius: 0;
min-width: 100%;
padding: 1rem;
margin: 0;
background-color: #f8f9fa;
border-left: 2px solid var(--primary-color);
flex-direction: column;
gap: 1.5rem;
}
.dropdown-menu::before {
display: none;
}
.dropdown.active .dropdown-menu {
display: flex;
animation: slideDown 0.3s ease forwards;
}
@keyframes slideDown {
from {
opacity: 0;
transform: translateY(-10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.dropdown-column {
padding: 0 1rem;
}
.dropdown-header {
font-size: 0.9rem;
margin-bottom: 0.75rem;
color: var(--text-color);
opacity: 0.7;
}
.dropdown-item {
padding: 0.75rem;
margin: 0.15rem 0;
border-radius: 4px;
}
.dropdown-item:hover {
transform: translateX(8px);
background-color: #f0f2f5;
}
/* 移动端箭头样式优化 */
.dropdown .navbar-link {
display: flex;
justify-content: space-between;
align-items: center;
padding-right: 1rem;
}
.dropdown .navbar-link::after {
margin: 0;
font-size: 1rem;
}
/* 移动端分隔线 */
.navbar-item:not(:last-child) {
border-bottom: 1px solid var(--border-color);
}
/* 移动端搜索框位置调整 */
.navbar-item:last-child {
border-bottom: none;
padding: 1rem;
}
.search-container {
margin: 0;
width: 100%;
padding: 0;
}
.search-input {
width: 100%;
background-color: #f5f5f5;
}
.search-input:focus {
width: 100%;
background-color: #ffffff;
}
.bar {
background-color: var(--text-color);
}
}
</style>
</head>
<body>
<nav class="navbar">
<a href="#" class="navbar-brand">Brand</a>
<div class="navbar-toggle" id="mobile-menu">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
<ul class="navbar-menu">
<li class="navbar-item"><a href="#" class="navbar-link">Home</a></li>
<li class="navbar-item dropdown">
<a href="#" class="navbar-link">Products</a>
<div class="dropdown-menu">
<div class="dropdown-column">
<h3 class="dropdown-header">New & Featured</h3>
<ul class="dropdown-list">
<li><a href="#" class="dropdown-item">New Arrivals</a></li>
<li><a href="#" class="dropdown-item">Best Sellers</a></li>
<li><a href="#" class="dropdown-item">Special Offers</a></li>
<li><a href="#" class="dropdown-item">Coming Soon</a></li>
</ul>
</div>
<div class="dropdown-column">
<h3 class="dropdown-header">Categories</h3>
<ul class="dropdown-list">
<li><a href="#" class="dropdown-item">Electronics</a></li>
<li><a href="#" class="dropdown-item">Clothing</a></li>
<li><a href="#" class="dropdown-item">Home & Garden</a></li>
<li><a href="#" class="dropdown-item">Sports</a></li>
</ul>
</div>
</div>
</li>
<li class="navbar-item"><a href="#" class="navbar-link">About</a></li>
<li class="navbar-item"><a href="#" class="navbar-link">Services</a></li>
<li class="navbar-item"><a href="#" class="navbar-link">Contact</a></li>
<li class="navbar-item">
<div class="search-container">
<input type="search" class="search-input" placeholder="Search...">
</div>
</li>
</ul>
</nav>
<script>
const mobileMenu = document.getElementById('mobile-menu');
const navbarMenu = document.querySelector('.navbar-menu');
// 移动端菜单切换
mobileMenu.addEventListener('click', () => {
mobileMenu.classList.toggle('active');
navbarMenu.classList.toggle('active');
});
// 优化移动端下拉菜单处理
const dropdowns = document.querySelectorAll('.dropdown');
dropdowns.forEach(dropdown => {
const link = dropdown.querySelector('.navbar-link');
const menu = dropdown.querySelector('.dropdown-menu');
link.addEventListener('click', (e) => {
if (window.innerWidth <= 768) {
e.preventDefault();
e.stopPropagation();
// 关闭其他打开的下拉菜单
dropdowns.forEach(otherDropdown => {
if (otherDropdown !== dropdown && otherDropdown.classList.contains('active')) {
otherDropdown.classList.remove('active');
otherDropdown.querySelector('.dropdown-menu').style.display = 'none';
}
});
// 切换当前下拉菜单
dropdown.classList.toggle('active');
if (dropdown.classList.contains('active')) {
menu.style.display = 'block';
} else {
menu.style.display = 'none';
}
}
});
});
// 点击导航链接时关闭菜单
document.querySelectorAll('.navbar-link:not(.dropdown .navbar-link)').forEach(link => {
link.addEventListener('click', () => {
mobileMenu.classList.remove('active');
navbarMenu.classList.remove('active');
});
});
// 点击外部时关闭菜单
document.addEventListener('click', (e) => {
if (!navbarMenu.contains(e.target) && !mobileMenu.contains(e.target)) {
mobileMenu.classList.remove('active');
navbarMenu.classList.remove('active');
dropdowns.forEach(dropdown => {
dropdown.classList.remove('active');
});
}
});
// 优化窗口大小改变时的处理
let resizeTimer;
window.addEventListener('resize', () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => {
if (window.innerWidth > 768) {
// 重置所有菜单状态
dropdowns.forEach(dropdown => {
dropdown.classList.remove('active');
const menu = dropdown.querySelector('.dropdown-menu');
menu.style.display = '';
});
mobileMenu.classList.remove('active');
navbarMenu.classList.remove('active');
}
}, 250);
});
</script>
</body>
</html>