;(function($) {
$.Loading = function(options) {
//暴漏插件默认值
$.Loading.defaults = {
speed: 200, //弹出层淡出速度
bgcolor: 'rgba(0,0,0,.7)', //遮罩层颜色,需要rgba格式,默认黑色0.5透明度
type: "html", //默认html样式,也可以是canvas
msg: '正在加载...', //默认加载信息
loadPicNum: 2, //为html时loading图片哪一种;
canvassupport: false, //是否支持html5 canvas
Loadtimer: 3000, //遮罩层隐藏时间 ,只有在html格式下有效
autoHide: false, //是否自动隐藏
random: false, //是否随机loading图片,只有在html格式下有效
callback: function() {}, //默认回调函数
canvasFillColor: "#009600", //默认canvas填充色
canvasbackColor: "#ccc", //默认canvas底色
canvasFontColor: "red" //默认canvas字体色
};
var opts = $.extend({}, $.Loading.defaults, options);;
(function() {
if (window.applicationCache) {
canvassupport = true;
} else {
canvassupport = false;
}
if (opts.type && opts.type === "html") {
pageRender("html");
};
if (opts.type && opts.type === "canvas") {
if (canvassupport) {
pageRender("canvas");
} else {
pageRender("html");
}
}
})();

function pageRender(type) {
var html = '';
html += '<div class="loadingbox">';

if (type === "html") {
if (!!opts.random) {
opts.loadPicNum = Math.ceil(Math.random() * 53);
}
html += '<div class="topPart"><img src="loading/loading' + opts.loadPicNum + '.gif" /></div>'
};
if (type === "canvas") {
html += '<div class="topPart"><canvas id="mycanvas"></canvas></div>';
}
html += '<p class="font">' + opts.msg + '</p>';
html += '</div>';

var img = new Image();
img.onload = function() {
if ($(".loadingbox").length > 0) {
$(".loadingbox").remove();
}
$('body').append(html);
renderStyle(type);
}
img.src = "loading/loading" + opts.loadPicNum + ".gif";
};

function renderStyle(type) {
var iw = document.documentElement.clientWidth || document.body.clientWidth;
var loadBixW = iw > 320 ? 200 : 160;
var picH = $('.topPart').height() > 90 ? 100 : $('.topPart').height();
var ml = (iw - loadBixW) / 2;

$('.loadingbox').css({
width: loadBixW + "px",
zIndex: '999999',
position: 'fixed',
background: opts.bgcolor,
borderRadius: '15px',
cursor: 'pointer',
left: ml + 'px',
top: "20%"
});
$('.font').css({
textAlign: "center",
fontSize: '16px',
color: '#fff',
margin: "5px 0 10px 0"
});
$('.topPart').css({
width: "100%",
textAlign: "center",
paddingTop: '10px'
});
if (type === "html") {

if (picH < 70 && picH > 0) {
$('.topPart').find('img').css({
maxWidth: "100%",
padding: "20px 0"
});
}
if (picH >= 90) {
$('.topPart').find('img').css({
maxWidth: "100%",
height: picH + "px"
});
}
if (opts.autoHide)
setTimeout(function() {
closeLoading(opts.callback);
}, opts.Loadtimer);
}
if (type === "canvas") {
var canvas = document.getElementById('mycanvas');
canvas.width = 120;
canvas.height = 120;
if (canvas.getContext) {
var cxt = canvas.getContext('2d'),
W = canvas.width,
H = canvas.height,
deg = 0,
new_deg = 0,
diff = 0,
loop,
reloop,
text,
text_w;

function init() {

cxt.clearRect(0, 0, W, H);
cxt.beginPath();
cxt.strokeStyle = opts.canvasbackColor;
cxt.lineWidth = 5;
cxt.arc(W / 2, H / 2, 50, 0, Math.PI * 2, false);
cxt.stroke();

var r = deg * Math.PI / 180;
cxt.beginPath();
cxt.strokeStyle = opts.canvasFillColor;
cxt.lineWidth = 5;
cxt.arc(W / 2, H / 2, 50, 0 - 90 * Math.PI / 180, r - Math.PI * 90 / 180, false);
cxt.stroke();

cxt.fillStyle = opts.canvasFontColor;
cxt.font = "25px Arial";
text = Math.floor(deg / 360 * 100) + "%";
text_w = cxt.measureText(text).width;
cxt.fillText(text, W / 2 - text_w / 2, H / 2 + 10);
};

function draw() {
new_deg = 360;
diff = new_deg - deg;
loop = setInterval(to, 5000 / diff);

};

function to() {
if (deg == new_deg) {
clearInterval(loop);
if (opts.autoHide) closeLoading(opts.callback);

}
if (deg < new_deg) {
deg++;
}
init();
}
draw();

}
}

};

function closeLoading(callback) {
$('.loadingbox:visible').fadeOut(opts.speed);
if (typeof callback === "function") {
callback();
}
};
};
})(jQuery);

demo:

$.Loading({
type:"html",
speed:200,
loadPicNum:9,
autoHide:false,
bgcolor:'#666666'
});

最新文章

  1. .net学习笔记---tcp/udp/http/socket
  2. Ubuntu 14 中,SecureCRT、SecureFX个性化设置
  3. iOS UIView 动画浅谈
  4. 如何在其他电脑上运行VS2005编译的DEBUG版应用程序
  5. HDU 4121 Xiangqi --模拟
  6. JDK安装配置问题
  7. dota 路人水平鉴定器
  8. Partition List ——LeetCode
  9. Linux(Centos)之安装tomcat并且部署Java Web项目(转)
  10. 【Python】python 多线程两种实现方式
  11. 【示例代码】HTML+JS 画图板源码分享
  12. 绕过网站安全狗拦截,上传Webshell技巧总结(附免杀PHP一句话)
  13. React学习(一)父子组件通讯
  14. 转-CSS padding margin border属性详解
  15. Beta冲刺第六天
  16. bzoj 5289: [Hnoi2018]排列
  17. Oracle 11g一步步安装详解
  18. springboot的几种启动方式
  19. Linux嗅探ettercap
  20. Linux-lvm逻辑卷管理和提示丢失pv物理卷

热门文章

  1. IP釋放、清除、以及刷新DNS
  2. 微信 回调模式 echostr校验失败,请您检查是否正确解密并输出明文echostr
  3. DELPHI XE5 与SQLITE
  4. C#开发Windows服务 入门
  5. Redmine插件
  6. 剑指offer——树的子结构 (JAVA代码)
  7. Linux常用命令小结(续)
  8. linux git的安装与使用
  9. 第1章 Sass简介
  10. 面对对象之@classmethod、@staticmethod用法