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+pjax简单示例汇总
Apr 21 jQuery
jquery append与appendTo方法比较
May 24 jQuery
jQuery简介_动力节点Java学院整理
Jul 04 jQuery
vue单页应用中如何使用jquery的方法示例
Jul 27 jQuery
jquery 键盘事件的使用方法详解
Sep 13 jQuery
jQuery简单实现向列表动态添加新元素的方法示例
Dec 25 jQuery
基于jquery trigger函数无法触发a标签的两种解决方法
Jan 06 jQuery
jQuery中的for循环var与let的区别
Apr 21 jQuery
jQuery实现上下滚动公告栏详细代码
Nov 21 jQuery
jQuery实现合并表格单元格中相同行操作示例
Jan 28 jQuery
jQuery添加新内容的四个常用方法分析【append,prepend,after,before】
Mar 19 jQuery
jQuery事件委托代码实践详解
Jun 21 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
雄兵连:天使彦天使彦为爱折翼,彦和炙心同时念动的誓言!
2020/03/02 国漫
php函数serialize()与unserialize()用法实例
2014/11/06 PHP
php基于自定义函数记录log日志方法
2017/07/21 PHP
JavaScript 拾漏补遗
2009/12/27 Javascript
HTML DOM的nodeType值介绍
2011/03/31 Javascript
js中各浏览器中鼠标按键值的差异
2011/04/07 Javascript
提示$ is not defined错误分析及解决
2013/04/09 Javascript
往光标所在位置插入值的js代码
2013/09/22 Javascript
javascript实现十六进制颜色值(HEX)和RGB格式相互转换
2014/06/20 Javascript
JavaScript中的公有、私有、特权和静态成员用法分析
2014/11/20 Javascript
js友好的时间返回函数
2016/08/24 Javascript
JS实现图片垂直居中显示小结
2016/12/13 Javascript
JS中传递参数的几种不同方法比较
2017/01/20 Javascript
Angular.JS去掉访问路径URL中的#号详解
2017/03/30 Javascript
bootstrap multiselect下拉列表功能
2017/08/22 Javascript
引入JavaScript时alert弹出框显示中文乱码问题
2017/09/16 Javascript
JavaScript基于对象方法实现数组去重及排序操作示例
2018/07/10 Javascript
Javascript异步流程控制之串行执行详解
2020/09/27 Javascript
vue实现动态表格提交参数动态生成控件的操作
2020/11/09 Javascript
[01:02:06]LGD vs Mineski Supermajor 胜者组 BO3 第二场 6.5
2018/06/06 DOTA
[10:53]2018DOTA2国际邀请赛寻真——EG
2018/08/11 DOTA
Python tempfile模块学习笔记(临时文件)
2014/05/25 Python
把项目从Python2.x移植到Python3.x的经验总结
2015/04/20 Python
Python实现拷贝多个文件到同一目录的方法
2016/09/19 Python
Python中几种导入模块的方式总结
2017/04/27 Python
人脸识别经典算法一 特征脸方法(Eigenface)
2018/03/13 Python
Python Numpy 数组的初始化和基本操作
2018/03/13 Python
解决python3插入mysql时内容带有引号的问题
2020/03/02 Python
某科技软件测试面试题
2013/05/19 面试题
英语系本科生个人求职信
2013/09/21 职场文书
2014年圣诞节促销方案
2014/03/14 职场文书
货物运输服务质量承诺书
2014/05/29 职场文书
授权收款委托书
2014/09/23 职场文书
采购部年度工作总结
2015/08/13 职场文书
爱心捐款倡议书:点燃希望,传递温暖
2019/11/04 职场文书
Java Spring Boot 正确读取配置文件中的属性的值
2022/04/20 Java/Android