以下是一个简单的 JavaScript 实现手机底部导航栏的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>底部导航栏</title>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
}
.bottom-nav {
display: flex;
position: fixed;
bottom: 0;
width: 100%;
background-color: #f8f8f8;
border-top: 1px solid #e7e7e7;
padding: 10px 0;
}
.nav-item {
flex: 1;
text-align: center;
}
.nav-item a {
text-decoration: none;
color: #666;
display: block;
padding: 10px;
}
.nav-item.active a {
color: #007bff;
}
</style>
</head>
<body>
<div class="bottom-nav">
<div class="nav-item active" onclick="activeNavItem(this)">
<a href="#home">首页</a>
</div>
<div class="nav-item" onclick="activeNavItem(this)">
<a href="#category">分类</a>
</div>
<div class="nav-item" onclick="activeNavItem(this)">
<a href="#cart">购物车</a>
</div>
<div class="nav-item" onclick="activeNavItem(this)">
<a href="#profile">我的</a>
</div>
</div>
<script>
function activeNavItem(item) {
const navItems = document.querySelectorAll('.nav-item');
navItems.forEach(navItem => {
navItem.classList.remove('active');
});
item.classList.add('active');
}
</script>
</body>
</html>
基础概念:这是一个使用 HTML、CSS 和 JavaScript 构建的简单底部导航栏。
优势:
类型:常见的底部导航栏类型包括固定式(如上述示例)、可滚动式等。
应用场景:适用于各种移动端网页和应用,如电商网站、社交应用、新闻阅读等。
常见问题及解决方法:
href
属性值正确,并且页面中有相应的锚点或路由设置。领取专属 10元无门槛券
手把手带您无忧上云