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

 更新时间:2019年07月03日 10:24:58   作者:小心哈  
这篇文章主要介绍了JQuery+Bootstrap 自定义全屏Loading插件,本文通过示例代码给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下

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 Ajax 仿AjaxPro.Utility.RegisterTypeForAjax辅助方法

    jQuery Ajax 仿AjaxPro.Utility.RegisterTypeForAjax辅助方法

    我们都知道在AjaxPro的方法AjaxPro.Utility.RegisterTypeForAjax(typeof(所在类的类名));会将标记有[Ajax.AjaxMethod]方法注册在客户端。
    2011-09-09
  • Jquery uploadify上传插件使用详解

    Jquery uploadify上传插件使用详解

    这篇文章主要介绍了JQuery上传插件Uploadify使用,文章给出详细的步骤来实现一个简单的上传功能,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • jQuery包裹节点用法完整示例

    jQuery包裹节点用法完整示例

    这篇文章主要介绍了jQuery包裹节点用法,结合完整实例形式分析了jQuery中wrap方法包裹节点的使用技巧,注释中还包含了wrapAll与wrapInner方法的使用,需要的朋友可以参考下
    2016-09-09
  • jQuery中delegate()方法的用法详解

    jQuery中delegate()方法的用法详解

    delegate()方法是为匹配元素的子元素添加一个或多个事件,并规定当这些事件发生时运行的函数。下面就是详细介绍,有需要的朋友可以来介绍一下。
    2016-10-10
  • jquery拖拽效果完整实例(附demo源码下载)

    jquery拖拽效果完整实例(附demo源码下载)

    这篇文章主要介绍了jquery拖拽效果实现方法,详细介绍了jQuery实现拖拽功能的具体步骤与相关技巧,并附代码了demo源码供读者下载参考,需要的朋友可以参考下
    2016-01-01
  • jquery 元素相对定位代码

    jquery 元素相对定位代码

    align:左右对齐;vlign:垂直对齐;xleft:水平补给值;xleft:垂直补给值;adjust:是否相对窗口自动调整;rwindow:定位相对于窗口(align&vlign为center时垂直或水平居中)
    2010-10-10
  • jquery select2的使用心得(推荐)

    jquery select2的使用心得(推荐)

    下面小编就为大家带来一篇jquery select2的使用心得(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • JQuery文字列表向上滚动的代码

    JQuery文字列表向上滚动的代码

    JQuery实现的文字列表一条一条向上滚动的效果
    2013-11-11
  • jQuery基于图层模仿五星星评价功能的方法

    jQuery基于图层模仿五星星评价功能的方法

    这篇文章主要介绍了jQuery基于图层模仿五星星评价功能的方法,使用jQuery动态修改元素背景色的方法实现星评功能,需要的朋友可以参考下
    2015-05-05
  • jquery判断checkbox是否选中及改变checkbox状态的实现方法

    jquery判断checkbox是否选中及改变checkbox状态的实现方法

    下面小编就为大家带来一篇jquery判断checkbox是否选中及改变checkbox状态的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05

最新评论