課程
/前端開發
/Node.js
/進擊Node.js基礎(二)
按照你的代碼寫的,marginLeft 的值一直為0 ?怎么回事?
2016-03-23
源自:進擊Node.js基礎(二) 1-2
正在回答
<!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>
這個親測無壓力
貌似要設置初始值
<!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上跑看看。
一試就靈
試了還是不行
ball.style.marginLeft = marginLeft + 'px';試一下
崖址山
止水_Lily
tong wen?
舉報
本教程帶你攻破 Nodejs,讓 JavaScript流暢運行在服務器端
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-07-17
這個親測無壓力
2016-05-09
貌似要設置初始值
2016-04-16
代碼可用,在chrome上跑看看。
2016-04-13
一試就靈
2016-04-07
試了還是不行
2016-03-24
ball.style.marginLeft = marginLeft + 'px';試一下
2016-03-23
tong wen?