国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > JS > 正文

JS動畫實現回調地獄promise的實例代碼詳解

2024-05-06 16:46:46
字體:
來源:轉載
供稿:網友

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武林網網站的支持!


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 天气| 平武县| 化州市| 天气| 黄石市| 合阳县| 当阳市| 澳门| 温州市| 泰安市| 奎屯市| 辽中县| 闵行区| 铅山县| 龙海市| 利川市| 洪江市| 天长市| 收藏| 黄石市| 四川省| 久治县| 平安县| 长寿区| 宁晋县| 临海市| 安庆市| 金华市| 黑河市| 尚志市| 宁海县| 玛沁县| 登封市| 宿迁市| 阜阳市| 陇川县| 包头市| 东乡县| 会理县| 泸西县| 麟游县|