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