/** * Created by chen on 2017/10/31. */ jQuery.bootstrapLoading = { isLoading: function () { return $('#loadingPage').is(':visible'); }, start: function (options) { var defaults = { opacity: 0.5, //loading页面透明度 // backgroundColor: "#FFFFFF", //loading页面背景色 borderColor: "#bbb", //提示边框颜色 borderWidth: 1, //提示边框宽度 borderStyle: "solid", //提示边框样式 loadingTips: "Loading, please wait...", //提示文本 TipsColor: "#fff", //提示颜色 delayTime: 500, //页面加载完成后,加载页面渐出速度 zindex: 2000, //loading页面层次 sleep: 0 //设置挂起,等于0时则无需挂起 } var options = $.extend(defaults, options); //获取页面宽高 var _PageHeight = document.documentElement.clientHeight, _PageWidth = document.documentElement.clientWidth; //在页面未加载完毕之前显示的loading Html自定义内容 //var _LoadingHtml = '
' + options.loadingTips + '
'; var _LoadingHtml = '
' + '
Loading...
'; //呈现loading效果 $("body").append(_LoadingHtml); //获取loading提示框宽高 var _LoadingTipsH = document.getElementById("loadingTips").clientHeight, _LoadingTipsW = document.getElementById("loadingTips").clientWidth; //计算距离,让loading提示框保持在屏幕上下左右居中 var _LoadingTop = _PageHeight > _LoadingTipsH ? (_PageHeight - _LoadingTipsH) / 2 : 0, _LoadingLeft = _PageWidth > _LoadingTipsW ? (_PageWidth - _LoadingTipsW) / 2 : 0; $("#loadingTips").css({ "left": _LoadingLeft + "px", "top": _LoadingTop + "px" }); //监听页面加载状态 document.onreadystatechange = PageLoaded; //当页面加载完成后执行 function PageLoaded() { if (document.readyState == "complete") { var loadingMask = $('#loadingPage'); setTimeout(function () { loadingMask.animate({ "opacity": 0 }, options.delayTime, function () { $(this).hide(); }); setTimeout(function () { const $loadingFocus = $('#loadingFocus')[0]; if ($loadingFocus) { $loadingFocus.focus(); } }, 200); }, options.sleep); } } }, end: function () { $("#loadingPage").remove(); }, progressStop: true, progressStart: async function (title = "导出文件", autoBar = false) { function setTimeoutSync(handle, time) { return new Promise(function (resolve, reject) { setTimeout(function () { if (handle && typeof handle === 'function') { handle(); } resolve(); }, time); }); } if ($("#progressModal").length == 0) { let phtml = ``; $("body").append(phtml); } else { $("#progress_modal_title").text(title); $("#progress_modal_body").text(`正在${title}`); } $("#progress_modal_bar").css('width', '0%'); $("#progressModal").modal('show'); if (autoBar == true) {//假的进度条 $.bootstrapLoading.progressStop = false; let width = 0; while ($.bootstrapLoading.progressStop == false) { await setTimeoutSync(null, 1000); width += 5; if (width > 90) width -= 50; $("#progress_modal_bar").css('width', `${width}%`); } } }, progressEnd: function () { if ($('#progressModal').is(':visible')) { $("#progress_modal_bar").css('width', '100%'); $.bootstrapLoading.progressStop = true; $("#progressModal").modal('hide'); } } }