JQuery+Bootstrap 自定义全屏Loading插件的示例demo


Posted in jQuery onJuly 03, 2019

JQuery+Bootstrap 自定义全屏Loading插件

/**
 * 自定义Loading插件
 * @param {Object} config
 * {
 * content[加载显示文本],
 * time[自动关闭等待时间(ms)]
 * } 
 * @param {String} config 
 * 加载显示文本
 * @refer 依赖 JQuery-1.9.1及以上、Bootstrap-3.3.7及以上
 * @return {KZ_Loading} 对象实例
 */
function KZ_Loading(config) {
  if (this instanceof KZ_Loading) {
    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>';
    this.config = {
      content: 'loading...',
      time: 0,
    };
    if (config != null) {
      if (typeof config === 'string') {
        this.config = Object.assign(this.config, {
          content: config
        });
      } else if (typeof config === 'object') {
        this.config = Object.assign(this.config, config);
      }
    }
    this.id = new Date().getTime().toString();
    this.state = 'hide';

    /*显示 */
    this.show = function () {
      $('.kz-loading[data-kzid=' + this.id + ']').modal({
        backdrop: 'static',
        keyboard: false
      });
      this.state = 'show';
      if (this.config.time > 0) {
        var that = this;
        setTimeout(function () {
          that.hide();
        }, this.config.time);
      }
    };
    /*隐藏 */
    this.hide = function (callback) {
      $('.kz-loading[data-kzid=' + this.id + ']').modal('hide');
      this.state = 'hide';
      if (callback) {
        callback();
      }
    };
    /*销毁dom */
    this.destroy = function () {
      var that = this;
      this.hide(function () {
        var node = $('.kz-loading[data-kzid=' + that.id + ']');
        node.next().remove();
        node.remove();
        that.show = function () {
          throw new Error('对象已销毁!');
        };
        that.hide = function () {};
        that.destroy = function () {};
      });
    }

    var domHtml = domTemplate.replace('@@KZ_Loadin_ID@@', this.id).replace('@@KZ_Loading_Text@@', this.config.content);
    $('body').append(domHtml);
  } else {
    return new KZ_Loading(config);
  }
}

基本调用:

var loading = new KZ_Loading('数据加载中。。。');
setTimeout(function () {
  console.log('加载完成!');
  loading.hide();
}, 1000);

自动关闭:

var loading = new KZ_Loading({
  content: '数据加载中。。。',
  time: 2000
});
loading.show();

销毁Loading Dom节点:

 loading.destroy();

ps:下面看下基于JQUERY BOOTSTRAP 最简单的loading遮罩层

<%--loading遮罩层--%>
<div class="modal fade" id="loadingModal" 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 id="loadText">loading...</h5>
  
</div>
</div>
<%--loading方法--%>
<script type="text/javascript">
  var loadingTime= 5;//默认遮罩时间
  showLoading = function (loadText) {
    if(!loadText){
      $("#loadText").html(loadText)
    }
    $('#loadingModal').modal({backdrop: 'static', keyboard: false});
  }
  hideLoading = function () {
    $('#loadingModal').modal('hide');
  }
</script>

总结

以上所述是小编给大家介绍的JQuery+Bootstrap 自定义全屏Loading插件的示例demo,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

jQuery 相关文章推荐
jQuery异步提交表单实例
May 30 jQuery
JQuery EasyUI的一些常用组件
Jul 12 jQuery
JavaScript自执行函数和jQuery扩展方法详解
Oct 27 jQuery
基于jquery.page.js实现分页效果
Jan 01 jQuery
vue.js2.0点击获取自己的属性和jquery方法
Feb 23 jQuery
jquery 实现拖动文件上传加载进度条功能
Mar 18 jQuery
jQuery基于Ajax实现读取XML数据功能示例
May 31 jQuery
JS/jQuery实现简单的开关灯效果【案例】
Feb 19 jQuery
使用jquery-easyui的布局layout写后台管理页面的代码详解
Jun 19 jQuery
jQuery实现的移动端图片缩放功能组件示例
May 01 jQuery
jquery绑定事件 bind和on的用法与区别分析
May 22 jQuery
jQuery实现日历效果
Sep 11 jQuery
jQuery属性选择器用法实例分析
Jun 28 #jQuery
jQuery位置选择器用法实例分析
Jun 28 #jQuery
jQuery层叠选择器用法实例分析
Jun 28 #jQuery
jQuery内容选择器与表单选择器实例分析
Jun 28 #jQuery
jQuery子选择器与可见性选择器实例分析
Jun 28 #jQuery
基于jQuery的时间戳与日期间的转化
Jun 21 #jQuery
jQuery事件委托代码实践详解
Jun 21 #jQuery
You might like
PHP Directory 函数的详解
2013/03/07 PHP
教大家制作简单的php日历
2015/11/17 PHP
基于Codeigniter框架实现的student信息系统站点动态发布功能详解
2017/03/23 PHP
PHP中file_put_contents追加和换行的实现方法
2017/04/01 PHP
Firefox和IE浏览器兼容JS脚本写法小结
2008/07/07 Javascript
javascript 跳转代码集合
2009/12/03 Javascript
js null undefined 空区别说明
2010/06/13 Javascript
jQuery 数据缓存模块进化史详细介绍
2012/11/19 Javascript
jQuery用unbind方法去掉hover事件及其他方法介绍
2013/03/18 Javascript
jQuery动态添加、删除元素的方法
2014/01/09 Javascript
JavaScript实现向setTimeout执行代码传递参数的方法
2015/04/16 Javascript
PHP+jQuery+Ajax+Mysql如何实现发表心情功能
2015/08/06 Javascript
Node.js 8 中的 util.promisify的详解
2017/06/12 Javascript
vue.js 嵌套循环、if判断、动态删除的实例
2018/03/07 Javascript
ionic grid(栅格)九宫格制作详解
2018/06/30 Javascript
基于Bootstrap下拉框插件bootstrap-select使用方法详解
2018/08/07 Javascript
JQuery Ajax跨域调用和非跨域调用问题实例分析
2019/04/16 jQuery
详解如何修改 node_modules 里的文件
2020/05/22 Javascript
Vue如何基于vue-i18n实现多国语言兼容
2020/07/17 Javascript
[03:20]次级联赛厮杀超职业 现超级兵对拆世纪大战
2014/10/30 DOTA
Python用sndhdr模块识别音频格式详解
2018/01/11 Python
全面分析Python的优点和缺点
2018/02/07 Python
python画折线图的程序
2018/07/26 Python
python使用numpy读取、保存txt数据的实例
2018/10/14 Python
对matplotlib改变colorbar位置和方向的方法详解
2018/12/13 Python
python list转置和前后反转的例子
2019/08/26 Python
Python3 读取Word文件方式
2020/02/13 Python
python3 sleep 延时秒 毫秒实例
2020/05/04 Python
python遍历路径破解表单的示例
2020/11/21 Python
网页设计个人找工作求职信
2013/11/28 职场文书
师范生的个人求职信范文
2014/01/04 职场文书
村容村貌整治方案
2014/05/21 职场文书
关于运动会的广播稿50字
2014/10/17 职场文书
2014年发展党员工作总结
2014/11/12 职场文书
图书馆义工感想
2015/08/07 职场文书
python解析json数据
2022/04/29 Python