Created
July 21, 2018 08:39
-
-
Save malipetek/2a596b07094fa1ea24e5e1002db5c3cd to your computer and use it in GitHub Desktop.
jQuery pls wait
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
$.fn.plswait = function(command, arg1){ | |
this.each(function(){ | |
function loadingBtnOverlay(el) { | |
this.element = $(el); | |
this.overlay = null; | |
return this; | |
} | |
loadingBtnOverlay.prototype.start = function (btn){ | |
if(!$('body > #sclupndown-anim').is('*')){ | |
$('body').append('<style id="sclupndown-anim"> @keyframes sclupndown{ 25%{ transform: scale(1); box-shadow: 1px 1px 2px 0px rgba(0,0,0,0.1) } 50%{ transform: scale(1.5); box-shadow: 3px 3px 5px 0px rgba(0,0,0,0.1) } 100%{ transform: scale(1); box-shadow: 1px 1px 2px 0px rgba(0,0,0,0.1) }}</style>'); | |
} | |
var _this = this; | |
this.stop().then(function(){ | |
_this.overlay = $('<div class="loading-btn-overlay">') | |
for(var i=0; i<5; i++){ | |
var $dot = $('<div class="loading-dot"> </div>'); | |
$dot.css({ | |
width: 10, | |
height: 10, | |
backgroundColor: '#fff', | |
borderRadius: '50%', | |
margin: '0 2%', | |
animation: 'sclupndown .7s', | |
animationIterationCount: 'infinite', | |
boxShadow: '1px 1px 2px 0px rgba(0,0,0,0.1)' | |
}); | |
$dot.css('animation-delay', .1*i +'s'); | |
_this.overlay.append($dot); | |
} | |
_this.overlay.css({ | |
display: 'flex', | |
justifyContent: 'center', | |
alignItems: 'center', | |
position: 'absolute', | |
top: _this.element.position().top, | |
left: _this.element.position().left, | |
backgroundColor: '#7796a8', | |
width: _this.element.outerWidth(), | |
height: _this.element.outerHeight(), | |
opacity: 0, | |
transition: 'opacity .3s', | |
zIndex: 100 | |
}); | |
_this.element.offsetParent().append(_this.overlay) | |
var overlay = _this.overlay; | |
setTimeout(function(){ | |
overlay.css('opacity', 1); | |
}, 50); | |
}); | |
} | |
loadingBtnOverlay.prototype.stop = function(){ | |
var _this = this; | |
return new Promise(function(res){ | |
var overlay = _this.overlay; | |
if(overlay){ | |
overlay.css('opacity', 0); | |
setTimeout(function(){ | |
overlay.remove(); | |
res(); | |
}, 300); | |
}else{ | |
res(); | |
} | |
}); | |
} | |
this.loading = this.loading || new loadingBtnOverlay(this); | |
switch(command){ | |
case 'stop': | |
this.loading.stop() | |
break; | |
case 'start': | |
this.loading.start() | |
break; | |
default: | |
this.loading.start() | |
break; | |
} | |
}); | |
return this; | |
} | |
// example usage | |
// $(selector).plswait(); | |
// $(selector).plswait('stop'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment