|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>练习</title>
- <style>
- div{
- width:100px;
- height:200px;
- background-color:red;
- }
- </style>
- </head>
- <body>
- <div>1</div>
- <script>
- var div=document.querySelector('div');
- function animate(obj,target){
- div.addEventListener('click',function(){
- var timer=setInterval(function(){
- if(obj.offsetleft>=target){
- clearInterval(timer);
- }
- obj.style.left= obj.offsetleft + 1 + 'px';
- },30);
- });
- };
- animate(div,300);
- </script>
- </body>
- </html>
复制代码
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>练习</title>
- <style>
- div {
- width: 100px;
- height: 200px;
- background-color: red;
- position: relative;
- }
- </style>
- </head>
- <body>
- <div style="left:0">1</div>
- <script>
- var div = document.querySelector('div');
- function animate(obj, target) {
- obj.addEventListener('click', function () {
- var timer = setInterval(function () {
- if (Number.parseInt(obj.style.left) >= target) {
- console.log(1);
- clearInterval(timer);
- }
- obj.style.left = Number.parseInt(obj.style.left) + 1 + 'px';
- }, 60);
- });
- };
- animate(div, 30);
- </script>
- </body>
- </html>
复制代码
|
|