loading.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. 加载动画插件
  3. version: 1.1.0
  4. author: 顾晨
  5. github: https://github.com/webdick/loadingJS
  6. 建议为需隐藏的元素添加 style="display: none;" 这样效果会达到最好
  7. el:'#load',//绑定元素容器
  8. hide:'#app',//隐藏元素选择器 在加载完成前将该选择器下的元素全部进行隐藏 加载完成后再进行显示 起到防止未加载完却可以滚动的问题 可留空
  9. bindClass:'.load',//如果只想预加载有指定类名的图片就写这 不加则默认是自动预加载网页中的所有img
  10. wait:10000,//加载超时时间 默认10000毫秒
  11. animateTime:'1.5s',//动画淡入淡出时间 默认1.5s 建议不要超过delTime的时间 否则动画未加载完元素就会被彻底删除
  12. delTime:3000,//完全删除动画元素时间(包括容器) 默认3000毫秒
  13. callback:()=>{
  14. // 加载完成回调
  15. }
  16. */
  17. ; (function () {
  18. class Load {
  19. constructor(conf) {
  20. // 获取元素
  21. this.$el = conf.el.nodeType === 1 ? conf.el : document.querySelector(conf.el);
  22. this.$model = conf.model ? conf.model == "model" ? conf.model : "auto" : "auto";
  23. // 重载信息等待时间
  24. this.$wait = conf.wait ? conf.wait : 10000;
  25. // 绑定类
  26. // this.$bindClass = conf.bindClass ? conf.bindClass : 'img[src]';
  27. this.$bindClass = conf.bindClass ? conf.bindClass : '';
  28. // 回调事件
  29. this.$callback = conf.callback;
  30. // 动画时长
  31. this.$animateTime = conf.animateTime ? conf.animateTime : 1500;
  32. // 彻底删除时间
  33. this.$delTime = conf.delTime ? conf.delTime : 3000;
  34. // 如果目标元素存在
  35. if (this.$el) {
  36. // 隐藏其他元素
  37. this.$hide = document.querySelector(conf.hide);
  38. if (this.$hide) {
  39. this.$hide.style.display = "none";
  40. }
  41. // 为目标添加元素
  42. this.addHtml(this.$el);
  43. // 初始化进度和全局图片元素
  44. this.initData();
  45. }
  46. }
  47. // 初始化加载
  48. initData() {
  49. // // 读取页面所有图片
  50. // var tags = document.querySelectorAll(this.$bindClass),
  51. // loadingText = document.querySelector('#loading_tesxt > span'),
  52. // tagsLen = tags.length,
  53. // progress = 0,
  54. // pices = 100 / tagsLen;
  55. // // 如果未查询到目标图片
  56. // if (tagsLen === 0) {
  57. // console.log('预加载图片未命中')
  58. // loadingText.innerHTML = "100%";
  59. // this.loadOut();
  60. // return;
  61. // }
  62. // // 遍历图片
  63. // tags.forEach((v, i) => {
  64. // var img = new Image();
  65. // img.src = v.src;
  66. // // 每当图片载入成功
  67. // img.onload = () => {
  68. // progress += pices;
  69. // loadingText.innerHTML = Math.floor(progress) + "%";
  70. // // 当图片全部载入完成
  71. // if (Math.floor(progress) >= 99) {
  72. // console.log('预加载命中' + tagsLen + '张图片')
  73. // this.loadOut();
  74. // return;
  75. // }
  76. // }
  77. // });
  78. // 超出等待时间
  79. setTimeout(() => {
  80. // 如果等待时间后仍处于加载状态则显示重载
  81. if (document.querySelector("#loading_container")) {
  82. document.querySelector("#loading_slow").classList.add("fadeIn");
  83. // console.log(document.querySelector("#loading_slow"))
  84. }
  85. }, this.$wait);
  86. }
  87. // 加载结束
  88. loadOut() {
  89. // 添加淡出动画
  90. document.querySelector("#loading_container").classList.add("fadeOut");
  91. if (this.$hide) {
  92. this.$hide.style.display = "block";
  93. }
  94. // 回调延迟
  95. setTimeout(() => {
  96. // 进行回调
  97. if (this.$callback)
  98. this.$callback();
  99. }, 300)
  100. // 删除元素
  101. setTimeout(() => {
  102. this.$el.parentNode.removeChild(this.$el);
  103. }, this.$delTime)
  104. return;
  105. }
  106. // 添加HTML
  107. addHtml(el) {
  108. // 引入css
  109. var css = this.addCss();
  110. // 引入动画
  111. var animate = this.addAnimate();
  112. // 插入html
  113. el.innerHTML = `
  114. <!-- style="display:none;"-->
  115. <div id="loading_container" >
  116. <div id="loading_circle">
  117. <span id="outer">
  118. <span id="loading_tesxt">
  119. <span></span>
  120. </span>
  121. <span id="inner"></span>
  122. <span id="inner2"></span>
  123. <span id="inner3"></span>
  124. </span>
  125. <div id="load">
  126. <!-- L&nbsp;&nbsp;o&nbsp;&nbsp;a&nbsp;&nbsp;d&nbsp;&nbsp;i&nbsp;&nbsp;n&nbsp;&nbsp;g -->
  127. 等&nbsp;&nbsp;待&nbsp;&nbsp;中&nbsp;&nbsp;,&nbsp;&nbsp;请&nbsp;&nbsp;稍&nbsp;&nbsp;后
  128. </div>
  129. <div id="loading_slow">
  130. 网速有点慢,请继续等待 或 <a href="" click="location.reload()">重载</a> 网页
  131. </div>
  132. </div>
  133. </div>
  134. <style>
  135. ${css + animate}
  136. </style>
  137. `;
  138. }
  139. // 添加css
  140. addCss() {
  141. return `
  142. div#loading_container {
  143. position: fixed;
  144. top: 0px;
  145. left: 0px;
  146. z-index: 1000;
  147. width: 100%;
  148. height: 100%;
  149. padding: 0;
  150. margin: 0;
  151. text-align: center;
  152. background-color: #00000080;
  153. }
  154. div#loading_circle {
  155. position: relative;
  156. width: 85%;
  157. margin: 20% auto;
  158. text-align: center;
  159. color: #FFFFFF;
  160. opacity: 0.9;
  161. text-transform: uppercase;
  162. /*font: normal 12px/14px "ff-basic-gothic-web-pro",Arial,Helvetica,sans-serif;*/
  163. font-family: "微软雅黑";
  164. font-size: 16px;
  165. height: 60%;
  166. /*letter-spacing:7px;*/
  167. }
  168. div#loading_circle span#outer {
  169. width: 50px;
  170. height: 50px;
  171. -webkit-border-radius: 15px;
  172. -moz-border-radius: 15px;
  173. border-radius: 50%;
  174. display: block;
  175. /*background:rgba(85,85,85,0.2);*/
  176. margin: 0 auto 20px;
  177. position: relative;
  178. border: 4px solid rgba(255, 255, 255, 0.5)
  179. }
  180. div#loading_circle span#outer span#loading_tesxt {
  181. width: 100%;
  182. height: 100%;
  183. position: absolute;
  184. top: 0;
  185. left: 0;
  186. text-align: center;
  187. line-height: 50px;
  188. }
  189. div#loading_circle span#outer span#loading_tesxt> span {
  190. display: block;
  191. }
  192. div#loading_circle > .logoimg{
  193. width: 100%;
  194. text-align: center;
  195. }
  196. div#loading_circle > .logoimg img{
  197. height: 100%;
  198. margin-bottom: 10px;
  199. display: inline-block;
  200. }
  201. div#loading_circle span#outer {
  202. /*-webkit-animation: throbber 1.5s infinite;
  203. animation: throbber 1.5s infinite;*/
  204. }
  205. div#loading_circle span#outer span#inner {
  206. width: 50px;
  207. height: 50px;
  208. display: block;
  209. position: absolute;
  210. -webkit-animation: xuanzhuan 1s infinite linear;
  211. animation: xuanzhuan 1s infinite linear;
  212. -webkit-animation-fill-mode: both;
  213. animation-fill-mode: both
  214. }
  215. div#loading_circle span#outer span#inner:after {
  216. content: "";
  217. width: 10px;
  218. height: 10px;
  219. -webkit-border-radius: 10px;
  220. -moz-border-radius: 10px;
  221. border-radius: 50%;
  222. display: block;
  223. background: rgb(255,255,255);
  224. position: absolute;
  225. left: 10px;
  226. top: -3px;
  227. }
  228. div#loading_circle span#outer span#inner2 {
  229. width: 50px;
  230. height: 50px;
  231. display: block;
  232. position: absolute;
  233. -webkit-animation: xuanzhuan 1.5s infinite linear reverse;
  234. animation: xuanzhuan 1.5s infinite linear reverse;
  235. }
  236. div#loading_circle span#outer span#inner2:after {
  237. content: "";
  238. width: 10px;
  239. height: 10px;
  240. -webkit-border-radius: 10px;
  241. -moz-border-radius: 10px;
  242. border-radius: 50%;
  243. display: block;
  244. background: rgb(255,255,255);
  245. position: absolute;
  246. left: 10px;
  247. bottom: -3px;
  248. }
  249. div#loading_circle span#outer span#inner3 {
  250. width: 50px;
  251. height: 50px;
  252. display: block;
  253. position: absolute;
  254. -webkit-animation: xuanzhuan 2s infinite linear;
  255. animation: xuanzhuan 2s infinite linear;
  256. }
  257. div#loading_circle span#outer span#inner3:after {
  258. content: "";
  259. width: 10px;
  260. height: 10px;
  261. -webkit-border-radius: 10px;
  262. -moz-border-radius: 10px;
  263. border-radius: 50%;
  264. display: block;
  265. background: rgb(255,255,255);
  266. position: absolute;
  267. right: -5px;
  268. top: 20px;
  269. }
  270. div#loading_circle div#loading_slow {
  271. display: none;
  272. margin: 20px 0 0;
  273. letter-spacing: 0;
  274. text-transform: none;
  275. font: normal 12px/14px "cronos-pro-display", helvetica, arial, sans-serif;
  276. font-family: "微软雅黑";
  277. }
  278. div#loading_circle div#loading_slow a {
  279. text-decoration: none;
  280. color: rgba(183, 47, 47, 0.7);
  281. }
  282. div#loading_circle div#loading_slow a:hover {
  283. color: rgba(183, 47, 47, 0.7);
  284. }
  285. `;
  286. }
  287. // 添加动画
  288. addAnimate() {
  289. var time = this.$animateTime;
  290. return `
  291. @-webkit-keyframes fadeOut {
  292. from {
  293. opacity: 1;
  294. }
  295. to {
  296. opacity: 0;
  297. }
  298. }
  299. @keyframes fadeOut {
  300. from {
  301. opacity: 1;
  302. }
  303. to {
  304. opacity: 0;
  305. }
  306. }
  307. .fadeOut {
  308. -webkit-animation-duration: ${time};
  309. animation-duration: ${time};
  310. -webkit-animation-fill-mode: both;
  311. animation-fill-mode: both;
  312. -webkit-animation-name: fadeOut;
  313. animation-name: fadeOut;
  314. }
  315. @-webkit-keyframes fadeIn {
  316. from {
  317. opacity: 0;
  318. }
  319. to {
  320. opacity: 1;
  321. }
  322. }
  323. @keyframes fadeIn {
  324. from {
  325. opacity: 0;
  326. }
  327. to {
  328. opacity: 1;
  329. }
  330. }
  331. .fadeIn {
  332. display:block!important;
  333. -webkit-animation-duration: ${time};
  334. animation-duration: ${time};
  335. -webkit-animation-fill-mode: both;
  336. animation-fill-mode: both;
  337. -webkit-animation-name: fadeIn;
  338. animation-name: fadeIn;
  339. }
  340. @-webkit-keyframes throbber {
  341. 0% {
  342. -webkit-transform: scale3d(1, 1, 1);
  343. transform: scale3d(1, 1, 1);
  344. }
  345. 50% {
  346. -webkit-transform: scale3d(1.2, 1.2, 1.2);
  347. transform: scale3d(1.2, 1.2, 1.2);
  348. }
  349. 100% {
  350. -webkit-transform: scale3d(1, 1, 1);
  351. transform: scale3d(1, 1, 1);
  352. }
  353. }
  354. @keyframes throbber {
  355. 0% {
  356. -webkit-transform: scale3d(1, 1, 1);
  357. transform: scale3d(1, 1, 1);
  358. }
  359. 50% {
  360. -webkit-transform: scale3d(1.2, 1.2, 1.2);
  361. transform: scale3d(1.2, 1.2, 1.2);
  362. }
  363. 100% {
  364. -webkit-transform: scale3d(1, 1, 1);
  365. transform: scale3d(1, 1, 1);
  366. }
  367. }
  368. @-webkit-keyframes xuanzhuan {
  369. 0% {
  370. -webkit-transform: rotate3d(0, 0, 1, 0deg);
  371. transform: rotate3d(0, 0, 1, 0deg);
  372. }
  373. 10% {
  374. -webkit-transform: rotate3d(0, 0, 1, 36deg);
  375. transform: rotate3d(0, 0, 1, 36deg);
  376. }
  377. 20% {
  378. -webkit-transform: rotate3d(0, 0, 1, 72deg);
  379. transform: rotate3d(0, 0, 1, 72deg);
  380. }
  381. 30% {
  382. -webkit-transform: rotate3d(0, 0, 1, 108deg);
  383. transform: rotate3d(0, 0, 1, 108deg);
  384. }
  385. 40% {
  386. -webkit-transform: rotate3d(0, 0, 1, 144deg);
  387. transform: rotate3d(0, 0, 1, 144deg);
  388. }
  389. 50% {
  390. -webkit-transform: rotate3d(0, 0, 1, 180deg);
  391. transform: rotate3d(0, 0, 1, 180deg);
  392. }
  393. 60% {
  394. -webkit-transform: rotate3d(0, 0, 1, 216deg);
  395. transform: rotate3d(0, 0, 1, 216deg);
  396. }
  397. 70% {
  398. -webkit-transform: rotate3d(0, 0, 1, 252deg);
  399. transform: rotate3d(0, 0, 1, 252deg);
  400. }
  401. 80% {
  402. -webkit-transform: rotate3d(0, 0, 1, 288deg);
  403. transform: rotate3d(0, 0, 1, 288deg);
  404. }
  405. 90% {
  406. -webkit-transform: rotate3d(0, 0, 1, 324deg);
  407. transform: rotate3d(0, 0, 1, 324deg);
  408. }
  409. 100% {
  410. -webkit-transform: rotate3d(0, 0, 1, 360deg);
  411. transform: rotate3d(0, 0, 1, 360deg);
  412. }
  413. }
  414. @keyframes xuanzhuan {
  415. 0% {
  416. -webkit-transform: rotate3d(0, 0, 1, 0deg);
  417. transform: rotate3d(0, 0, 1, 0deg);
  418. }
  419. 10% {
  420. -webkit-transform: rotate3d(0, 0, 1, 36deg);
  421. transform: rotate3d(0, 0, 1, 36deg);
  422. }
  423. 20% {
  424. -webkit-transform: rotate3d(0, 0, 1, 72deg);
  425. transform: rotate3d(0, 0, 1, 72deg);
  426. }
  427. 30% {
  428. -webkit-transform: rotate3d(0, 0, 1, 108deg);
  429. transform: rotate3d(0, 0, 1, 108deg);
  430. }
  431. 40% {
  432. -webkit-transform: rotate3d(0, 0, 1, 144deg);
  433. transform: rotate3d(0, 0, 1, 144deg);
  434. }
  435. 50% {
  436. -webkit-transform: rotate3d(0, 0, 1, 180deg);
  437. transform: rotate3d(0, 0, 1, 180deg);
  438. }
  439. 60% {
  440. -webkit-transform: rotate3d(0, 0, 1, 216deg);
  441. transform: rotate3d(0, 0, 1, 216deg);
  442. }
  443. 70% {
  444. -webkit-transform: rotate3d(0, 0, 1, 252deg);
  445. transform: rotate3d(0, 0, 1, 252deg);
  446. }
  447. 80% {
  448. -webkit-transform: rotate3d(0, 0, 1, 288deg);
  449. transform: rotate3d(0, 0, 1, 288deg);
  450. }
  451. 90% {
  452. -webkit-transform: rotate3d(0, 0, 1, 324deg);
  453. transform: rotate3d(0, 0, 1, 324deg);
  454. }
  455. 100% {
  456. -webkit-transform: rotate3d(0, 0, 1, 360deg);
  457. transform: rotate3d(0, 0, 1, 360deg);
  458. }
  459. }
  460. `;
  461. }
  462. }
  463. // 暴露入口类
  464. window.Load = Load
  465. })();