Created
January 17, 2018 18:56
-
-
Save wpscholar/635ef5c12b039400e1052c64aff9ca90 to your computer and use it in GitHub Desktop.
Vanilla JS loader indicator
This file contains 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
function Loader(el) { | |
el.loader = this; | |
this.el = el; | |
this.style = document.getElementById('js-loader-styles'); | |
this.init(); | |
if (!document.body.animate) { | |
if (!this.style) { | |
this.style = document.createElement('style'); | |
this.style.id = 'js-loader-styles'; | |
this.style.innerText = '@keyframes spinner { 100% { transform: rotate(360deg); } }'; | |
document.head.appendChild(this.style); | |
} | |
} | |
} | |
Loader.prototype.init = function () { | |
if (!this.overlay) { | |
this.el.style.position = 'relative'; | |
this.overlay = document.createElement('div'); | |
this.overlay.style.display = 'block'; | |
this.overlay.style.position = 'absolute'; | |
this.overlay.style.top = 0; | |
this.overlay.style.right = 0; | |
this.overlay.style.bottom = 0; | |
this.overlay.style.left = 0; | |
this.overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.2)'; | |
this.loader = document.createElement('div'); | |
this.loader.style.position = 'absolute'; | |
this.loader.style.top = '50%'; | |
this.loader.style.left = '50%'; | |
this.loader.style.marginLeft = '-60px'; | |
this.loader.style.marginTop = '-60px'; | |
this.loader.style.borderWidth = '1em'; | |
this.loader.style.borderColor = 'white'; | |
this.loader.style.borderStyle = 'solid'; | |
this.loader.style.borderTopColor = 'transparent'; | |
this.loader.style.borderRadius = '50%'; | |
this.loader.style.fontSize = '16px'; | |
this.loader.style.width = '120px'; | |
this.loader.style.height = '120px'; | |
if (document.body.animate) { | |
this.loader.animate([ | |
{transform: 'rotate(0deg)'}, | |
{transform: 'rotate(360deg)'} | |
], { | |
duration: 1000, | |
iterations: Infinity | |
}); | |
} else { | |
this.loader.style.animation = 'spinner 1s linear infinite'; | |
} | |
this.overlay.appendChild(this.loader); | |
this.el.appendChild(this.overlay); | |
} | |
}; | |
Loader.prototype.show = function () { | |
this.overlay.style.display = 'block'; | |
}; | |
Loader.prototype.hide = function () { | |
this.overlay.style.display = 'none'; | |
}; | |
Loader.prototype.destroy = function () { | |
if (this.overlay) { | |
this.overlay.parentNode.removeChild(this.overlay); | |
this.overlay = null; | |
this.loader = null; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment