tui-skeleton.vue 5.5 KB

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