128 lines
3.0 KiB
HTML
128 lines
3.0 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>淘宝头部定位2</title>
|
||
<style>
|
||
* {
|
||
margin: 0;
|
||
padding: 0;
|
||
list-style: none;
|
||
outline: none;
|
||
}
|
||
|
||
body {
|
||
height: 3000px;
|
||
background: rosybrown;
|
||
}
|
||
|
||
.header {
|
||
width: 100%;
|
||
height: 60px;
|
||
display: flex;
|
||
justify-content: center;
|
||
}
|
||
|
||
.header ul {
|
||
height: 100%;
|
||
display: flex;
|
||
margin-left: 10px;
|
||
}
|
||
|
||
.header li {
|
||
display: flex;
|
||
align-items: center;
|
||
}
|
||
|
||
.header input {
|
||
width: 600px;
|
||
height: 100%;
|
||
border-top-left-radius: 10px;
|
||
border-bottom-left-radius: 10px;
|
||
border: 1px solid #39bc30;
|
||
box-sizing: border-box;
|
||
background: white;
|
||
padding-left: 42px;
|
||
}
|
||
|
||
.header .btn {
|
||
width: 116px;
|
||
height: 100%;
|
||
background: #39bc30;
|
||
border-top-right-radius: 10px;
|
||
border-bottom-right-radius: 10px;
|
||
font-size: 16px;
|
||
font-weight: 700;
|
||
color: white;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
cursor: pointer;
|
||
user-select: none;
|
||
}
|
||
|
||
.test {
|
||
width: 100%;
|
||
height: 60px;
|
||
margin-bottom: 60px;
|
||
}
|
||
</style>
|
||
</head>
|
||
|
||
<body>
|
||
<div class="test"></div>
|
||
<div class="header">
|
||
<input type="text" placeholder="请输入内容">
|
||
<div class="btn">搜索</div>
|
||
<ul class="list">
|
||
<li>首页1</li>
|
||
<li>首页2</li>
|
||
<li>首页3</li>
|
||
<li>首页4</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
</body>
|
||
<script>
|
||
// 网页绑定滚动事件
|
||
//获取跟动的距离
|
||
// 大于某个值 给html固定定位,反之不加
|
||
|
||
|
||
var body = $("body"),
|
||
header = $(".header"),
|
||
test = $(".test"),
|
||
list = $(".list");
|
||
|
||
body.onscroll = () => {
|
||
var offsetTop = document.documentElement.scrollTop;
|
||
console.log("offsetTop:", offsetTop);
|
||
|
||
if (offsetTop > 60) {
|
||
// header.style = `
|
||
// position:"fixed"
|
||
// top:"0"
|
||
// background:"white"
|
||
// `
|
||
header.style.position = "fixed"
|
||
header.style.top = "0"
|
||
header.style.background = "white"
|
||
} else {
|
||
header.style.position = "static";
|
||
header.style.background = "red";
|
||
}
|
||
}
|
||
|
||
function $(className) {
|
||
return document.querySelector(className)
|
||
}
|
||
function _(className) {
|
||
return document.querySelectorAll(className)
|
||
}
|
||
</script>
|
||
|
||
</html> |