PicturePreview.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <div class="picture-area" :style="getAreaStyle()">
  3. <SerialNumberPosition :property="data.property" />
  4. <div ref="pictureArea" class="main">
  5. <div class="view-area">
  6. <template v-if="'list' === data.property.view_method">
  7. <el-carousel
  8. ref="pictureCarousel"
  9. class="view-list"
  10. indicator-position="none"
  11. :autoplay="false"
  12. :style="{ height: elementHeight - 144 - 17 + 'px' }"
  13. >
  14. <el-carousel-item v-for="(file, i) in data.file_list" :key="i">
  15. <el-image :id="file.file_id" :src="file.file_url" fit="contain" />
  16. </el-carousel-item>
  17. </el-carousel>
  18. <div class="container-box">
  19. <button v-if="viewLeftRightBtn" class="arrow left" @click="scroll(-1)">
  20. <i class="el-icon-arrow-left"></i>
  21. </button>
  22. <ul ref="container" class="view-list-bottom" :style="{ width: elementWidth + 'px' }">
  23. <li v-for="(file, i) in data.file_list" :key="i" @click="handleIndicatorClick(i)">
  24. <el-image :id="file.file_id" :src="file.file_url" fit="contain" />
  25. </li>
  26. </ul>
  27. <button v-if="viewLeftRightBtn" class="arrow right" @click="scroll(1)">
  28. <i class="el-icon-arrow-right"></i>
  29. </button>
  30. </div>
  31. </template>
  32. <ul v-else class="view-independent">
  33. <li v-for="(file, i) in data.file_list" :key="i" @click="handleIndicatorClick(i)">
  34. <el-image :id="file.file_id" :src="file.file_url" fit="contain" />
  35. </li>
  36. </ul>
  37. </div>
  38. </div>
  39. </div>
  40. </template>
  41. <script>
  42. import { getPictureData } from '@/views/book/courseware/data/picture';
  43. import PreviewMixin from '../common/PreviewMixin';
  44. export default {
  45. name: 'PicturePreview',
  46. mixins: [PreviewMixin],
  47. data() {
  48. return {
  49. data: getPictureData(),
  50. curImgIndex: 0,
  51. elementWidth: 0,
  52. elementHeight: 0,
  53. viewLeftRightBtn: false,
  54. fileLen: 0,
  55. resizeObserver: null, // ResizeObserver 实例,用于监听元素大小变化
  56. };
  57. },
  58. watch: {
  59. data: {
  60. handler(val) {
  61. this.fileLen = val.file_list.length;
  62. if (this.fileLen > 0) {
  63. const ele = this.$refs.pictureArea;
  64. this.elementWidth = ele.clientWidth;
  65. this.elementHeight = ele.clientHeight;
  66. window.addEventListener('resize', this.handleResize);
  67. }
  68. },
  69. deep: true,
  70. },
  71. elementWidth(newWidth, oldWidth) {
  72. // console.log(`宽度从 ${oldWidth} 变为 ${newWidth}`);
  73. this.elementWidth = newWidth;
  74. this.isViewLeftRightBtn();
  75. },
  76. elementHeight(newHeight, oldHeight) {
  77. // console.log(`高度从 ${oldHeight} 变为 ${newHeight}`);
  78. this.elementHeight = newHeight;
  79. },
  80. },
  81. mounted() {
  82. this.$nextTick(() => {
  83. this.resizeObserver = new ResizeObserver((entries) => {
  84. for (let entry of entries) {
  85. this.elementWidth = entry.contentRect.width;
  86. this.elementHeight = entry.contentRect.height;
  87. }
  88. });
  89. this.resizeObserver.observe(this.$el);
  90. });
  91. },
  92. beforeDestroy() {
  93. window.removeEventListener('resize', this.handleResize);
  94. if (this.resizeObserver) {
  95. this.resizeObserver.disconnect();
  96. }
  97. },
  98. methods: {
  99. handleResize() {
  100. const width = this.$refs.pictureArea.clientWidth;
  101. if (width !== this.elementWidth) {
  102. this.elementWidth = width;
  103. }
  104. },
  105. // 是否显示左右箭头
  106. isViewLeftRightBtn() {
  107. // 计算底部列表图片宽度
  108. let listWidth = this.fileLen * this.data.min_width + 13 * (this.fileLen - 1);
  109. if (listWidth > this.elementWidth) {
  110. this.viewLeftRightBtn = true;
  111. } else {
  112. this.viewLeftRightBtn = false;
  113. }
  114. },
  115. handleIndicatorClick(index) {
  116. // 获取 Carousel 实例
  117. const carousel = this.$refs.pictureCarousel;
  118. // 切换到对应索引的图片
  119. carousel.setActiveItem(index);
  120. },
  121. // 滚动图片列表
  122. scroll(direction) {
  123. const container = this.$refs.container;
  124. const step = Number(this.data.min_width) + 13; // 每次滚动的距离
  125. this.scrollPosition += step * direction;
  126. container.scrollLeft += step * direction;
  127. },
  128. },
  129. };
  130. </script>
  131. <style lang="scss" scoped>
  132. .picture-area {
  133. display: grid;
  134. gap: 6px;
  135. padding: 8px;
  136. > .main {
  137. display: flex;
  138. margin: 4px auto;
  139. > span {
  140. display: flex;
  141. }
  142. }
  143. .main {
  144. grid-area: main;
  145. width: 100%;
  146. .view-area {
  147. width: 100%;
  148. :deep .el-carousel {
  149. margin-bottom: 17px;
  150. background-color: #d9d9d9;
  151. &__container::before {
  152. display: inline-block;
  153. padding-bottom: 55%;
  154. content: '';
  155. }
  156. &__container {
  157. height: 100%;
  158. }
  159. &__item {
  160. display: flex;
  161. justify-content: center;
  162. text-align: center;
  163. }
  164. }
  165. .container-box {
  166. position: relative;
  167. .arrow {
  168. position: absolute;
  169. top: 0;
  170. z-index: 10;
  171. height: 144px;
  172. text-align: center;
  173. background-color: rgba(0, 0, 0, 10%);
  174. border-radius: 0;
  175. }
  176. .arrow:hover {
  177. background-color: rgba(0, 0, 0, 30%);
  178. }
  179. .right {
  180. right: 0;
  181. }
  182. .view-list-bottom {
  183. display: flex;
  184. flex-wrap: nowrap;
  185. column-gap: 13px;
  186. min-width: 144px;
  187. overflow: hidden;
  188. li {
  189. display: flex;
  190. align-items: center;
  191. justify-content: center;
  192. background-color: #d9d9d9;
  193. }
  194. .el-image {
  195. width: 144px;
  196. height: 144px;
  197. cursor: pointer;
  198. }
  199. }
  200. }
  201. .view-independent {
  202. display: flex;
  203. flex-wrap: wrap;
  204. gap: 40px;
  205. li {
  206. display: flex;
  207. align-items: center;
  208. justify-content: center;
  209. background-color: #d9d9d9;
  210. }
  211. .el-image {
  212. width: 144px;
  213. height: 144px;
  214. }
  215. }
  216. }
  217. }
  218. }
  219. </style>