Files
ClassContent/历届学生/front-end-team-10/项目练习/学生项目/薛涛/2.24/滚动页面.html
2024-09-27 02:06:13 +08:00

64 lines
1.1 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
height: 3000px;
}
.header {
width: 100%;
height: 60px;
background: #ff0;
/* position: static; */
}
.test {
width: 100%;
height: 60px;
}
</style>
</head>
<body>
<div class="test"></div>
<div class="header"></div>
</body>
<script>
var body = $("body"),
header = $(".header")
body.onscroll = () => {
var offsetTop = document.documentElement.scrollTop
if (offsetTop > 60) {
header.style.position = "fixed"
header.style.top = "0"
} else {
header.style.position = "static"
}
}
function $(className) {
return document.querySelector(className)
}
</script>
</html>