loader.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. (function () {
  2. var tpl = '<div class="_loading_ flex items-center justify-center pin" style="{style}">{svg}</div>',
  3. loading = '._loading_',
  4. LOADING_SVG = [
  5. '<svg width="{width}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" class="lds-disk" style="background: none;"><g transform="translate(50,50)"><g ng-attr-transform="scale({{config.scale}})" transform="scale(0.5)"><circle cx="0" cy="0" r="50" ng-attr-fill="{{config.c1}}" fill="{color}"></circle><circle cx="0" ng-attr-cy="{{config.cy}}" ng-attr-r="{{config.r}}" ng-attr-fill="{{config.c2}}" cy="-35" r="15" fill="#ffffff" transform="rotate(101.708)"><animateTransform attributeName="transform" type="rotate" calcMode="linear" values="0 0 0;360 0 0" keyTimes="0;1" dur="1s" begin="0s" repeatCount="indefinite"></animateTransform></circle></g></g></svg>',
  6. '<svg xmlns="http://www.w3.org/2000/svg" class="mx-auto block" style="width:{width};{svg_style}" viewBox="0 0 120 30" fill="{color}"><circle cx="15" cy="15" r="15"><animate attributeName="r" from="15" to="15" begin="0s" dur="0.8s" values="15;9;15" calcMode="linear" repeatCount="indefinite"/><animate attributeName="fill-opacity" from="1" to="1" begin="0s" dur="0.8s" values="1;.5;1" calcMode="linear" repeatCount="indefinite" /></circle><circle cx="60" cy="15" r="9" fill-opacity="0.3"><animate attributeName="r" from="9" to="9" begin="0s" dur="0.8s" values="9;15;9" calcMode="linear" repeatCount="indefinite" /><animate attributeName="fill-opacity" from="0.5" to="0.5" begin="0s" dur="0.8s" values=".5;1;.5" calcMode="linear" repeatCount="indefinite" /></circle><circle cx="105" cy="15" r="15"><animate attributeName="r" from="15" to="15" begin="0s" dur="0.8s" values="15;9;15" calcMode="linear" repeatCount="indefinite" /><animate attributeName="fill-opacity" from="1" to="1" begin="0s" dur="0.8s" values="1;.5;1" calcMode="linear" repeatCount="indefinite" /></circle></svg>',
  7. ];
  8. /**
  9. * Loading
  10. *
  11. * @param opts
  12. * @constructor
  13. */
  14. function Loader(opts) {
  15. var defStyle = 'position:absolute;left:10px;right:10px;', content, $container;
  16. opts = $.extend({
  17. container: '#pjax-container',
  18. z_index: 100,
  19. width: '50px',
  20. color: 'var(--primary-60)',
  21. bg: '#fff',
  22. style: '',
  23. svg: LOADING_SVG[0]
  24. }, opts);
  25. $container = opts.container;
  26. $container = $container == 'object' ? $container : $($container);
  27. content = $(
  28. tpl
  29. .replace('{svg}', opts.svg)
  30. .replace('{color}', opts.color)
  31. .replace('{color}', opts.color)
  32. .replace('{width}', opts.width)
  33. .replace('{style}', defStyle + 'background:' + opts.bg + ';' + 'z-index:' + opts.z_index + ';' + opts.style)
  34. );
  35. content.appendTo($container);
  36. this.remove = function () {
  37. $container.find(loading).remove();
  38. };
  39. }
  40. Loader.destroyAll = function () {
  41. $(loading).remove();
  42. };
  43. LA.Loader = Loader;
  44. // 全屏居中loading
  45. LA.loading = function (opts) {
  46. if (opts === false) {
  47. // 关闭loading
  48. return setTimeout(LA.Loader.destroyAll, 70);
  49. }
  50. // 配置参数
  51. opts = $.extend({
  52. color: 'var(--primary-80)',
  53. z_index: 999991014,
  54. width: '58px',
  55. shade: 'rgba(255, 255, 255, 0.02)',
  56. top: 200,
  57. svg: LOADING_SVG[1],
  58. }, opts);
  59. var win = $(window),
  60. // 容器
  61. $container = $('<div class="_loading_" type="loading" times="1" showtime="0" contype="string" style="z-index:'+opts.z_index+';width:300px;position:fixed"></div>'),
  62. // 遮罩层直接沿用layer
  63. shadow = $('<div class="layui-layer-shade _loading_" style="z-index:'+(opts.z_index-2)+'; background-color:'+opts.shade+'"></div>');
  64. $container.appendTo('body');
  65. if (opts.shade) {
  66. shadow.appendTo('body');
  67. }
  68. function resize() {
  69. $container.css({
  70. left: (win.width() - 300)/2,
  71. top: (win.height() - opts.top)/2
  72. });
  73. }
  74. // 自适应窗口大小
  75. win.on('resize', resize);
  76. resize();
  77. $container.loading(opts);
  78. };
  79. $.fn.loading = function (opt) {
  80. if (opt === false) {
  81. return $(this).find(loading).remove();
  82. }
  83. opt = opt || {};
  84. opt.container = $(this);
  85. return new Loader(opt);
  86. };
  87. })();