JS動(dòng)畫實(shí)現(xiàn)回調(diào)地獄promise的實(shí)例代碼詳解
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)地獄,代碼簡(jiǎn)單點(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>
這同樣可以達(dá)到效果,并且這樣做的好處是,修改更加容易一些。對(duì)于promise,有幾點(diǎn)需要注意:
1.在執(zhí)行promise相關(guān)函數(shù)的時(shí)候,要返回一個(gè)promise對(duì)象,這是常用的做法。
2.只有返回了promise對(duì)象,我們才能實(shí)用then。
3.并且在then中還要返回promise對(duì)象,這樣我們就可以不斷的使用then()來管理異步。
4.在promise執(zhí)行之后,要使用resolve()來表明這個(gè)promise執(zhí)行的結(jié)束。 這樣,才能執(zhí)行then方法。
問題: 在then中如果直接執(zhí)行promiseAnimate(ball2, 200);不可以嗎? 為什么一定要return呢?
答: 當(dāng)然不可以,因?yàn)槿绻苯訄?zhí)行,確實(shí)返回了一個(gè)promise對(duì)象,但是這個(gè)promise對(duì)象只是在then下面的函數(shù)中啊, 我們必須在這個(gè)函數(shù)繼續(xù)返回這個(gè)promise對(duì)象才能達(dá)到繼續(xù)使用then的目的。
其中resolve()代表著這個(gè)異步過程的結(jié)束。
綜上所述: 動(dòng)畫多用setTimeout和調(diào)用自己的方式執(zhí)行,當(dāng)然,使用setInterval也是一樣的,只是前者我們更為推薦。 無論是使用setTimeout還是setInterval,都不可避免的會(huì)產(chǎn)生如果解決異步的問題。 之前我們解決異步的方式是使用回調(diào)函數(shù),但是回調(diào)函數(shù)非常容易就會(huì)產(chǎn)生回調(diào)地獄,所以用promise會(huì)更好一些。
總結(jié)
以上所述是小編給大家介紹的JS動(dòng)畫實(shí)現(xiàn)回調(diào)地獄promise的實(shí)例代碼詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
javascript實(shí)現(xiàn)網(wǎng)頁(yè)端解壓并查看zip文件
昨天給大家分享了在網(wǎng)頁(yè)端使用zip.js插件實(shí)現(xiàn)在線壓縮文件的代碼,今天給大家分享一下javascript實(shí)現(xiàn)網(wǎng)頁(yè)端解壓并查看zip文件的方法,非常的實(shí)用,有需要的小伙伴可以參考下2015-12-12windows系統(tǒng)下簡(jiǎn)單nodejs安裝及環(huán)境配置
相信對(duì)于很多關(guān)注javascript發(fā)展的同學(xué)來說,nodejs已經(jīng)不是一個(gè)陌生的詞眼,這里不想談太多的nodejs的相關(guān)信息。只說一下,windows系統(tǒng)下簡(jiǎn)單nodejs環(huán)境配置2013-01-01Javascript使用post方法提交數(shù)據(jù)實(shí)例
這篇文章主要介紹了Javascript使用post方法提交數(shù)據(jù),實(shí)例分析了javascript實(shí)現(xiàn)post提交數(shù)據(jù)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08js中innerText/textContent和innerHTML與target和currentTarget的區(qū)別
今天小編就為大家分享一篇關(guān)于js中innerText/textContent和innerHTML與target和currentTarget的區(qū)別,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01IntersectionObserver實(shí)現(xiàn)圖片懶加載的示例
下面小編就為大家?guī)硪黄狪ntersectionObserver實(shí)現(xiàn)圖片懶加載的示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-0915位和18位身份證JS校驗(yàn)的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄?5位和18位身份證JS校驗(yàn)的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07在一個(gè)頁(yè)面重復(fù)使用一個(gè)js函數(shù)的方法詳解
下面小編就為大家?guī)硪黄谝粋€(gè)頁(yè)面重復(fù)使用一個(gè)js函數(shù)的方法詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,祝大家游戲愉快哦2016-12-12