Treasure.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <div
  3. ref="gridWrapperElem"
  4. class="history"
  5. >
  6. <div
  7. class="grid"
  8. @mousemove.prevent
  9. @mousemove="onMouseMove"
  10. @mouseleave="onMouseLeave"
  11. @mousedown="onMouseDown"
  12. @mouseup="onMouseUp"
  13. >
  14. <div
  15. v-for="item in treasureList.value"
  16. :key="item.id"
  17. class="cover grid-item"
  18. @click="!isDragged && $router.push({name: 'TreasureDetail', query: {id: item.id}})"
  19. >
  20. <img
  21. class="photo"
  22. :src="`${prefix}${item.thumb}`"
  23. alt=""
  24. draggable="false"
  25. load="lazy"
  26. >
  27. <h3>{{ item.name }}</h3>
  28. </div>
  29. </div>
  30. <!-- element-ui的loading效果从调用到出现有延时,这期间要遮盖住组件 -->
  31. <div
  32. v-show="isShowLoadingMask"
  33. class="loading-mask"
  34. />
  35. </div>
  36. </template>
  37. <script>
  38. export default {
  39. name: 'TreasureView',
  40. }
  41. </script>
  42. <script setup>
  43. import {
  44. onMounted,
  45. reactive,
  46. ref,
  47. toRefs,
  48. } from 'vue'
  49. import { ElLoading } from 'element-plus'
  50. const prefix = process.env.VUE_APP_API_ORIGIN
  51. // 滑动功能
  52. let isMouseDown = false
  53. let isDragged = false
  54. const gridWrapperElem = ref('null')
  55. function onMouseDown() {
  56. isMouseDown = true
  57. isDragged = false
  58. }
  59. function onMouseUp() {
  60. isMouseDown = false
  61. }
  62. function onMouseMove(e) {
  63. if (isMouseDown) {
  64. console.log(e)
  65. gridWrapperElem.value.scrollLeft -= e.movementX
  66. isDragged = true
  67. }
  68. }
  69. function onMouseLeave(e) {
  70. isMouseDown = false
  71. }
  72. // 获取数据
  73. let treasureList = reactive({ value: null })
  74. api.getTreasureList().then((res) => {
  75. treasureList.value = res
  76. })
  77. // 布局相关
  78. onMounted(() => {
  79. setTimeout(() => {
  80. const isotopeInst = $('.grid').isotope({
  81. layoutMode: 'packery',
  82. itemSelector: '.grid-item',
  83. packery: {
  84. horizontal: true,
  85. // gutter: 10,
  86. }
  87. })
  88. }, 500)
  89. })
  90. // mask相关
  91. const isShowLoadingMask = ref(true)
  92. onMounted(() => {
  93. const loadingInstance = ElLoading.service({
  94. background: 'transparent',
  95. })
  96. setTimeout(() => {
  97. // const isotopeInst = $('.grid').isotope({
  98. // layoutMode: 'packery',
  99. // itemSelector: '.grid-item',
  100. // packery: {
  101. // horizontal: true,
  102. // // gutter: 10,
  103. // }
  104. // })
  105. isShowLoadingMask.value = false
  106. loadingInstance.close()
  107. }, 666)
  108. })
  109. </script>
  110. <style lang="less" scoped>
  111. .history {
  112. width: 100%;
  113. height: 100%;
  114. overflow: auto;
  115. &::-webkit-scrollbar { background: transparent; width: 0; } /*宽度是对垂直滚动条而言,高度是对水平滚动条而言*/
  116. &::-webkit-scrollbar-thumb {
  117. // background: rgba(220, 231, 240, 0.2);
  118. // border-radius: 2px;
  119. }
  120. >.grid {
  121. height: 100%;
  122. &::after {
  123. content: '';
  124. display: block;
  125. clear: both;
  126. }
  127. >.grid-item {
  128. float: left;
  129. position: relative;
  130. border: 5px solid #000;
  131. font-size: 0; // 令strut高度为0
  132. cursor: pointer;
  133. > img {
  134. min-width: 100px;
  135. min-height: 100px;
  136. max-height: 100vh;
  137. object-fit: cover;
  138. }
  139. >h3 {
  140. position: absolute;
  141. left: 0;
  142. bottom: 5px;
  143. width: 100%;
  144. padding: 7px 25px;
  145. background: linear-gradient(90deg, rgba(176,161,121,0.7) 0%, rgba(0,0,0,0) 100%);
  146. font-size: 20px;
  147. font-family: Source Han Sans CN-Regular, Source Han Sans CN;
  148. font-weight: 400;
  149. color: #FFFFFF;
  150. line-height: 1.5;
  151. }
  152. }
  153. >.grid-item--width2 { width: 400px; }
  154. >.grid-item--height2 { height: 400px; }
  155. }
  156. .loading-mask {
  157. position: absolute;
  158. left: 0;
  159. top: 0;
  160. width: 100%;
  161. height: 100%;
  162. background: black;
  163. }
  164. }
  165. </style>