PerfectLoad.js 6.0 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. 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: 999,
  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. }
  76. const SCComponent = (() => {
  77. /*
  78. * 假滚动条,0 - 80% 快, 80% - 95%很慢,95%开始停住,直到调用end方法
  79. * */
  80. const InitProgressBar = (() => {
  81. function ProgressBar($modal = $('#progress'), $title = $('#progress-title'),
  82. $content = $('#progress-content'), $bar = $('#progressBar')) {
  83. this.$modal = $modal;
  84. this.$title = $title;
  85. this.$content = $content;
  86. this.$bar = $bar;
  87. }
  88. //显示滚动条,自动处理滚动条速度
  89. ProgressBar.prototype.start = function (title, content) {
  90. this.$title.text(title);
  91. this.$content.text(content);
  92. this.$bar.css('width', `0%`);
  93. this.$modal.modal('show');
  94. this.outer = setInterval(() => {
  95. let curWidth = parseInt(this.$bar[0].style.width.replace('%', ''));
  96. curWidth = parseInt(curWidth + 2);
  97. this.$bar.css('width', `${curWidth}%`);
  98. if (curWidth >= 80) {
  99. clearInterval(this.outer);
  100. }
  101. }, 100);
  102. this.inner = setInterval(() => {
  103. let curWidth = parseFloat(this.$bar[0].style.width.replace('%', ''));
  104. if (curWidth >= 80) {
  105. curWidth = parseFloat(curWidth + 0.1);
  106. this.$bar.css('width', `${curWidth}%`);
  107. if (curWidth >= 95) {
  108. clearInterval(this.inner);
  109. }
  110. }
  111. }, 500);
  112. };
  113. //结束显示滚动条,滚动条从当前位置滚到100% 消失
  114. ProgressBar.prototype.end = function () {
  115. if (this.outer) {
  116. clearInterval(this.outer);
  117. }
  118. if (this.inner) {
  119. clearInterval(this.inner);
  120. }
  121. $('#progressBar').css('width', '100%');
  122. setTimeout(() => {
  123. this.$modal.modal('hide');
  124. }, 500);
  125. };
  126. return ProgressBar;
  127. })();
  128. return {InitProgressBar}
  129. })();