赞
踩
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<!-- 计时器每调动一次,移动的步长逐渐减小 -->
<title>缓动动画</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
p{
width: 100px;
height: 100px;
background-color: yellow;
position: absolute;
top: 100px;
left: 0px;
}
</style>
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
window.onload=function(){
var leader=0;//每次移动的位置;
var target=0;//移动的最后目标位置;
var timer;//定时器的引用;
$("btn").onclick=function(){
target=500;
if(timer){
clearInterval(timer);
}
timer = setInterval(function(){
leader=leader+(target-leader)/10;
$("ht").style.left=leader+"px";//改变的是行内样式,而不是内联样式;
},100);
}
}
</script>
</head>
<body>
<input type="button" value="点击" id="btn" />
<p id="ht"></p>
</body>
</html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。