Skip to content

Instantly share code, notes, and snippets.

@andrejewski
Created August 26, 2018 21:53
Show Gist options
  • Save andrejewski/459a0f0244c759b010fdaf9118dc9a79 to your computer and use it in GitHub Desktop.
Save andrejewski/459a0f0244c759b010fdaf9118dc9a79 to your computer and use it in GitHub Desktop.
<style>
* {
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";
box-sizing: border-box;
}
body {
margin: 0px;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
transition: 0.5s linear all;
opacity: 1;
}
body.done {
opacity: 0;
top: -500px;
}
#loader {
width: 60vw;
border: 0.25vw solid #888;
padding: 0.25vw;
}
#progress {
height: 15vh;
background-color: #32ad64;
position: relative;
}
#handle {
position: absolute;
top: 0;
right: 0;
bottom: 0;
width: 2vw;
}
#text {
font-weight: bold;
font-size: 2vw;
color: #555;
}
</style>
<div>
<div id='loader'>
<div id='amount'>
<div id='progress'>
<div id='handle'></div>
</div>
</div>
</div>
<p id='text'><span id='complete'>0%</span> completed</p>
</div>
<script>
const amount = document.querySelector('#amount')
const progress = document.querySelector('#progress')
const complete = document.querySelector('#complete')
function updateProgress (percent) {
percent = Math.max(0, Math.min(percent, 100))
progress.style.width = percent + '%'
complete.innerText = Math.round(percent) + '%'
if (percent === 100) {
document.body.classList.add('done')
}
}
updateProgress(0)
window.requestAnimationFrame(function next () {
const width = parseFloat(progress.style.width);
if (width > 60) {
return;
}
updateProgress(width + Math.random())
window.requestAnimationFrame(next)
})
const handle = document.querySelector('#handle')
let isDragging = false
handle.onmousedown = function () {
isDragging = true
}
function getRelativeWidth (container, point) {
const { left, right } = container.getClientRects()[0]
const pixelWidth = Math.max(0, point.x - left)
const fullWidth = right - left
return 100 * (pixelWidth / fullWidth)
}
document.body.onmousemove = function (e) {
if (isDragging) {
const newWidth = getRelativeWidth(amount, e)
updateProgress(newWidth)
}
}
document.body.onmouseup = function () {
isDragging = false
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment