VideoPreview.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <div class="video_area" :style="getAreaStyle()">
  3. <SerialNumberPosition :property="data.property" />
  4. <div ref="videoArea" class="main">
  5. <ul v-if="'independent' === data.property.view_method" class="view-independent">
  6. <li v-for="(file, i) in data.file_list" :key="i">
  7. <VideoPlay
  8. view-size="small"
  9. view-method="independent"
  10. :file-id="file.file_id"
  11. :cur-video-index="curVideoIndex"
  12. @changeFile="changeFile"
  13. />
  14. </li>
  15. </ul>
  16. <div v-else class="view-list">
  17. <el-carousel
  18. ref="video_carousel"
  19. indicator-position="none"
  20. direction="vertical"
  21. :autoplay="false"
  22. :interval="0"
  23. :style="{ width: elementWidth - 248 - 32 + 'px', height: elementHeight + 'px' }"
  24. >
  25. <el-carousel-item v-for="(file, i) in data.file_list" :key="i">
  26. <VideoPlay
  27. view-size="big"
  28. :file-id="file.file_id"
  29. :cur-video-index="curVideoIndex"
  30. @changeFile="changeFile"
  31. />
  32. </el-carousel-item>
  33. </el-carousel>
  34. <div class="container-box">
  35. <ul
  36. ref="container"
  37. class="view-list-bottom"
  38. :style="{ height: elementHeight + 'px', transform: `translateY(${translateY}px)` }"
  39. >
  40. <li v-for="(file, i) in data.file_list" :key="i" @click="handleAudioClick(i)">
  41. <VideoPlay view-size="small" :file-id="file.file_id" :audio-index="i" :cur-video-index="curVideoIndex" />
  42. </li>
  43. </ul>
  44. <button v-if="viewTopBottomBtn" class="arrow top" @click="scroll(1)">
  45. <i class="el-icon-arrow-up"></i>
  46. </button>
  47. <button v-if="viewTopBottomBtn" class="arrow bottom" @click="scroll(-1)">
  48. <i class="el-icon-arrow-down"></i>
  49. </button>
  50. </div>
  51. </div>
  52. </div>
  53. </div>
  54. </template>
  55. <script>
  56. import { getVideoData } from '@/views/book/courseware/data/video';
  57. import PreviewMixin from '../common/PreviewMixin';
  58. import VideoPlay from '../common/VideoPlay.vue';
  59. export default {
  60. name: 'VideoPreview',
  61. components: { VideoPlay },
  62. mixins: [PreviewMixin],
  63. data() {
  64. return {
  65. data: getVideoData(),
  66. curVideoIndex: 0,
  67. elementWidth: 0,
  68. elementHeight: 0,
  69. viewTopBottomBtn: false,
  70. fileLen: 0,
  71. translateY: 0,
  72. };
  73. },
  74. watch: {
  75. data: {
  76. handler(val) {
  77. this.fileLen = val.file_list.length;
  78. if (this.fileLen > 0) {
  79. const ele = this.$refs.videoArea;
  80. this.elementWidth = ele.clientWidth;
  81. this.elementHeight = ele.clientHeight;
  82. window.addEventListener('resize', this.handleResize);
  83. }
  84. },
  85. deep: true,
  86. },
  87. elementWidth(newWidth, oldWidth) {
  88. // console.log(`宽度从 ${oldWidth} 变为 ${newWidth}`);
  89. this.elementWidth = newWidth;
  90. },
  91. elementHeight(newHeight, oldHeight) {
  92. // console.log(`高度从 ${oldHeight} 变为 ${newHeight}`);
  93. this.elementHeight = newHeight;
  94. this.isViewTopBottomBtn();
  95. },
  96. },
  97. mounted() {
  98. this.$nextTick(() => {
  99. let _class = document.querySelector('.canvas');
  100. if (_class === null) return;
  101. if (_class.classList.contains('canvas')) {
  102. this.resizeObserver = new ResizeObserver((entries) => {
  103. for (let entry of entries) {
  104. this.elementWidth = entry.contentRect.width;
  105. this.elementHeight = entry.contentRect.height;
  106. }
  107. });
  108. this.resizeObserver.observe(this.$el);
  109. }
  110. });
  111. },
  112. beforeDestroy() {
  113. window.removeEventListener('resize', this.handleResize);
  114. if (this.resizeObserver) {
  115. this.resizeObserver.disconnect();
  116. }
  117. },
  118. methods: {
  119. handleAudioClick(index) {
  120. // 获取 Carousel 实例
  121. const carousel = this.$refs.video_carousel;
  122. // 切换到对应索引的文件
  123. carousel.setActiveItem(index);
  124. this.curVideoIndex = index;
  125. },
  126. changeFile(type) {
  127. // 获取 Carousel 实例
  128. const carousel = this.$refs.video_carousel;
  129. // 切换到对应索引的文件
  130. if (type === 'prev') {
  131. carousel.prev();
  132. this.curVideoIndex += -1;
  133. } else {
  134. carousel.next();
  135. this.curVideoIndex += 1;
  136. }
  137. if (this.curVideoIndex >= this.data.file_id_list.length) {
  138. this.curVideoIndex = 0;
  139. }
  140. if (this.curVideoIndex < 0) {
  141. this.curVideoIndex = this.data.file_id_list.length - 1;
  142. }
  143. },
  144. handleResize() {
  145. const width = this.$refs.videoArea.clientWidth;
  146. if (width !== this.elementWidth) {
  147. this.elementWidth = width;
  148. }
  149. },
  150. // 是否显示上下箭头
  151. isViewTopBottomBtn() {
  152. // 计算右侧列表图片高度
  153. let listHeight = this.fileLen * this.data.min_height + 20 * (this.fileLen - 1);
  154. if (listHeight > this.elementHeight) {
  155. this.viewTopBottomBtn = true;
  156. } else {
  157. this.viewTopBottomBtn = false;
  158. }
  159. },
  160. // 滚动图片列表
  161. scroll(direction) {
  162. const minHeight = Number(this.data.min_height);
  163. const step = minHeight + 20; // 每次滚动的距离
  164. let _down = (minHeight + 20) * (this.fileLen - 1);
  165. // 计算滚动后的 translateY 值
  166. let newY = this.translateY + step * direction;
  167. // 检查是否超出上下边界
  168. if (newY > 0) {
  169. // 滚动到第一张图片时不再向上滚动
  170. this.translateY = 0;
  171. } else if (newY < -_down) {
  172. // 滚动到最后一张图片时不再向下滚动
  173. this.translateY = -_down;
  174. } else {
  175. // 在边界内时执行滚动
  176. this.translateY = newY;
  177. }
  178. },
  179. },
  180. };
  181. </script>
  182. <style lang="scss" scoped>
  183. .video_area {
  184. display: grid;
  185. gap: 6px;
  186. padding: 8px;
  187. > .main {
  188. display: flex;
  189. > span {
  190. display: flex;
  191. }
  192. }
  193. .main {
  194. grid-area: main;
  195. width: 100%;
  196. .view-independent {
  197. display: flex;
  198. flex-wrap: wrap;
  199. gap: 20px;
  200. width: 100%;
  201. > li {
  202. width: 248px;
  203. height: 139px;
  204. }
  205. }
  206. .view-list {
  207. display: flex;
  208. column-gap: 32px;
  209. :deep .el-carousel {
  210. background-color: #d9d9d9;
  211. &__container {
  212. height: 100%;
  213. }
  214. }
  215. .container-box {
  216. position: relative;
  217. overflow: hidden;
  218. .arrow {
  219. position: absolute;
  220. z-index: 10;
  221. width: 100%;
  222. height: 40px;
  223. text-align: center;
  224. background-color: rgba(0, 0, 0, 10%);
  225. border-radius: 0;
  226. }
  227. .arrow:hover {
  228. background-color: rgba(0, 0, 0, 30%);
  229. }
  230. .top {
  231. top: 0;
  232. }
  233. .bottom {
  234. bottom: 0;
  235. }
  236. .view-list-bottom {
  237. display: flex;
  238. flex-direction: column;
  239. row-gap: 20px;
  240. > li {
  241. width: 248px;
  242. height: calc(248px * 9 / 16);
  243. }
  244. }
  245. }
  246. }
  247. }
  248. }
  249. </style>