Skip to content

Instantly share code, notes, and snippets.

@FLamparski
Last active August 29, 2015 14:17
Show Gist options
  • Save FLamparski/27f16d73974e6547f786 to your computer and use it in GitHub Desktop.
Save FLamparski/27f16d73974e6547f786 to your computer and use it in GitHub Desktop.
A CSS3 spinnin' loader.
The MIT License (MIT)
Copyright (c) 2015 Filip Wieland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
<html>
<head>
<!-- This is a CSS3 loading spinner. Attached JS shows how it can be hooked up
to your page. Created by Filip Wieland <http://www.filipwieland.com/>.
Copyright © 2015 Filip Wieland. MIT Licensed (see COPYING). -->
<meta charset="utf8">
<title>A pure CSS3 loader</title>
<style type="text/css">
@keyframes loader-spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@keyframes loader-show {
0% { transform: scale(0); opacity: 0; }
75% { transform: scale(1.15); opacity: 1; }
100% { transform: scale(1); opacity: 1; }
}
@-webkit-keyframes loader-spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@-webkit-keyframes loader-show {
0% { -webkit-transform: scale(0); opacity: 0; }
75% { -webkit-transform: scale(1.15); opacity: 1; }
100% { -webkit-transform: scale(1); opacity: 1; }
}
* {
box-sizing: border-box;
}
.loader-container {
pointer-events: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.loader-outer {
width: 100px;
height: 100px;
background-color: #2196F3;
border-radius: 100%;
box-shadow: 0px 0px 20px 10px rgba(0, 0, 0, 0.2);
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
.loader-spinner {
position: absolute;
width: 106px;
height: 106px;
top: -3px;
left: -3px;
background-color: transparent;
border-radius: 100%;
border: 3px solid transparent;
border-right-color: #FF4081;
z-index: -10;
animation: loader-spin 1s cubic-bezier(0.5, 0.1, 0.4, 0.9) infinite;
-webkit-animation: loader-spin 1s cubic-bezier(0.5, 0.1, 0.4, 0.9) infinite;
}
.loader-label {
color: #F2F2F2;
}
.loader-show {
animation: loader-show 0.3s ease-in-out;
-webkit-animation: loader-show 0.3s ease-in-out;
}
.loader-hide {
animation: loader-show 0.3s reverse ease-in-out;
-webkit-animation: loader-show 0.3s reverse ease-in-out;
}
.loader-hidden > .loader-outer {
width: 0px;
height: 0px;
opacity: 0;
}
.loader-container.loader-hidden {
display: none;
}
</style>
</head>
<body>
<div class="controls"><button type="button" data-action="start">Start</button><button type="button" data-action="stop" disabled>Stop</button></div>
<div class="loader-container loader-hidden">
<div class="loader-outer">
<div class="loader-spinner"></div>
<span class="loader-label">Loading</span>
</div>
</div>
<script type="text/javascript">
var ANIM_END = (function (){
var t;
var el = document.createElement('fakeelement');
var animations = {
'animation':'animationend',
'OAnimation':'oAnimationEnd',
'MozAnimation':'animationend',
'WebkitAnimation':'webkitAnimationEnd'
}
for(t in animations){
if( el.style[t] !== undefined ){
return animations[t];
}
}
}());
console.log('Animation end is', ANIM_END);
var Q = document.querySelector.bind(document);
var btnStart = Q('button[data-action=start]');
var btnStop = Q('button[data-action=stop]');
btnStart.addEventListener('click', function(event) {
btnStart.disabled = true;
btnStop.disabled = false;
Q('.loader-outer').classList.add('loader-show');
Q('.loader-container').classList.remove('loader-hidden');
Q('.loader-outer').addEventListener(ANIM_END, function shown(event) {
this.removeEventListener(ANIM_END, shown);
Q('.loader-outer').classList.remove('loader-show');
});
});
btnStop.addEventListener('click', function(event) {
btnStop.disabled = true;
btnStart.disabled = false;
Q('.loader-outer').classList.add('loader-hide');
Q('.loader-outer').addEventListener(ANIM_END, function hidden(event) {
this.removeEventListener(ANIM_END, hidden);
Q('.loader-container').classList.add('loader-hidden');
Q('.loader-outer').classList.remove('loader-hide');
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment