学习前端很重要的一个方法就是多写代码,比如可以模仿不同网站的页面,尝试用自己的代码去实现相同的页面布局,然后再去对比自己写的代码与网站代码的区别,及其中的优劣,通过对比来发现自己的不足,从而让自己对布局的能力增强。
理就是这么个理,今天就来实现hao123网站首页的顶部。
## 效果图:
大致实现的效果如下面的动图。

## 代码实现:
HTML代码部分
1 |
|
CSS部分
1 | html,body{background-color: #eee;} |
javascript 部分
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 function showEmailMoreDetail(isClose){
var moreDetail = document.querySelector(".pass_sub");
var emailSide = document.querySelector(".email-side");
if(isClose){
moreDetail.style.display="none";
emailSide.style.cssText = ""
}else{
moreDetail.style.display="inline-block";
emailSide.style.cssText = "background-color:#f2f4f8;"
}
}
function showHotNewsTagMore(isShowMore){
var hotNewsBox = document.querySelector(".hot-news-tag");
if(!isShowMore){
hotNewsBox.style.cssText = "box-shadow:2px 2px 4px 2px #ccc;height:auto;";
document.querySelector(".hot-news-tag .tag-more").style.cssText = "transform:rotate(270deg);"
}else{
hotNewsBox.style.cssText = "";
document.querySelector(".hot-news-tag .tag-more").style.cssText = ""
}
}
window.onload = function(){
var yibuyi_scroll_count = 0; //宜不宜
var isCloseEmailDetail = false;
var searchBar_scroll_count =0;
var isHotNewsTagMore = false;
/* 城市切换 */
document.querySelector("#toggleCity").onclick = function(){
document.querySelector("#citySelect").style.cssText="visibility:visible;z-index:1000;";
}
document.querySelector("#cityCancle").onclick = function(){
document.querySelector("#citySelect").style.cssText="";
}
/* 黄历滚动 */
setInterval(function(){
var itemlist = document.querySelector(".item_list");
var itemNum = itemlist.querySelectorAll(".item").length;
yibuyi_scroll_count ++;
if(yibuyi_scroll_count >= itemNum){
yibuyi_scroll_count = 0;
}
var moveCssText = "top:"+(-18*yibuyi_scroll_count)+"px;";
itemlist.style.cssText = moveCssText;
},3000);
/* 搜索框旁边 新闻热条滚动 */
setInterval(function(){
var itemlist = document.querySelector(".rolling-list");
var itemNum = itemlist.querySelectorAll(".item").length;
searchBar_scroll_count ++;
if(searchBar_scroll_count >= itemNum){
searchBar_scroll_count = 0;
}
var moveCssText = "left:"+(-120*yibuyi_scroll_count)+"px;";
itemlist.style.cssText = moveCssText;
},3000);
/*邮箱 */
document.querySelector(".email_link").onclick = function(){
showEmailMoreDetail(isCloseEmailDetail);
isCloseEmailDetail = !isCloseEmailDetail;
}
document.querySelector(".email_input").onclick = function(){
showEmailMoreDetail(isCloseEmailDetail);
isCloseEmailDetail = !isCloseEmailDetail;
}
/* 搜索框下方的更多tag */
document.querySelector(".hot-news-tag .tag-more").onclick = function(){
showHotNewsTagMore(isHotNewsTagMore);
isHotNewsTagMore = !isHotNewsTagMore;
}
}