html+css+js 鼠标放在页面顶部则导航栏出现 鼠标移出则隐藏
这个功能可以通过HTML、CSS和JavaScript实现。以下是实现这个功能的示例代码:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Navigation Bar on Hover</title>
<style>
body {
margin: 0;
padding: 0;
}
.nav {
display: none;
position: fixed;
top: 0;
width: 100%;
background-color: #333;
color: white;
text-align: center;
padding: 10px 0;
}
.nav.active {
display: block;
}
</style>
</head>
<body>
<div class="nav">
<span>Navigation Link 1</span>
<span>Navigation Link 2</span>
<span>Navigation Link 3</span>
</div>
<script>
const nav = document.querySelector('.nav');
window.addEventListener('DOMContentLoaded', () => {
document.addEventListener('mouseover', (e) => {
if (e.target === e.currentTarget) {
nav.classList.add('active');
}
});
document.addEventListener('mouseout', (e) => {
if (e.target === e.currentTarget) {
nav.classList.remove('active');
}
});
});
</script>
</body>
</html>
在这个示例中,当鼠标悬停在页面body
上时,导航栏会显示,鼠标移出则导航栏隐藏。这是通过监听mouseover
和mouseout
事件实现的,并通过添加或移除一个CSS类来控制导航栏的显示与隐藏。
评论已关闭