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

首頁 > 語言 > JavaScript > 正文

JS動(dòng)畫實(shí)現(xiàn)回調(diào)地獄promise的實(shí)例代碼詳解

2024-05-06 15:28:07
字體:
供稿:網(wǎng)友

1. js實(shí)現(xiàn)動(dòng)畫

<!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>

上述代碼就可以實(shí)現(xiàn)一個(gè)動(dòng)畫。注意下面幾點(diǎn):

•動(dòng)畫的實(shí)現(xiàn)往往依賴于setTimeout。
•注意ele.style.marginLeft如果開始能夠獲取,必須從元素的style中設(shè)置了才能獲取,否則獲取不到。
•利用callback可以實(shí)現(xiàn)雖然使用了setTimeout還能串行執(zhí)行。

但是這產(chǎn)生了回調(diào)地獄,代碼簡單點(diǎn)還好說,一旦代碼復(fù)雜了,我們將很難處理其中的邏輯。所以這時(shí)就可以用到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>            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 平顶山市| 新巴尔虎右旗| 龙江县| 东阿县| 武胜县| 高青县| 隆昌县| 安宁市| 镇安县| 乐清市| 河北省| 辽宁省| 修文县| 乐安县| 南漳县| 五河县| 来安县| 深水埗区| 孝义市| 杂多县| 泰顺县| 鹿邑县| 五指山市| 麦盖提县| 永胜县| 江油市| 郁南县| 拉萨市| 措勤县| 青海省| 惠水县| 宁化县| 买车| 改则县| 丽水市| 合山市| 永福县| 南靖县| 兴山县| 浮梁县| 河间市|