到底是哪里有問(wèn)題?。????
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
?? ?<title>多物體運(yùn)動(dòng)</title>
?? ?<link rel="stylesheet" type="text/css" href="css/style.css">
?? ?<script type="text/javascript" src="js/script.js"></script>
</head>
<body>
?? <ul>
? ??? ?<li ></li>
? ??? ?<li ></li>
? ??? ?<li ></li>
?? </ul>
</body>
</html>
CSS
ul li{
?? ?list-style:none;
?? ?width: 200px;
?? ?height: 100px;
?? ?margin-top: 20px;
?? ?background: yellow;
?? ?cursor: pointer;
}
JS
window.onload=function(){
?? ?var ali=document.getElementsByTagName('li');
???? for(var i=0;i<ali.length;i++){
??? ??? ?ali[i].onmouseover=function(){
?????????? setWidth(this,400);
?? ???? }
?? ???? ali[i].onmouseover=function(){
?? ??? ??? setWidth(this,200);
?? ???? }
???? }
?? ?
}
var timer=null;
function setWidth(obj,target){
?? ?clearInterval(timer);
??? timer=setInterval(function(){
?? ??? ???? var speed=(target-obj.offsetWidth)/5;
??????????? speed=speed>0?Math.ceil(speed):Math.floor(speed);
??????????? if(obj.offsetWidth==target){
?????????? ??? ?clearInterval(timer);
??????????? }
??????????? else{
?????????? ??? ?obj.style.width=obj.offsetWidth+speed+"px";
??????????? }
??????? },30);
}
2016-11-18
好吧 又找到錯(cuò)誤了!應(yīng)該是鼠標(biāo)離開(kāi)事件,寫(xiě)成了鼠標(biāo)滑過(guò)事件