PerfectLoad.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. if(this.isLoading()){
  10. return;
  11. }
  12. var defaults = {
  13. opacity: 0.5,
  14. //loading页面透明度
  15. // backgroundColor: "#FFFFFF",
  16. //loading页面背景色
  17. borderColor: "#bbb",
  18. //提示边框颜色
  19. borderWidth: 1,
  20. //提示边框宽度
  21. borderStyle: "solid",
  22. //提示边框样式
  23. loadingTips: "Loading, please wait...",
  24. //提示文本
  25. TipsColor: "#fff",
  26. //提示颜色
  27. delayTime: 500,
  28. //页面加载完成后,加载页面渐出速度
  29. zindex: 2000,
  30. //loading页面层次
  31. sleep: 0
  32. //设置挂起,等于0时则无需挂起
  33. }
  34. var options = $.extend(defaults, options);
  35. //获取页面宽高
  36. var _PageHeight = document.documentElement.clientHeight,
  37. _PageWidth = document.documentElement.clientWidth;
  38. //在页面未加载完毕之前显示的loading Html自定义内容
  39. //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>';
  40. 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;">' +'<div class="text-green"><i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i> <span class="sr-only">Loading...</span> </div> </div></div>';
  41. //呈现loading效果
  42. $("body").append(_LoadingHtml);
  43. //获取loading提示框宽高
  44. var _LoadingTipsH = document.getElementById("loadingTips").clientHeight,
  45. _LoadingTipsW = document.getElementById("loadingTips").clientWidth;
  46. //计算距离,让loading提示框保持在屏幕上下左右居中
  47. var _LoadingTop = _PageHeight > _LoadingTipsH ? (_PageHeight - _LoadingTipsH) / 2 : 0,
  48. _LoadingLeft = _PageWidth > _LoadingTipsW ? (_PageWidth - _LoadingTipsW) / 2 : 0;
  49. $("#loadingTips").css({
  50. "left": _LoadingLeft + "px",
  51. "top": _LoadingTop + "px"
  52. });
  53. //监听页面加载状态
  54. document.onreadystatechange = PageLoaded;
  55. //当页面加载完成后执行
  56. function PageLoaded() {
  57. if (document.readyState == "complete") {
  58. var loadingMask = $('#loadingPage');
  59. setTimeout(function () {
  60. loadingMask.animate({
  61. "opacity": 0
  62. },
  63. options.delayTime,
  64. function () {
  65. $(this).hide();
  66. });
  67. },
  68. options.sleep);
  69. }
  70. }
  71. },
  72. end: function () {
  73. $("#loadingPage").remove();
  74. },
  75. progressStop:true,
  76. progressStart:async function(title="导出文件",autoBar = false){
  77. if($("#progressModal").length == 0){
  78. let phtml = `<div class="modal fade" id="progressModal" data-backdrop="static">
  79. <div class="modal-dialog" role="document">
  80. <div class="modal-content">
  81. <div class="modal-header">
  82. <h5 class="modal-title" id="progress_modal_title">${title}</h5>
  83. </div>
  84. <div class="modal-body">
  85. <!--正在生成-->
  86. <h5 class="my-3" id="progress_modal_body">正在${title}</h5>
  87. <div class="progress mb-3">
  88. <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>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. </div>`;
  94. $("body").append(phtml);
  95. }else {
  96. $("#progress_modal_title").text(title);
  97. $("#progress_modal_body").text(`正在${title}`);
  98. }
  99. $("#progress_modal_bar").css('width','0%');
  100. $("#progressModal").modal('show');
  101. if(autoBar == true){//假的进度条
  102. $.bootstrapLoading.progressStop = false;
  103. let width = 0;
  104. while ($.bootstrapLoading.progressStop == false){
  105. await setTimeoutSync(null,1000);
  106. width += 5;
  107. if(width > 90) width -= 50;
  108. $("#progress_modal_bar").css('width',`${width}%`);
  109. }
  110. }
  111. },
  112. progressEnd:function () {
  113. if ($('#progressModal').is(':visible')) {
  114. $("#progress_modal_bar").css('width','100%');
  115. $.bootstrapLoading.progressStop = true;
  116. $("#progressModal").modal('hide');
  117. }
  118. }
  119. }
  120. const SCComponent = (() => {
  121. /*
  122. * 假滚动条,调用end方法后进度直接跳到100%
  123. * 使用利用合成线程的css动画,不会被js执行阻塞。
  124. * */
  125. const InitProgressBar = (() => {
  126. function ProgressBar() {
  127. this.$modal = $('#progress');
  128. this.$title = $('#progress-title');
  129. this.$title.text('导出');
  130. this.$content = $('#progress-content');
  131. this.$content.text('正在导出...');
  132. this.$barCover = $('#progressCover');
  133. this.animationName = 'progress-cover-move';
  134. }
  135. //显示滚动条,自动处理滚动条速度
  136. ProgressBar.prototype.start = function (title, content) {
  137. this.$title.text(title);
  138. this.$content.text(content);
  139. this.$barCover.css('transform', 'translate(0)');
  140. this.$barCover.addClass(this.animationName);
  141. this.$modal.modal('show');
  142. };
  143. //结束显示滚动条,滚动条从当前位置滚到100% 消失
  144. ProgressBar.prototype.end = function () {
  145. this.$barCover.removeClass(this.animationName);
  146. this.$barCover.css('transform', 'translate(100%)');
  147. setTimeout(() => {
  148. this.$modal.modal('hide');
  149. }, 500);
  150. };
  151. return ProgressBar;
  152. })();
  153. return {InitProgressBar}
  154. })();