<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<style type="text/css">
.top-nav {
font-size: 14px;
font-weight: bold;
list-style: none;
}
.top-nav li {
float: left;
margin-left: 1px;
}
.top-nav li a {
line-height: 34px;
text-decoration: none;
background: #3f240e;
color: #fff;
display: block;
width: 80px;
text-align: center;
}
.top-nav ul {
list-style: none;
display: none;
padding: 0;
position: absolute;
height: 0;
overflow: hidden;
}
.top-nav li a:hover {
background: url(images/slide-bg.png) 0 0 repeat-x;
}
.note {
color: #3f240e;
display: block;
background: url(images/slide-bg.png) 0 0 repeat-x;
}
.corner {
display: block;
height: 11px;
background: url(images/corner.png) 31px 0 no-repeat;
}
</style>
<script type="text/javascript">
window.onload = function() {
var Lis = document.getElementsByTagName("li");
for(var i=0; i<Lis.length; i++) {
Lis[i].onmouseover = function() {
var u = this.getElementsByTagName("ul")[0];
if(u != undefined) {
u.style.display = "block";
AddH(u.id);
}
}
Lis[i].onmouseleave = function() {
var u = this.getElementsByTagName("ul")[0];
if(u != undefined) {
SubH(u.id);
}
}
}
}
function AddH(id) {
var ulList = document.getElementById(id);
var h = ulList.offsetHeight;
h+=1;
if(h<43) {
ulList.style.height = h + "px";
setTimeout("AddH(' "+id+"')",10);
}
else {
return;
}
}
function SubH(id) {
//setTimeout();
}
</script>
<body>
<ul class="top-nav">
<li><a href="#"><span class="note">慕課網(wǎng)</span></a></li>
<li><a href="#">課堂大廳</a></li>
<li><a href="#">學(xué)習(xí)中心</a>
<ul id="mnuUL">
<span class="corner"></span>
<li><a href="#">前端課程</a></li>
<li><a href="#">手機(jī)開發(fā)</a></li>
<li><a href="#">后臺(tái)編程</a></li>
</ul>
</li>
<li><a href="#">關(guān)于我們</a></li>
</ul>
</body>
</html>
如果滑鼠在上面快速來回滑動(dòng),動(dòng)畫才會(huì)慢慢出來
·TypeError: null is not an object (evaluating 'ulList.offsetHeight')·
光陰似箭催人老,日月如移越少年。
setTimeout("AddH(' "+id+"')",10);
AddH('
後面多了個(gè)空格,這樣你獲取的mnuUL拼裝出來的id前面多了個(gè)空格,導(dǎo)致獲取不到後續(xù)的id,從而報(bào)錯(cuò)
Cannot read property 'offsetHeight' of null
建議改用 CSS3 動(dòng)畫,js 定時(shí)器動(dòng)畫很難保證幀率在每個(gè)環(huán)境下一致。