合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.1 KiB

1 week ago
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>lime-svg</title>
  8. <style>
  9. html,
  10. body,
  11. img,
  12. div {
  13. padding: 0;
  14. margin: 0;
  15. width: 100%;
  16. height: 100%;
  17. position: relative;
  18. overflow: hidden;
  19. pointer-events: none;
  20. }
  21. html,
  22. body {
  23. background-color: transparent;
  24. }
  25. #lime-icon {
  26. transition: all 100ms;
  27. }
  28. #lime-icon.is-inherit {
  29. -webkit-mask-image: var(--svg);
  30. mask-image: var(--svg);
  31. -webkit-mask-repeat: no-repeat;
  32. mask-repeat: no-repeat;
  33. -webkit-mask-size: 100% 100%;
  34. mask-size: 100% 100%;
  35. background-color: var(--color, transparent);
  36. /* 设置背景颜色为--color的值,默认为透明 */
  37. /* background-blend-mode: multiply; */
  38. }
  39. #lime-icon:not(.is-inherit) {
  40. background: var(--svg) no-repeat;
  41. background-size: 100% 100%;
  42. background-color: transparent;
  43. }
  44. #lime-image {
  45. mix-blend-mode: lighten;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div id="lime-icon">
  51. <img id="lime-image" />
  52. </div>
  53. <script type="text/javascript" src="uni.webview.1.5.5.js"></script>
  54. <script>
  55. const image = document.getElementById('lime-image');
  56. const icon = document.getElementById('lime-icon');
  57. let lastSrc = ''
  58. function setSrc(src) {
  59. if (lastSrc == src) return
  60. lastSrc = src;
  61. const _src = src.split('<svg')
  62. if (_src.length > 1) {
  63. src = (_src[0] == '' ? 'data:image/svg+xml,' : _src[0]) + encodeURIComponent('<svg' + _src[1])
  64. }
  65. // src = src.replace(/#/g, '%23').replace(/"/g, `'`)
  66. image.src = src
  67. // icon.style.setProperty('--svg', `url('${src}')`);
  68. icon.style.setProperty('--svg', `url("${src}")`);
  69. // icon.style.setProperty('--svg', `url(${src})`);
  70. image.onload = (e) => {
  71. emit('load', {
  72. type: "load",
  73. timeStamp: e.timeStamp,
  74. detail: {
  75. width: image.naturalWidth,
  76. height: image.naturalHeight,
  77. }
  78. })
  79. }
  80. image.onerror = (e) => {
  81. emit('error', {
  82. type: "error",
  83. timeStamp: e.timeStamp,
  84. detail: {
  85. width: image.naturalWidth,
  86. height: image.naturalHeight,
  87. }
  88. })
  89. }
  90. }
  91. function setStyle(style) {
  92. if (typeof style === 'object') {
  93. for (let [key, value] of Object.entries(style)) {
  94. if (key == '--color') {
  95. if (value) {
  96. icon.classList.add('is-inherit')
  97. } else {
  98. value = 'black'
  99. }
  100. }
  101. icon.style.setProperty(key, value);
  102. }
  103. }
  104. }
  105. function emit(event, data = {}) {
  106. postMessage({
  107. event,
  108. data
  109. });
  110. };
  111. function postMessage(data) {
  112. uni.webView.postMessage({data})
  113. // if (window.__uniapp_x_) {
  114. // window.__uniapp_x_.postMessage(JSON.stringify(data))
  115. // } else if (uni.webView.postMessage) {
  116. // uni.webView.postMessage({
  117. // data
  118. // })
  119. // }
  120. };
  121. // setStyle({
  122. // color: 'red',
  123. // })
  124. // setSrc('https://api.iconify.design/bi/bell-fill.svg')
  125. // 禁用所有事件
  126. document.addEventListener('mousedown', function(event) {
  127. event.stopImmediatePropagation();
  128. event.preventDefault();
  129. event.stopPropagation();
  130. });
  131. document.addEventListener('mouseup', function(event) {
  132. event.stopImmediatePropagation();
  133. event.preventDefault();
  134. event.stopPropagation();
  135. });
  136. document.addEventListener('click', function(event) {
  137. event.stopImmediatePropagation();
  138. event.preventDefault();
  139. event.stopPropagation();
  140. emit('click')
  141. });
  142. document.addEventListener('keydown', function(event) {
  143. event.stopImmediatePropagation();
  144. event.preventDefault();
  145. event.stopPropagation();
  146. });
  147. document.addEventListener('keyup', function(event) {
  148. event.stopImmediatePropagation();
  149. event.preventDefault();
  150. event.stopPropagation();
  151. });
  152. document.addEventListener('keypress', function(event) {
  153. event.stopImmediatePropagation();
  154. event.preventDefault();
  155. event.stopPropagation();
  156. });
  157. </script>
  158. </body>
  159. </html>