為什么只有第一個紅色的球可以移動?
為什么只有第一個紅色的球可以移動?
function promiseAnimate(ball,distance){
? ? ? ? ? ? return new Promise(function(resolve,reject){
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ?setTimeout(function(){
? ? ? ? ? ? ? ? ? ? //console.log(ball)
? ? ? ? ? ? ? ? ? ? var marginLeft=parseInt(ball.style.marginLeft,10)
? ? ? ? ? ? ? ? ? ? if(marginLeft===distance){
? ? ? ? ? ? ? ? ? ? ? ?resolve()
? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? if(marginLeft<distance){
? ? ? ? ? ? ? ? ? ? ? ? ? ? marginLeft++
? ? ? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? marginLeft--
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ball.style.marginLeft=marginLeft+'px'
? ? ? ? ? ? ? ? ? ? ? ? promiseAnimate(ball,distance)
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ?},13)
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?})? ?
? ? ? ? }
? ? ? ? ?
?
? ? ? ? promiseAnimate(ball01,100)
? ? ? ? ? ?.then(function(){
? ? ? ? ? ? ? ?return promiseAnimate(ball02,200)
? ? ? ? ? ?})
? ? ? ? ? ?.then(function(){
? ? ? ? ? ? ? ?return promiseAnimate(ball03,300)
? ? ? ? ? ?})
? ? ? ? ? ?.then(function(){
? ? ? ? ? ? ? ?return promiseAnimate(ball03,150)
? ? ? ? ? ?})
? ? ? ? ? ?.then(function(){
? ? ? ? ? ? ? ?return promiseAnimate(ball02,150)
? ? ? ? ? ?})
? ? ? ? ? ?.then(function(){
? ? ? ? ? ? ? ?return promiseAnimate(ball01,150)
? ? ? ? ? ?})
2018-10-15
function promiseAnimate(ball,distance){
return new Promise(function(resolve,reject){
function _animate(){
setTimeout(function(){
var marginLeft = parseInt(ball.style.marginLeft,10)
if(marginLeft === distance){
resolve()
}
else{
if(marginLeft < distance){
marginLeft ++
}
else{
marginLeft --
}
ball.style.marginLeft = marginLeft + 'px'
_animate()
}
},13)
}
_animate()
})
}