-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path搜索框fixed.html
52 lines (50 loc) · 888 Bytes
/
搜索框fixed.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
*{
margin:0;
padding:0;
}
body{
height:2000px;
}
#header{
height:200px;
width:100%;
background-color: green;
}
#search{
width:100%;
height:50px;
background-color: red;
}
.fix{
position:fixed;
top:0;
left:0;
}
</style>
<body>
<div id="header"></div>
<div id="search"></div>
<script>
var oSearch = document.getElementById("search");
var iSearchTop = oSearch.offsetTop;
var bIsFixed = false;
window.onscroll = function(){
var iScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if(iScrollTop >= iSearchTop && !bIsFixed){
oSearch.className = "fix";
bIsFixed = true;
}else if(iScrollTop < iSearchTop && bIsFixed){
oSearch.className = "";
bIsFixed = false;
}
};
</script>
</body>
</html>