作者:dsgfg | 来源:互联网 | 2024-12-27 19:42
本项目展示了如何通过HTML、CSS和Javascript创建一个具有动态效果的导航栏。我们将详细介绍每部分的代码及其功能。
一、HTML结构
二、CSS样式
body {
margin: 0;
padding: 0;
background-color: #f1f1f1;
}
#navigation ul {
list-style-type: none;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
margin-top: 150px;
}
#navigation li {
margin: 0 10px;
}
#navigation a {
text-decoration: none;
color: #313131;
font-size: 50px;
font-weight: bold;
font-family: sans-serif;
transition: all 0.7s ease;
padding: 14px 20px;
text-transform: uppercase;
}
#navigation a:hover {
font-size: 70px;
}
三、Javascript交互
window.addEventListener('DOMContentLoaded', (event) => {
const navLinks = document.querySelectorAll('#navigation a');
navLinks.forEach((link, index) => {
link.addEventListener('mouseover', () => {
switch (index) {
case 0:
link.style.color = 'red';
break;
case 1:
link.style.color = 'orange';
break;
case 2:
link.style.color = 'yellow';
break;
case 3:
link.style.color = 'green';
break;
default:
link.style.color = 'blue';
}
});
});
});
当鼠标悬停在导航链接上时,链接会变色并放大,以增强用户体验。以下是效果图: