为了账号安全,请及时绑定邮箱和手机立即绑定

大家看看哪里出下问题了?

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>多物体动画</title>

<style>

*{padding:0; margin:0; list-style: none;font-size: 36px;}

ul li{

/* width:150px; */

height:80px;

line-height: 80px;

background-color: #ff8040;

margin-bottom:20px;

border:4px #c0c0c0 solid;

text-align: center;

}

</style>

<script>

window.onload=function(){

var lis=document.getElementsByTagName("li");

for(var i=0;i<lis.length;i++)

{

lis[i].timer=null;                     //多物体运动所有参数不能共用,包括定时器,必须给各物体都加一个定时器

lis[i].onmouseover=function(){

move(this,300);

}

lis[i].onmouseout=function(){

move(this,150);

}

}

function move(obj,target){

clearInterval(obj.timer);

obj.timer=setInterval(function(){

var speed=(target-obj.offsetWidth)/10;

speed=speed>0?Math.ceil(speed):Math.floor(speed);

if(obj.offsetWidth==target)

clearInterval(obj.timer);

else

obj.style.width=parseInt(obj.style.width)+speed+'px';

},50);

}

}

</script>

</head>

<body>

<ul >

<li width="150px">1</li>

<li width="150px">2</li>

<li width="150px">3</li>

</ul>

</body>

</html>




正在回答

1 回答

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>多物体动画</title>

<style>

*{padding:0; margin:0; list-style: none;font-size: 36px;}

ul li{

width:150px;

height:80px;

line-height: 80px;

background-color: #ff8040;

margin-bottom:20px;

border:4px #c0c0c0 solid;

text-align: center;

}

</style>

<script>

window.onload=function(){

var lis=document.getElementsByTagName("li");

for(var i=0;i<lis.length;i++)

{

lis[i].timer=null;                     //多物体运动所有参数不能共用,包括定时器,必须给各物体都加一个定时器

lis[i].onmouseover=function(){

move(this,300);

}

lis[i].onmouseout=function(){

move(this,150);

}

}

}

function getStyle(obj,attr){

if(obj.currentStyle){

return obj.currentStyle[attr];

}else{

return getComputedStyle(obj,false)[attr];

}

}

function move(obj,target){

clearTimeout(obj.timer);

obj.timer=setInterval(function(){

var atr=parseInt(getStyle(obj,'width'));

var speed=(target-atr)/10;

speed=speed>0?Math.ceil(speed):Math.floor(speed);

if(atr==target){

clearTimeout(obj.timer);

}

else{

obj.style.width=atr+speed+'px';

}

},50);

}

 

</script>

</head>

<body>

<ul >

<li width="150px">1</li>

<li width="150px">2</li>

<li width="150px">3</li>

</ul>

</body>

</html>


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

大家看看哪里出下问题了?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信