欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JQuery+Bootstrap 自定义全屏Loading插件

程序员文章站 2022-05-26 14:48:18
基本调用: 自动关闭: 销毁Loading Dom节点: ......
 1 /**
 2  * 自定义loading插件
 3  * @param {object} config
 4  * {
 5  * content[加载显示文本],
 6  * time[自动关闭等待时间(ms)]
 7  * } 
 8  * @param {string} config 
 9  * 加载显示文本
10  * @refer 依赖 jquery-1.9.1及以上、bootstrap-3.3.7及以上
11  * @return {kz_loading} 对象实例
12  */
13 function kz_loading(config) {
14     if (this instanceof kz_loading) {
15         const domtemplate = '<div class="modal fade kz-loading" data-kzid="@@kz_loadin_id@@" backdrop="static" keyboard="false"><div style="width: 200px;height:20px; z-index: 20000; position: absolute; text-align: center; left: 50%; top: 50%;margin-left:-100px;margin-top:-10px"><div class="progress progress-striped active" style="margin-bottom: 0;"><div class="progress-bar" style="width: 100%;"></div></div><h5>@@kz_loading_text@@</h5></div></div>';
16         this.config = {
17             content: 'loading...',
18             time: 0,
19         };
20         if (config != null) {
21             if (typeof config === 'string') {
22                 this.config = object.assign(this.config, {
23                     content: config
24                 });
25             } else if (typeof config === 'object') {
26                 this.config = object.assign(this.config, config);
27             }
28         }
29         this.id = new date().gettime().tostring();
30         this.state = 'hide';
31 
32         /*显示 */
33         this.show = function () {
34             $('.kz-loading[data-kzid=' + this.id + ']').modal({
35                 backdrop: 'static',
36                 keyboard: false
37             });
38             this.state = 'show';
39             if (this.config.time > 0) {
40                 var that = this;
41                 settimeout(function () {
42                     that.hide();
43                 }, this.config.time);
44             }
45         };
46         /*隐藏 */
47         this.hide = function (callback) {
48             $('.kz-loading[data-kzid=' + this.id + ']').modal('hide');
49             this.state = 'hide';
50             if (callback) {
51                 callback();
52             }
53         };
54         /*销毁dom */
55         this.destroy = function () {
56             var that = this;
57             this.hide(function () {
58                 var node = $('.kz-loading[data-kzid=' + that.id + ']');
59                 node.next().remove();
60                 node.remove();
61                 that.show = function () {
62                     throw new error('对象已销毁!');
63                 };
64                 that.hide = function () {};
65                 that.destroy = function () {};
66             });
67         }
68 
69         var domhtml = domtemplate.replace('@@kz_loadin_id@@', this.id).replace('@@kz_loading_text@@', this.config.content);
70         $('body').append(domhtml);
71     } else {
72         return new kz_loading(config);
73     }
74 }

基本调用:

1 var loading = new kz_loading('数据加载中。。。');
2 settimeout(function () {
3    console.log('加载完成!');
4    loading.hide();
5 }, 1000);

自动关闭:

1 var loading = new kz_loading({
2     content: '数据加载中。。。',
3     time: 2000
4 });
5 loading.show();

销毁loading dom节点:

1 loading.destroy();