Files
ClassContent/历届学生/font-end-nine/项目练习/学生项目/杨锐/js联习/页面滚动.html
2024-09-27 02:06:13 +08:00

49 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>
body {
height: 30000px;
}
.header {
width: 100%;
height: 100px;
background: red;
}
.fixed {
position: fixed;
width: 100%;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="header"></div>
</body>
<script>
var header = $(".header");
//获取class为header的元素
$("body").onscroll = function () {
//定义页面的滚动事件
if (document.documentElement.scrollTop > 400) {
//如果页面滚动的高度大于400时
header.className = "header fixed"
//给元素定位通过css属性进行添加
}else{
header.className = "header"
}
}
function $(className) {
return document.querySelector(className)
}
</script>
</html>