本文最后更新于 2025-05-14T22:24:26+08:00
在单个静态HTML页面中通过侧边栏实现页面切换
在我们平时的开发中,在单个页面中实现侧边栏点击切换主页面部分内容一般会使用Vue来实现,但在部分特殊情况下,我们会需要使用静态页面,此时大部分项目的操作都是创建多个页面来跳转,不过我在此处实现了一种基于原生静态HTML页面实现的一种方案。
HTML页面代码(index.html):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <link rel="stylesheet" href="container.css" /> <link rel="stylesheet" href="sidebar.css" /> <link rel="stylesheet" href="content.css" /> <title>Cipherlink Nexus</title> <style> .page { display: none !important; }
.page.active { display: block !important; } </style> <script> function initializeRouter() { const routes = ['home', 'server', 'client', 'about'];
function updateActiveState(hash) { const validHash = routes.includes(hash) ? hash : 'home';
document.querySelectorAll('.page').forEach(page => { page.classList.remove('active'); }); const activePage = document.getElementById(validHash.charAt(0).toUpperCase() + validHash.slice(1)); if (activePage) activePage.classList.add('active');
document.querySelectorAll('.sidebar a').forEach(link => { link.classList.remove('active'); if (link.getAttribute('href') === `#${validHash}`) { link.classList.add('active'); } }); }
window.addEventListener('hashchange', () => { const hash = window.location.hash.substring(1).toLowerCase(); updateActiveState(hash); });
const initialHash = window.location.hash.substring(1).toLowerCase(); updateActiveState(initialHash || 'home'); }
window.addEventListener('DOMContentLoaded', initializeRouter); </script> </head>
<body> <div id="container" class="container"> <div class="sidebar"> <a href="#home">Home</a> <a href="#server">Server</a> <a href="#client">Client</a> <a href="#about">About</a> </div>
<div id="content" class="content"> <div id="Home" class="page"> <div id="content" class="content"> <h2>Welcome to home page</h2> <p>description</p> </div> </div>
<div id="Server" class="page"> <h2>Server Configuration</h2> <p>Server management interface</p> </div>
<div id="Client" class="page"> <h2>Client Setup</h2> <p>Client configuration details</p> </div>
<div id="About" class="page"> <h2>About Our Project</h2> <p>2025</p> </div> </div> </div> </body>
</html>
|
容器样式(container.css):
1 2 3 4 5 6 7 8
| body { background-color: aliceblue; }
#container { display: flex; min-height: 100vh; }
|
侧边栏样式(sidebar.css):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| .sidebar { margin: 0; padding: 0; width: 200px; background-color: #f1f1f1; position: fixed; height: 100%; overflow: auto; }
.sidebar a { display: block; color: black; padding: 16px; text-decoration: none; }
.sidebar a.active { background-color: #4CAF50; color: white; }
.sidebar a:hover:not(.active) { background-color: #555; color: white; }
div.content { margin-left: 200px; padding: 20px; min-height: 100vh; }
|
将以上代码复制或替换到项目中需要的位置即可,可根据需要调整CSS样式与JavaScript代码。