<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>网页loading效果 可以通过js控制旋转速度</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
background-color: rgba(0, 0, 0, 0.7);
}
.m-loading-wrapper {
z-index: 90;
position: absolute;
left: 50%;
top: 50%;
margin-left: -16px;
margin-top: -16px;
width: 32px;
height: 32px;
overflow: hidden;
display: none;
transform: rotate(270deg);
}
.m-loading {
position: absolute;
left: 0px;
top: 0px;
width: 32px;
height: 96px;
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAADACAMAAACDB5U0AAAAMFBMVEUAAAD///87TQQwAAAAEHRSTlMAUQwWSGr/BSODz+iYN7ioo0T1UwAABSpJREFUeNrtmouSozgMRbFlYfn9/387FoaIJFAYU7s7W5Vb3ZOkMzrIliD2JdNPf79UCOpJPMxV8ABADKC7UakUOgLolHRPvPbz7OEbYI1SxvaMO4Qwu28AqUroAdg4VwR9ArSqgNQ3c5xC+QQkVaWnLrm5Esw7ADi+obqmMczZvgGMMjyFnVKcAu4B1JOAlZbLNQUPAtDmbQaPM0nZO9qecwqutXKAtYTwaihz1FG2BB88ru8spYTtZLK7ElqqLJWOAd6HrNZS1nZkGEBrwq2EwP3ItKMhBM+IuLxpciF5D4yB5ZG7gWlwWD3nlyS8g5NZ3sJPT6oUQ0McZNiyZyWYzmXaTBR7cHzDg1eGLpoBawIhHgOq6LobwflCx0OQwV8g7El2HeHjApGeTmX1Sx9ZmiwqUsSTQrLMezHzTl5qcFAHIQwCzOvno4G6hqBOhzDpgUn85wRndNsXjtEdn+7J0DVCq1hixtMa0EV8cqXEWE4A5upqQC28HA8Brq5HgLGG19+jj0C5Ip6Og1p4QVpeIb7SkLUFmbWLjj4XXMs+rVeVGNHyn+3b2sJS6+N0DChx+/zE6BwDQCng0lTRlgy/SIczGBW8njsXTaUiInOSMpI2JHl+csWyWAFYX2pE5Ed4P+rliWBiBaRJAG19AlOntKvCSQDy+d4nxQnQDtC7w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C1f+QfQEo//+Cnf1t/AFGZQwaktm6jAAAAAElFTkSuQmCC");
background-size: 32px 96px;
}
</style>
</head>
<body>
<div>
设置速度:
<input type="input" value="100" id="m-speed">
<input type="button" value="显示loading" id="m-btn">
</div>
<div class="m-loading-wrapper" id="m-loading">
<div class="m-loading" id="m-loading-inner"></div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var myAction = {},
index = 1,
loadingImgInterval,
loadingInterval;
var dom = {
btn: $('#m-btn'),
speed: $('#m-speed'),
loading: $('#m-loading'),
loadingInner: $('#m-loading-inner')
};
$.extend(myAction, {
loadingFn: function(that) {
dom.loading.show();
var topArr = [0, -32, -64];
var rotateArr = [0, 90, 180, 270];
var topIndex = 0;
var rotateIndex = 0;
var speed = dom.speed.val() - 0;
clearInterval(loadingImgInterval);
loadingImgInterval = setInterval(function() {
if (topIndex >= 3) {
topIndex = 0;
if (rotateIndex >= 4) {
rotateIndex = 0;
}
dom.loading.css({ transform: 'rotate(' + rotateArr[rotateIndex] + 'deg)' });
rotateIndex++;
}
dom.loadingInner.css({ 'top': topArr[topIndex] });
topIndex++;
}, speed);
clearInterval(loadingInterval);
loadingInterval = setInterval(function() {
dom.loading.hide();
dom.loading.css({ transform: 'rotate(' + 270 + 'deg)' });
dom.loadingInner.css({ 'top': 0 });
clearInterval(loadingImgInterval);
clearInterval(loadingInterval);
}, 5000);
},
initEvent: function() {
dom.btn.on('click', function() {
var that = $(this);
myAction.loadingFn(that);
});
}
});
var init = function() {
myAction.initEvent();
}();
})
</script>
</body>
</html>