loading.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * 脚本名称:页面加载Loading脚本
  3. * 脚本说明:
  4. * 1.因为使用到document.body.scrollHeight对象,脚本引入时,脚本应放到body内,否则报错;
  5. * 2.loadingImage为loading的图片,您可以替换为您想要的图片;loading弹窗的样式可以通过代码修改;
  6. * 3.使用方法:弹出遮罩:whir.loading.add("加载中,请稍候", 1);扔掉遮罩: whir.loading.remove();
  7. * 脚本作者:zhangqs008@163.com
  8. * 更新说明:(haoyanbing 20171225)
  9. * 1. 使用CSS样式替换图片,需要引入loading.css
  10. * 2. 使用方法Loading.loading.show();Loading.loading.hide();
  11. */
  12. var Loading = window.Loading || {};
  13. Loading.loading =
  14. {
  15. show: function () {
  16. var arr = this.getPageSize();
  17. var width = parseInt(arr[2]);
  18. var height = parseInt(arr[3]);
  19. //背景遮罩
  20. var mask = document.createElement("div");
  21. mask.id = 'mask';
  22. mask.style.position = 'fixed';
  23. mask.style.left = '0';
  24. mask.style.top = '0';
  25. mask.style.width = '100%';
  26. mask.style.height = parseInt(arr[1]) + "px";
  27. mask.style.background = "#000";
  28. mask.style.opacity = 0.3;
  29. mask.style.zIndex = "10000";
  30. mask.addEventListener('touchstart', function (e) { e.preventDefault(); }, false); //触摸事件
  31. mask.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); //滑动事件
  32. mask.addEventListener('touchend', function (e) { e.preventDefault(); }, false); //离开元素事件
  33. document.body.appendChild(mask);
  34. //提示文本
  35. var loading = document.createElement("div");
  36. loading.id = 'loading';
  37. var spinner = '<div class="spinner">'
  38. +'<div class="bar1"></div>'
  39. +'<div class="bar2"></div>'
  40. +'<div class="bar3"></div>'
  41. +'<div class="bar4"></div>'
  42. +'<div class="bar5"></div>'
  43. +'<div class="bar6"></div>'
  44. +'<div class="bar7"></div>'
  45. +'<div class="bar8"></div>'
  46. +'<div class="bar9"></div>'
  47. +'<div class="bar10"></div>'
  48. +'<div class="bar11"></div>'
  49. +'<div class="bar12"></div>'
  50. +'</div>';
  51. loading.innerHTML = spinner;
  52. loading.style.position = 'absolute';
  53. loading.style.left = ((width / 2) - 25) + "px";
  54. loading.style.top = (height / 2 - 25) + "px";
  55. loading.style.width = '50px';
  56. loading.style.height = "50px";
  57. loading.style.display = "inline-block";
  58. loading.style.padding = "0px 0px 0px 0px";
  59. loading.style.fontSize = " 1em";
  60. loading.style.fontFamily = " initial";
  61. loading.style.zIndex = "100001";
  62. // loading.style.background = "url(" + loadingImage + ") no-repeat fixed center";
  63. document.body.appendChild(loading);
  64. },
  65. hide: function () {
  66. var element = document.getElementById("mask");
  67. if(element){
  68. element.parentNode.removeChild(element);
  69. element = document.getElementById("loading");
  70. element.parentNode.removeChild(element);
  71. }
  72. },
  73. getPageSize: function () {
  74. var xScroll, yScroll;
  75. if (window.innerHeight && window.scrollMaxY) {
  76. xScroll = window.innerWidth + window.scrollMaxX;
  77. yScroll = window.innerHeight + window.scrollMaxY;
  78. } else {
  79. if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
  80. xScroll = document.body.scrollWidth;
  81. yScroll = document.body.scrollHeight;
  82. } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  83. xScroll = document.body.offsetWidth;
  84. yScroll = document.body.offsetHeight;
  85. }
  86. }
  87. var windowWidth = 0;
  88. var windowHeight = 0;
  89. var pageHeight = 0;
  90. var pageWidth = 0;
  91. if (self.innerHeight) { // all except Explorer
  92. if (document.documentElement.clientWidth) {
  93. windowWidth = document.documentElement.clientWidth;
  94. } else {
  95. windowWidth = self.innerWidth;
  96. }
  97. windowHeight = self.innerHeight;
  98. } else {
  99. if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  100. windowWidth = document.documentElement.clientWidth;
  101. windowHeight = document.documentElement.clientHeight;
  102. } else {
  103. if (document.body) { // other Explorers
  104. windowWidth = document.body.clientWidth;
  105. windowHeight = document.body.clientHeight;
  106. }
  107. }
  108. }
  109. // for small pages with total height less then height of the viewport
  110. if (yScroll < windowHeight) {
  111. pageHeight = windowHeight;
  112. } else {
  113. pageHeight = yScroll;
  114. }
  115. // for small pages with total width less then width of the viewport
  116. if (xScroll < windowWidth) {
  117. pageWidth = xScroll;
  118. } else {
  119. pageWidth = windowWidth;
  120. }
  121. var arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
  122. return arrayPageSize;
  123. }
  124. };