Skeleton.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <view class="skeleton" :class="[type, { animated }]" :style="customStyle">
  3. <view v-if="type === 'text'" class="skeleton-text" :style="{ width, height }" />
  4. <view v-else-if="type === 'circle'" class="skeleton-circle" :style="{ width, height }" />
  5. <view v-else-if="type === 'rect'" class="skeleton-rect" :style="{ width, height }" />
  6. <view v-else-if="type === 'image'" class="skeleton-image" :style="{ width, height }" />
  7. <slot v-else />
  8. </view>
  9. </template>
  10. <script setup lang="ts">
  11. interface Props {
  12. type?: 'text' | 'circle' | 'rect' | 'image' | 'custom';
  13. width?: string;
  14. height?: string;
  15. animated?: boolean;
  16. }
  17. withDefaults(defineProps<Props>(), {
  18. type: 'text',
  19. width: '100%',
  20. height: '20rpx',
  21. animated: true,
  22. });
  23. const customStyle = {};
  24. </script>
  25. <style scoped>
  26. .skeleton {
  27. background: linear-gradient(
  28. 90deg,
  29. #f0f0f0 25%,
  30. #e0e0e0 37%,
  31. #f0f0f0 63%
  32. );
  33. background-size: 400% 100%;
  34. }
  35. .skeleton.animated {
  36. animation: skeleton-loading 1.4s ease infinite;
  37. }
  38. .skeleton-text {
  39. border-radius: 4rpx;
  40. }
  41. .skeleton-circle {
  42. border-radius: 50%;
  43. }
  44. .skeleton-rect {
  45. border-radius: 8rpx;
  46. }
  47. .skeleton-image {
  48. border-radius: 8rpx;
  49. display: flex;
  50. align-items: center;
  51. justify-content: center;
  52. }
  53. @keyframes skeleton-loading {
  54. 0% {
  55. background-position: 100% 50%;
  56. }
  57. 100% {
  58. background-position: 0 50%;
  59. }
  60. }
  61. </style>