|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>动画函数封装</title>
- <style>
- div{
- width:100px;
- height:100px;
- background-color:gold;
- }
-
- </style>
- </head>
-
-
- <body>
- <div>1</div>
- <button>点击移动盒子</button>
- <script>
- var div=document.querySelector('div');
- function animate(obj,target,callback){
- clearInterval(obj.timer);
- obj.timer=setInterval(function(){
- var step=(target-obj.offsetLeft)/10;
- step=step>0?Math.ceil(step):Math.floor(step);
- if(obj.offsetLeft==target){
- clearInterval(obj.timer);
- if(callback){
- callback();
- }
- }
- obj.style.left=obj.offsetLeft+step+'px';
- },1);
- }
-
- div.addEventListener('click',function(){
- animate(div,800);
- alert('美好');
- });
- </script>
- </body>
- </html>
复制代码
div{
position:absolute;
width:100px;
height:100px;
background-color:gold;
}
|
|