梧桐笑
2017-09-13 16:52
<!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>
<!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>
JS动画效果
113925 学习 · 1443 问题
相似问题