tui-skeleton.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <view class="tui-skeleton-cmomon tui-skeleton-box" :style="{width: winWidth+'px', height:winHeight+'px', backgroundColor:backgroundColor}">
  3. <view class="tui-skeleton-cmomon" v-for="(item,index) in skeletonElements" :key="index" :style="{width: item.width+'px', height:item.height+'px', left: item.left+'px', top: item.top+'px',backgroundColor: skeletonBgColor,borderRadius:getRadius(item.skeletonType,borderRadius)}"></view>
  4. <view class="tui-loading" :class="[getLoadingType(loadingType)]" v-if="isLoading"></view>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. name: "tuiSkeleton",
  10. props: {
  11. //选择器(外层容器)
  12. selector: {
  13. type: String,
  14. default: "tui-skeleton"
  15. },
  16. //外层容器背景颜色
  17. backgroundColor: {
  18. type: String,
  19. default: "#fff"
  20. },
  21. //骨架元素背景颜色
  22. skeletonBgColor: {
  23. type: String,
  24. default: "#e9e9e9"
  25. },
  26. //骨架元素类型:矩形,圆形,带圆角矩形["rect","circular","fillet"]
  27. //默认所有,根据页面情况进行传值
  28. //页面对应元素class为:tui-skeleton-rect,tui-skeleton-circular,tui-skeleton-fillet
  29. //如果传入的值不在下列数组中,则为自定义class值,默认按矩形渲染
  30. skeletonType: {
  31. type: Array,
  32. default () {
  33. return ["rect", "circular", "fillet"]
  34. }
  35. },
  36. //圆角值,skeletonType=fillet时生效
  37. borderRadius: {
  38. type: String,
  39. default: "16rpx"
  40. },
  41. //骨架屏预生成数据:提前生成好的数据,当传入该属性值时,则不会再次查找子节点信息
  42. preloadData: {
  43. type: Array,
  44. default () {
  45. return []
  46. }
  47. },
  48. //是否需要loading
  49. isLoading: {
  50. type: Boolean,
  51. default: true
  52. },
  53. //loading类型[1-10]
  54. loadingType: {
  55. type: Number,
  56. default: 1
  57. }
  58. },
  59. created() {
  60. const res = uni.getSystemInfoSync();
  61. this.winWidth = res.windowWidth;
  62. this.winHeight = res.windowHeight;
  63. //如果有预生成数据,则直接使用
  64. this.isPreload(true)
  65. },
  66. mounted() {
  67. this.$nextTick(() => {
  68. this.nodesRef(`.${this.selector}`).then((res) => {
  69. if(res && res[0]){
  70. this.winHeight = res[0].height + Math.abs(res[0].top)
  71. }
  72. });
  73. !this.isPreload() && this.selectorQuery()
  74. })
  75. },
  76. data() {
  77. return {
  78. winWidth: 375,
  79. winHeight: 800,
  80. skeletonElements: []
  81. };
  82. },
  83. methods: {
  84. getLoadingType: function(type) {
  85. let value = 1
  86. if (type && type > 0 && type < 11) {
  87. value = type
  88. }
  89. return 'tui-loading-' + value
  90. },
  91. getRadius: function(type, val) {
  92. let radius = "0"
  93. if (type == "circular") {
  94. radius = "50%"
  95. } else if (type == "fillet") {
  96. radius = val
  97. }
  98. return radius;
  99. },
  100. isPreload(init) {
  101. let preloadData = this.preloadData || []
  102. if (preloadData.length) {
  103. init && (this.skeletonElements = preloadData)
  104. return true
  105. }
  106. return false
  107. },
  108. async selectorQuery() {
  109. let skeletonType = this.skeletonType || []
  110. let nodes = []
  111. for (let item of skeletonType) {
  112. let className = '';
  113. // #ifndef MP-WEIXIN
  114. className = `.${item}`;
  115. if (~'rect_circular_fillet'.indexOf(item)) {
  116. className = `.${this.selector}-${item}`;
  117. }
  118. // #endif
  119. // #ifdef MP-WEIXIN
  120. className = `.${this.selector} >>> .${item}`;
  121. if (~'rect_circular_fillet'.indexOf(item)) {
  122. className = `.${this.selector} >>> .${this.selector}-${item}`;
  123. }
  124. // #endif
  125. await this.nodesRef(className).then((res) => {
  126. res.map(d => {
  127. d.skeletonType = item
  128. })
  129. nodes = nodes.concat(res)
  130. })
  131. }
  132. this.skeletonElements = nodes
  133. },
  134. async nodesRef(className) {
  135. return await new Promise((resolve, reject) => {
  136. uni.createSelectorQuery().selectAll(className).boundingClientRect((res) => {
  137. if (res) {
  138. resolve(res);
  139. } else {
  140. reject(res)
  141. }
  142. }).exec();
  143. })
  144. }
  145. }
  146. }
  147. </script>
  148. <style scoped>
  149. .tui-skeleton-cmomon {
  150. position: absolute;
  151. z-index: 99999;
  152. }
  153. .tui-skeleton-box {
  154. left: 0;
  155. top: 0;
  156. }
  157. .tui-loading {
  158. display: inline-block;
  159. vertical-align: middle;
  160. width: 40rpx;
  161. height: 40rpx;
  162. background: 0 0;
  163. border-radius: 50%;
  164. border: 2px solid;
  165. animation: tui-rotate 0.7s linear infinite;
  166. position: fixed;
  167. z-index: 999999;
  168. left: 50%;
  169. top: 50%;
  170. margin-left: -20rpx;
  171. margin-top: -20rpx;
  172. }
  173. .tui-loading-1 {
  174. border-color: #e5e5e5 #e5e5e5 #e5e5e5 #5677fc;
  175. }
  176. .tui-loading-2 {
  177. border-color: #e5e5e5 #e5e5e5 #e5e5e5 #8f8d8e;
  178. }
  179. .tui-loading-3 {
  180. border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) #fff;
  181. }
  182. .tui-loading-4 {
  183. border-color: #e5e5e5 #e5e5e5 #e5e5e5 #35b06a;
  184. }
  185. .tui-loading-5 {
  186. border-color: #e5e5e5 #e5e5e5 #e5e5e5 #fc872d;
  187. }
  188. .tui-loading-6 {
  189. border-color: #e5e5e5 #e5e5e5 #e5e5e5 #eb0909;
  190. }
  191. .tui-loading-7 {
  192. border-color: #5677fc transparent #5677fc transparent;
  193. }
  194. .tui-loading-8 {
  195. border-color: #35b06a transparent #35b06a transparent;
  196. }
  197. .tui-loading-9 {
  198. border-color: #fc872d transparent #fc872d transparent;
  199. }
  200. .tui-loading-10 {
  201. border-color: #eb0909 transparent #eb0909 transparent;
  202. }
  203. @-webkit-keyframes tui-rotate {
  204. 0% {
  205. transform: rotate(0);
  206. }
  207. 100% {
  208. transform: rotate(360deg);
  209. }
  210. }
  211. @keyframes tui-rotate {
  212. 0% {
  213. transform: rotate(0);
  214. }
  215. 100% {
  216. transform: rotate(360deg);
  217. }
  218. }
  219. </style>