PerfectLoad.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Created by chen on 2017/10/31.
  3. */
  4. jQuery.bootstrapLoading = {
  5. isLoading: function () {
  6. return $('#loadingPage').is(':visible');
  7. },
  8. start: function (options) {
  9. var defaults = {
  10. opacity: 0.5,
  11. //loading页面透明度
  12. // backgroundColor: "#FFFFFF",
  13. //loading页面背景色
  14. borderColor: "#bbb",
  15. //提示边框颜色
  16. borderWidth: 1,
  17. //提示边框宽度
  18. borderStyle: "solid",
  19. //提示边框样式
  20. loadingTips: "Loading, please wait...",
  21. //提示文本
  22. TipsColor: "#fff",
  23. //提示颜色
  24. delayTime: 500,
  25. //页面加载完成后,加载页面渐出速度
  26. zindex: 2000,
  27. //loading页面层次
  28. sleep: 0
  29. //设置挂起,等于0时则无需挂起
  30. }
  31. var options = $.extend(defaults, options);
  32. //获取页面宽高
  33. var _PageHeight = document.documentElement.clientHeight,
  34. _PageWidth = document.documentElement.clientWidth;
  35. //在页面未加载完毕之前显示的loading Html自定义内容
  36. //var _LoadingHtml = '<div id="loadingPage" style="position:fixed;left:0;top:0;_position: absolute;width:100%;height:' + _PageHeight + 'px;background:' + options.backgroundColor + ';opacity:' + options.opacity + ';filter:alpha(opacity=' + options.opacity * 100 + ');z-index:' + options.zindex + ';"><div id="loadingTips" style="position: absolute; cursor1: wait; width: auto;border-color:' + options.borderColor + ';border-style:' + options.borderStyle + ';border-width:' + options.borderWidth + 'px; height:80px; line-height:80px; padding-left:80px; padding-right: 5px;border-radius:10px; background: ' + options.backgroundColor + ' url(/Content/bootstrap-loading/images/loading.gif) no-repeat 5px center; color:' + options.TipsColor + ';font-size:20px;">' + options.loadingTips + '</div></div>';
  37. var _LoadingHtml = '<div id="loadingPage" tabindex="-1" style="position:fixed;left:0;top:0;_position: absolute;width:100%;height:' + _PageHeight + 'px;background:' + options.backgroundColor + ';opacity:' + options.opacity + ';filter:alpha(opacity=' + options.opacity * 100 + ');z-index:' + options.zindex + ';"><div id="loadingTips" style="position: absolute; cursor1: wait; width: auto; top: 50%; left: 50%;">' + '<div class="text-green"><i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i> <span class="sr-only">Loading...</span> </div> </div><input type="text" id="loadingFocus" style="display: none;"></div>';
  38. //呈现loading效果
  39. $("body").append(_LoadingHtml);
  40. //获取loading提示框宽高
  41. var _LoadingTipsH = document.getElementById("loadingTips").clientHeight,
  42. _LoadingTipsW = document.getElementById("loadingTips").clientWidth;
  43. //计算距离,让loading提示框保持在屏幕上下左右居中
  44. var _LoadingTop = _PageHeight > _LoadingTipsH ? (_PageHeight - _LoadingTipsH) / 2 : 0,
  45. _LoadingLeft = _PageWidth > _LoadingTipsW ? (_PageWidth - _LoadingTipsW) / 2 : 0;
  46. $("#loadingTips").css({
  47. "left": _LoadingLeft + "px",
  48. "top": _LoadingTop + "px"
  49. });
  50. //监听页面加载状态
  51. document.onreadystatechange = PageLoaded;
  52. //当页面加载完成后执行
  53. function PageLoaded() {
  54. if (document.readyState == "complete") {
  55. var loadingMask = $('#loadingPage');
  56. setTimeout(function () {
  57. loadingMask.animate({
  58. "opacity": 0
  59. },
  60. options.delayTime,
  61. function () {
  62. $(this).hide();
  63. });
  64. setTimeout(function () {
  65. const $loadingFocus = $('#loadingFocus')[0];
  66. if ($loadingFocus) {
  67. $loadingFocus.focus();
  68. }
  69. }, 200);
  70. },
  71. options.sleep);
  72. }
  73. }
  74. },
  75. end: function () {
  76. $("#loadingPage").remove();
  77. },
  78. progressStop: true,
  79. progressStart: async function (title = "导出文件", autoBar = false) {
  80. function setTimeoutSync(handle, time) {
  81. return new Promise(function (resolve, reject) {
  82. setTimeout(function () {
  83. if (handle && typeof handle === 'function') {
  84. handle();
  85. }
  86. resolve();
  87. }, time);
  88. });
  89. }
  90. if ($("#progressModal").length == 0) {
  91. let phtml = `<div class="modal fade" id="progressModal" data-backdrop="static">
  92. <div class="modal-dialog" role="document">
  93. <div class="modal-content">
  94. <div class="modal-header">
  95. <h5 class="modal-title" id="progress_modal_title">${title}</h5>
  96. </div>
  97. <div class="modal-body">
  98. <!--正在生成-->
  99. <h5 class="my-3" id="progress_modal_body">正在${title}</h5>
  100. <div class="progress mb-3">
  101. <div id="progress_modal_bar" class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="75" aria-valuemin="0" aria-valuemax="100" style="width: 10%"></div>
  102. </div>
  103. </div>
  104. </div>
  105. </div>
  106. </div>`;
  107. $("body").append(phtml);
  108. } else {
  109. $("#progress_modal_title").text(title);
  110. $("#progress_modal_body").text(`正在${title}`);
  111. }
  112. $("#progress_modal_bar").css('width', '0%');
  113. $("#progressModal").modal('show');
  114. if (autoBar == true) {//假的进度条
  115. $.bootstrapLoading.progressStop = false;
  116. let width = 0;
  117. while ($.bootstrapLoading.progressStop == false) {
  118. await setTimeoutSync(null, 1000);
  119. width += 5;
  120. if (width > 90) width -= 50;
  121. $("#progress_modal_bar").css('width', `${width}%`);
  122. }
  123. }
  124. },
  125. progressEnd: function () {
  126. if ($('#progressModal').is(':visible')) {
  127. $("#progress_modal_bar").css('width', '100%');
  128. $.bootstrapLoading.progressStop = true;
  129. $("#progressModal").modal('hide');
  130. }
  131. }
  132. }