1. js實現動畫
<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>animate</title>  <style>    .ball {      width: 40px;      height: 40px;      margin-bottom: 5px;      border-radius: 20px;    }    .ball1 {      background: red;    }    .ball2 {      background: blue;    }    .ball3 {      background: yellow;    }  </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, left, callback) {      setTimeout(function () {        var marginLeft = parseInt(ball.style.marginLeft, 10);        if (marginLeft === left) {          callback && callback();        } else {          if (marginLeft < left) {            marginLeft += 2;          } else {            marginLeft -= 2;          }          ball.style.marginLeft = marginLeft + "px";          animate(ball, left, callback);        }      }, 13);    }    animate(ball1, 100, function () {      animate(ball2, 200, function () {        animate(ball3, 300, function () {          animate(ball1, 200, function () {            animate(ball3, 200, function () {              animate(ball2, 180, function () {                animate(ball2, 220, function () {                  animate(ball2, 200, function () {                    console.log("over");                  })                })              })            })          })        })       })    });  </script></body></html>上述代碼就可以實現一個動畫。注意下面幾點:
	•動畫的實現往往依賴于setTimeout。
	•注意ele.style.marginLeft如果開始能夠獲取,必須從元素的style中設置了才能獲取,否則獲取不到。
	•利用callback可以實現雖然使用了setTimeout還能串行執(zhí)行。
但是這產生了回調地獄,代碼簡單點還好說,一旦代碼復雜了,我們將很難處理其中的邏輯。所以這時就可以用到es6中的promise了。
Promise的寫法如下:
<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>animate</title>  <style>    .ball {      width: 40px;      height: 40px;      margin-bottom: 5px;      border-radius: 20px;    }    .ball1 {      background: red;    }    .ball2 {      background: blue;    }    .ball3 {      background: yellow;    }  </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 promiseAnimate(ball, left) {      return new Promise(function (resolve, reject) {        function animate(ball, left) {          setTimeout(function () {            var marginLeft = parseInt(ball.style.marginLeft, 10);            if (marginLeft === left) {              resolve();            } else {              if (marginLeft < left) {                marginLeft += 2;              } else {                marginLeft -= 2;              }              ball.style.marginLeft = marginLeft + "px";              animate(ball, left);            }          }, 13);        }        animate(ball,left);      });    }    promiseAnimate(ball1, 500)    .then(function () {      return promiseAnimate(ball2, 200);    })    .then(function () {      return promiseAnimate(ball3, 300);    })    .then(function () {      return promiseAnimate(ball1, 200);    })    .then(function () {      return promiseAnimate(ball3, 200);    })    .then(function () {      return promiseAnimate(ball2, 180);    })    .then(function () {      return promiseAnimate(ball2, 220);    })    .then(function () {      return promiseAnimate(ball2, 200);    })  </script></body></html>這同樣可以達到效果,并且這樣做的好處是,修改更加容易一些。對于promise,有幾點需要注意:
	1.在執(zhí)行promise相關函數的時候,要返回一個promise對象,這是常用的做法。
	2.只有返回了promise對象,我們才能實用then。
	3.并且在then中還要返回promise對象,這樣我們就可以不斷的使用then()來管理異步。
	4.在promise執(zhí)行之后,要使用resolve()來表明這個promise執(zhí)行的結束。 這樣,才能執(zhí)行then方法。
問題: 在then中如果直接執(zhí)行promiseAnimate(ball2, 200);不可以嗎? 為什么一定要return呢?
答: 當然不可以,因為如果直接執(zhí)行,確實返回了一個promise對象,但是這個promise對象只是在then下面的函數中啊, 我們必須在這個函數繼續(xù)返回這個promise對象才能達到繼續(xù)使用then的目的。
其中resolve()代表著這個異步過程的結束。
綜上所述: 動畫多用setTimeout和調用自己的方式執(zhí)行,當然,使用setInterval也是一樣的,只是前者我們更為推薦。 無論是使用setTimeout還是setInterval,都不可避免的會產生如果解決異步的問題。 之前我們解決異步的方式是使用回調函數,但是回調函數非常容易就會產生回調地獄,所以用promise會更好一些。
總結
以上所述是小編給大家介紹的JS動畫實現回調地獄promise的實例代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
新聞熱點
疑難解答