index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <div>
  3. <a-alert message="抽取于 Element 的 Image 组件进行改造,在保留原生img的特性下,支持懒加载,自定义占位、加载失败等。" style="margin-bottom: 20px;" />
  4. <div class="demo-image">
  5. <div v-for="fit in fits" :key="fit" class="block">
  6. <span class="demonstration">{{ fit }}</span>
  7. <c-image
  8. :src="url"
  9. :fit="fit"
  10. style="width: 100px; height: 100px"
  11. />
  12. </div>
  13. </div>
  14. <a-alert message="占位内容" style="margin-bottom: 20px;margin-top: 20px;" />
  15. <div class="demo-image__placeholder">
  16. <div class="block">
  17. <span class="demonstration">默认</span>
  18. <c-image :src="src" style="width: 300px; height: 200px" />
  19. </div>
  20. <div class="block">
  21. <span class="demonstration">自定义</span>
  22. <c-image :src="src" style="width: 300px; height: 200px">
  23. <template #placeholder>
  24. <div class="image-slot">
  25. 加载中...
  26. </div>
  27. </template>
  28. </c-image>
  29. </div>
  30. </div>
  31. <a-alert message="加载失败" style="margin-bottom: 20px;margin-top: 20px;" />
  32. <div class="demo-image__error">
  33. <div class="block">
  34. <span class="demonstration">默认</span>
  35. <c-image style="width: 300px; height: 200px" />
  36. </div>
  37. <div class="block">
  38. <span class="demonstration">自定义</span>
  39. <c-image style="width: 300px; height: 200px">
  40. <template #error>
  41. <div class="image-slot">
  42. <PictureOutlined />
  43. </div>
  44. </template>
  45. </c-image>
  46. </div>
  47. </div>
  48. <a-alert message="懒加载" style="margin-bottom: 20px;margin-top: 20px;" />
  49. <div class="demo-image__lazy">
  50. <c-image
  51. v-for="url in urls"
  52. :key="url"
  53. :src="url"
  54. lazy
  55. style="display: block;min-height: 200px;margin-bottom: 10px;"
  56. />
  57. </div>
  58. </div>
  59. </template>
  60. <script lang="ts">
  61. import { defineComponent, ref } from 'vue'
  62. import CImage from '_c/Image/index.vue'
  63. import { PictureOutlined } from '@ant-design/icons-vue'
  64. export default defineComponent({
  65. // name: 'Image',
  66. components: {
  67. CImage,
  68. PictureOutlined
  69. },
  70. setup() {
  71. const fits = ref<string[]>(['fill', 'contain', 'cover', 'none', 'scale-down'])
  72. const url = ref<string>('https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg')
  73. const src = ref<string>('https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg')
  74. const urls = ref<string[]>([
  75. 'https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg',
  76. 'https://fuss10.elemecdn.com/1/34/19aa98b1fcb2781c4fba33d850549jpeg.jpeg',
  77. 'https://fuss10.elemecdn.com/0/6f/e35ff375812e6b0020b6b4e8f9583jpeg.jpeg',
  78. 'https://fuss10.elemecdn.com/9/bb/e27858e973f5d7d3904835f46abbdjpeg.jpeg',
  79. 'https://fuss10.elemecdn.com/d/e6/c4d93a3805b3ce3f323f7974e6f78jpeg.jpeg',
  80. 'https://fuss10.elemecdn.com/3/28/bbf893f792f03a54408b3b7a7ebf0jpeg.jpeg',
  81. 'https://fuss10.elemecdn.com/2/11/6535bcfb26e4c79b48ddde44f4b6fjpeg.jpeg'
  82. ])
  83. return {
  84. fits,
  85. url,
  86. src,
  87. urls
  88. }
  89. }
  90. })
  91. </script>
  92. <style lang="less" scoped>
  93. .demo-image,
  94. .demo-image__placeholder,
  95. .demo-image__error {
  96. background: #fff;
  97. }
  98. .demo-image .block,
  99. .demo-image__error .block,
  100. .demo-image__placeholder .block {
  101. padding: 30px 0;
  102. text-align: center;
  103. border-right: 1px solid #eff2f6;
  104. display: inline-block;
  105. width: 20%;
  106. box-sizing: border-box;
  107. vertical-align: top;
  108. .demonstration {
  109. display: block;
  110. margin-bottom: 5px;
  111. }
  112. .image__error,
  113. .image-slot {
  114. display: flex;
  115. justify-content: center;
  116. align-items: center;
  117. font-size: 14px;
  118. color: #c0c4cc;
  119. vertical-align: middle;
  120. height: 100%;
  121. height: 100%;
  122. background: #f5f7fa;
  123. }
  124. }
  125. .demo-image__error .block,
  126. .demo-image__placeholder .block {
  127. text-align: center;
  128. width: 49%;
  129. }
  130. .demo-image__lazy {
  131. width: 800px;
  132. height: 400px;
  133. overflow-y: auto;
  134. }
  135. </style>