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 相关文章推荐
推荐三款日期选择插件(My97DatePicker、jquery.datepicker、Mobiscroll)
Apr 21 jQuery
jquery实现简单实用的轮播器
May 23 jQuery
jQuery封装placeholder效果实现方法,让低版本浏览器支持该效果
Jul 08 jQuery
使用jQuery实现简单的tab框实例
Aug 22 jQuery
React中jquery引用的实现方法
Sep 12 jQuery
如何快速解决JS或Jquery ajax异步跨域的问题
Jan 08 jQuery
jQuery 改变P标签文本值方法
Feb 24 jQuery
Vue引入jquery实现平滑滚动到指定位置
May 09 jQuery
基于jQuery使用Ajax动态执行模糊查询功能
Jul 05 jQuery
AJAX在JQuery中的应用详解
Jan 30 jQuery
jquery 时间戳转日期过程详解
Oct 12 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
15种PHP Encoder的比较
2007/03/06 PHP
php存储过程调用实例代码
2013/02/03 PHP
CI框架入门之MVC简单示例
2016/11/21 PHP
PHP获取文件扩展名的方法实例总结
2017/06/10 PHP
ThinkPHP框架实现导出excel数据的方法示例【基于PHPExcel】
2018/05/12 PHP
Swoole实现异步投递task任务案例详解
2019/04/02 PHP
新浪的图片新闻效果
2007/01/13 Javascript
javascript 处理事件绑定的一些兼容写法
2009/12/24 Javascript
批量实现面向对象的实例代码
2013/07/01 Javascript
window.open 以post方式传递参数示例代码
2014/02/27 Javascript
JS实现控制表格行文本对齐的方法
2015/03/30 Javascript
javascript获取以及设置光标位置
2017/02/16 Javascript
jQuery插件HighCharts实现2D柱状图、折线图的组合多轴图效果示例【附demo源码下载】
2017/03/09 Javascript
angular实现spa单页面应用实例
2017/07/10 Javascript
原生JS+Canvas实现五子棋游戏
2020/05/28 Javascript
Element的el-tree控件后台数据结构的生成以及方法的抽取
2020/03/05 Javascript
vue路由跳转传递参数的方式总结
2020/05/10 Javascript
ssm+vue前后端分离框架整合实现(附源码)
2020/07/08 Javascript
[52:57]2014 DOTA2国际邀请赛中国区预选赛 LGD-CDEC VS HGT
2014/05/21 DOTA
python计算书页码的统计数字问题实例
2014/09/26 Python
python中的json总结
2018/10/11 Python
python assert的用处示例详解
2019/04/01 Python
python实现的读取网页并分词功能示例
2019/10/29 Python
使用python实现画AR模型时序图
2019/11/20 Python
Python concurrent.futures模块使用实例
2019/12/24 Python
python实现加密的方式总结
2020/01/19 Python
海蓝之谜英国官网:La Mer英国
2020/01/15 全球购物
俄罗斯极限运动网上商店:Board Shop №1
2020/12/18 全球购物
工商管理专业大学生职业生涯规划范文
2014/03/09 职场文书
药剂专业个人求职信范文
2014/04/29 职场文书
物理学专业自荐信
2014/06/11 职场文书
师德师风自查总结
2014/10/14 职场文书
个人工作年终总结
2015/03/09 职场文书
详解MySQL连接挂死的原因
2021/05/18 MySQL
python脚本框架webpy的url映射详解
2021/11/20 Python
详细聊聊Oracle表碎片对性能有多大的影响
2022/03/19 Oracle