-
Notifications
You must be signed in to change notification settings - Fork 0
/
baidu.html
151 lines (145 loc) · 3.35 KB
/
baidu.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>百度一下</title>
<link rel="icon" href="https://www.baidu.com/favicon.ico" />
<style>
*{
padding: 0;
margin: 0;
}
li{
list-style: none;
}
.container{
width: 641px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
.logo{
width: 540px;
height: 218px;
margin: 0 auto;
background: #fff url(http://www.baidu.com/img/bd_logo1.png?where=super) center bottom no-repeat;
background-size: 50%;
margin-bottom: 40px;
}
.search-box{
width: 641px;
height: 36px;
position: relative;
}
.keyword{
height: 34px;
width: 535px;
border:1px solid #ccc;
float: left;
border-right: 0;
padding-left: 5px;
outline: none;
font-size: 16px;
color: #3f3f3f;
}
.btn{
width: 100px;
height: 36px;
float: right;
background-color: #3385FF;
color: #fff;
border:0;
cursor: pointer;
font-size: 15px;
line-height: 36px;
}
.btn:hover{
background-color: #2868c8;
box-shadow: 1px 1px 1px #2868c8;
}
.search-box .photo{
width: 18px;
height: 16px;
background: #fff url(https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/soutu/img/camera_new_5606e8f.png) 0 0 no-repeat;
position: absolute;
top: 10px;
right: 110px;
}
.search-box .keyword:focus{
border-color: #3385FF;
}
.search-box .photo:hover{
background-position: 0 -20px;
}
.droplist{
position: absolute;
width: 541px;
border:1px solid #ccc;
border-top: 0;
top: 36px;
left: 0;
display: none;
}
.droplist li{
padding-left: 5px;
width: 536px;
height: 30px;
line-height: 30px;;
}
.droplist li:hover{
background-color: aliceblue;
}
</style>
</head>
<body>
<div class="container">
<div class="logo"></div>
<div class="search-box">
<form action="https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=?" method="get" target="_blank">
<i class="photo"></i>
<input type="text" class="keyword" name="wd" autocomplete="off">
<ul class="droplist">
<li>nihap</li>
</ul>
<input type="submit" class="btn" value="百度一下">
</form>
</div>
</div>
</body>
<script>
var sercahInput = document.getElementsByClassName("keyword")[0];
var droplist = document.getElementsByClassName("droplist")[0];
sercahInput.addEventListener("input", throttle(function () {
var _script = document.createElement("script");
_script.src = `http://suggestion.baidu.com/?wd=${this.value}&cb=callback`;
document.body.appendChild(_script);
},600,sercahInput));
function callback(data) {
droplist.innerHTML = "";
data.s.forEach(item => {
var li = document.createElement("li");
li.innerText = item;
droplist.appendChild(li);
});
droplist.style.display = "block";
}
droplist.addEventListener("click", function (e) {
sercahInput.value = e.target.innerText;
droplist.style.display = "none";
});
//节流
function throttle(callback, duration, context) { //回调函数 间隔时间 操作区域
var lasttime = 0;
return function (e) {
var now = new Date().getTime();
if (now - lasttime > duration) { //第一次一定会执行
callback.call(context, e); //用调这个函数来调用回调函数
lasttime = new Date().getTime(); //更新时间
}
}
}
</script>
</html>