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

动画实现不了

按照你的代码写的,marginLeft 的值一直为0  怎么回事?

正在回答

7 回答

<!doctype>
<html>
<head>
	<title>Promise animation</title>
	<style>
	.ball{
		width:40px;
		height:40px;
		border-radius:20px;
	}
	.ball1{
		background:red;
	}
	.ball2{
		background:yellow;
	}
	.ball3{
		background:green;
	}
	</style>
</head>
<body>
	<div class="ball ball1" style="margin-left:0;"></div>
	<div class="ball ball2" style="margin-left:0;"></div>
	<div class="ball ball3" style="margin-left:0;"></div>
	
	<script>
	var ball1 = document.querySelector('.ball1')
	var ball2 = document.querySelector('.ball2')
	var ball3 = document.querySelector('.ball3')
	
	function animate (ball,distance,cb){
		setTimeout(function(){
		
			var marginLeft =parseInt(ball.style.marginLeft,10)
			
			if (marginLeft === distance){
				cb&&cb()
			}
			else{
			
				if (marginLeft<distance){
					marginLeft++
				}
				else{
					marginLeft--
				}
				ball.style.marginLeft = marginLeft
				animate (ball,distance,cb)
				//不断重复动作
			}
		},13)
	}
	
	animate(ball1,100,function(){
		animate(ball2,200,function(){
			animate(ball3,300,function(){
				animate(ball3,150,function(){
					animate(ball2,150,function(){
						animate(ball1,100,function(){
						//
						})
					})
				})
			})
		})
	})
	</script>
	
</body>
</html>

这个亲测无压力

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

貌似要设置初始值

0 回复 有任何疑惑可以回复我~
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Promise animate</title>
  <style>
    .ball{
      width: 40px;
      height: 40px;
      border-radius: 20px;
    }
    .ball1{
      background: red;
    }
    .ball2{
      background: yellow;
    }
    .ball3{
      background: green;
    }
  </style>
</head>
<body>
  <div class="ball ball1" style="margin-left:0;"></div>
  <div class="ball ball2" style="margin-left:0;"></div>
  <div class="ball ball3" style="margin-left:0;"></div>

<script>
  var ball1 = document.querySelector('.ball1');
  var ball2 = document.querySelector('.ball2');
  var ball3 = document.querySelector('.ball3');

  function animate(ball, distance, callback){
    setTimeout(function() {
      var marginLeft = parseInt(ball.style.marginLeft, 10);
      console.log(marginLeft);
      console.info(distance);
      if(marginLeft === distance){
        callback && callback();
      }else{
        if(marginLeft < distance){
          marginLeft++;
        }else{
          marginLeft--;
        }

        ball.style.marginLeft = marginLeft + 'px';
        animate(ball, distance, callback);
      }
    }, 13);
  }

  animate(ball1, 100, function(){
    animate(ball2, 200, function() {
      animate(ball3, 300, function() {
        animate(ball3, 150, function() {
          animate(ball2, 150, function() {
            animate(ball1, 150, function() {
          
            })
          })
        })
      })
    })
  });
</script>
</body>
</html>

代码可用,在chrome上跑看看。

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

一试就灵

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

试了还是不行

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

ball.style.marginLeft = marginLeft + 'px';试一下

3 回复 有任何疑惑可以回复我~
#1

崖址山

WHY?
2016-03-24 回复 有任何疑惑可以回复我~
#2

止水_Lily

老师写的就没有+'px',代码仍然运行了呀
2016-05-24 回复 有任何疑惑可以回复我~

tong wen 

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

举报

0/150
提交
取消
进击Node.js基础(二)
  • 参与学习       76793    人
  • 解答问题       226    个

本教程带你攻破 Nodejs,让 JavaScript流畅运行在服务器端

进入课程

动画实现不了

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