Last active
November 11, 2017 01:11
-
-
Save christianscott/93bde81fd5ffb337e8ab990bad89825e to your computer and use it in GitHub Desktop.
Smooth progress bar animation using requestAnimationFrame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const makeLoader = (progressEl, updateProgressCount, initialProgress=0) => { | |
let progressCount = initialProgress | |
const load = () => { | |
progressCount = updateProgressCount(progressCount) | |
progressEl.value = progressCount | |
requestAnimationFrame(load) // don't need to use setTimeout - will only be called ~60 times/second | |
} | |
return load | |
} | |
// will make the progress bar loop between 0 and 100 | |
const incrementMod100 = (i) => (i + 1) % 100 | |
// alternatively... as an idea | |
const MakeXHRProgessMonitor = (requestObject) => { | |
const XHRProgessMonitor = () => { | |
// calculate loaded and total... | |
return (loaded / total) * 100 | |
} | |
return XHRProgessMonitor | |
} | |
const progressEl = document.getElementById("progress") | |
const loader = makeLoader(progressEl, incrementMod100) | |
requestAnimationFrame(loader) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<progress id="progress" value="0" max="100" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment