tui-skeleton.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // #ifdef H5
  67. mounted() {
  68. this.$nextTick(() => {
  69. this.nodesRef(`.${this.selector}`).then((res) => {
  70. this.winHeight = res[0].height + Math.abs(res[0].top)
  71. });
  72. !this.isPreload() && this.selectorQuery()
  73. })
  74. },
  75. // #endif
  76. onReady() {
  77. this.nodesRef(`.${this.selector}`).then((res) => {
  78. this.winHeight = res[0].height + Math.abs(res[0].top)
  79. });
  80. !this.isPreload() && this.selectorQuery()
  81. },
  82. data() {
  83. return {
  84. winWidth: 375,
  85. winHeight: 800,
  86. skeletonElements: []
  87. };
  88. },
  89. methods: {
  90. getLoadingType: function(type) {
  91. let value = 1
  92. if (type && type > 0 && type < 11) {
  93. value = type
  94. }
  95. return 'tui-loading-' + value
  96. },
  97. getRadius: function(type, val) {
  98. let radius = "0"
  99. if (type == "circular") {
  100. radius = "50%"
  101. } else if (type == "fillet") {
  102. radius = val
  103. }
  104. return radius;
  105. },
  106. isPreload(init) {
  107. let preloadData = this.preloadData || []
  108. if (preloadData.length) {
  109. init && (this.skeletonElements = preloadData)
  110. return true
  111. }
  112. return false
  113. },
  114. async selectorQuery() {
  115. let skeletonType = this.skeletonType || []
  116. let nodes = []
  117. for (let item of skeletonType) {
  118. let className = `.${this.selector} >>> .${item}`
  119. // #ifdef H5
  120. className = `.${item}`
  121. // #endif
  122. if (~"rect_circular_fillet".indexOf(item)) {
  123. // #ifndef H5
  124. className = `.${this.selector} >>> .${this.selector}-${item}`
  125. // #endif
  126. // #ifdef H5
  127. className = `.${this.selector}-${item}`
  128. // #endif
  129. }
  130. await this.nodesRef(className).then((res) => {
  131. res.map(d => {
  132. d.skeletonType = item
  133. })
  134. nodes = nodes.concat(res)
  135. })
  136. }
  137. this.skeletonElements = nodes
  138. },
  139. async nodesRef(className) {
  140. return await new Promise((resolve, reject) => {
  141. uni.createSelectorQuery().selectAll(className).boundingClientRect((res) => {
  142. if (res) {
  143. resolve(res);
  144. } else {
  145. reject(res)
  146. }
  147. }).exec();
  148. })
  149. }
  150. }
  151. }
  152. </script>
  153. <style>
  154. .tui-skeleton-cmomon {
  155. position: absolute;
  156. z-index: 99999;
  157. overflow: hidden;
  158. }
  159. .tui-skeleton-box {
  160. left: 0;
  161. top: 0;
  162. }
  163. .tui-loading {
  164. display: inline-block;
  165. vertical-align: middle;
  166. width: 40rpx;
  167. height: 40rpx;
  168. background: 0 0;
  169. border-radius: 50%;
  170. border: 2px solid;
  171. animation: tui-rotate 0.7s linear infinite;
  172. position: fixed;
  173. z-index: 999999;
  174. left: 50%;
  175. top: 50%;
  176. margin-left: -20rpx;
  177. margin-top: -20rpx;
  178. }
  179. .tui-loading-1 {
  180. border-color: #e5e5e5 #e5e5e5 #e5e5e5 #C74547;
  181. }
  182. .tui-loading-2 {
  183. border-color: #e5e5e5 #e5e5e5 #e5e5e5 #8f8d8e;
  184. }
  185. .tui-loading-3 {
  186. border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) #fff;
  187. }
  188. .tui-loading-4 {
  189. border-color: #e5e5e5 #e5e5e5 #e5e5e5 #35b06a;
  190. }
  191. .tui-loading-5 {
  192. border-color: #e5e5e5 #e5e5e5 #e5e5e5 #fc872d;
  193. }
  194. .tui-loading-6 {
  195. border-color: #e5e5e5 #e5e5e5 #e5e5e5 #eb0909;
  196. }
  197. .tui-loading-7 {
  198. border-color: #C74547 transparent #C74547 transparent;
  199. }
  200. .tui-loading-8 {
  201. border-color: #35b06a transparent #35b06a transparent;
  202. }
  203. .tui-loading-9 {
  204. border-color: #fc872d transparent #fc872d transparent;
  205. }
  206. .tui-loading-10 {
  207. border-color: #eb0909 transparent #eb0909 transparent;
  208. }
  209. @-webkit-keyframes tui-rotate {
  210. 0% {
  211. transform: rotate(0);
  212. }
  213. 100% {
  214. transform: rotate(360deg);
  215. }
  216. }
  217. @keyframes tui-rotate {
  218. 0% {
  219. transform: rotate(0);
  220. }
  221. 100% {
  222. transform: rotate(360deg);
  223. }
  224. }
  225. </style>