VideoPreview.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <template>
  2. <div ref="videoArea" class="video-preview" :style="getAreaStyle()">
  3. <SerialNumberPosition v-if="isEnable(data.property.sn_display_mode)" :property="data.property" />
  4. <div ref="videoAreaBox" 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 <= 0 ? 139 : 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" :style="{ height: elementHeight <= 0 ? 139 : elementHeight + 'px' }">
  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. inject: ['getDragStatus'],
  64. data() {
  65. return {
  66. data: getVideoData(),
  67. curVideoIndex: 0,
  68. elementWidth: 0,
  69. elementHeight: 0,
  70. viewTopBottomBtn: false,
  71. fileLen: 0,
  72. translateY: 0,
  73. elementID: '',
  74. isResizing: false,
  75. resizeObserver: null,
  76. };
  77. },
  78. watch: {
  79. data: {
  80. handler(val) {
  81. this.fileLen = val.file_list.length;
  82. if (this.fileLen > 0 && this.data.property.view_method === 'list') {
  83. const ele = this.$refs.videoAreaBox;
  84. const sn_position = this.data.property.sn_position;
  85. // 序号在左和右补齐序号高度,去掉padding(8*2)
  86. if (sn_position.includes('left') || sn_position.includes('right')) {
  87. this.elementWidth = ele.clientWidth - 16;
  88. this.elementHeight = ele.clientHeight + 30;
  89. } else {
  90. this.elementWidth = ele.clientWidth;
  91. this.elementHeight = ele.clientHeight;
  92. }
  93. if (ele.clientHeight <= 0) {
  94. this.elementHeight = this.data.min_height;
  95. }
  96. const mainEle = this.$refs.videoArea;
  97. // 检查元素是否包含已知的类名
  98. mainEle.classList.forEach((className) => {
  99. // 排除已知的类名
  100. if (className !== 'audio-area') {
  101. // 打印另一个类名
  102. this.elementID = className;
  103. }
  104. });
  105. }
  106. },
  107. deep: true,
  108. },
  109. elementHeight() {
  110. this.isViewTopBottomBtn();
  111. },
  112. },
  113. mounted() {
  114. this.$nextTick(() => {
  115. const canvasElement = document.querySelector('.canvas');
  116. if (!canvasElement) {
  117. const ele = this.$refs.videoAreaBox;
  118. const sn_position = this.data.property.sn_position;
  119. // 序号在左和右补齐序号高度,去掉padding(8*2)
  120. if (sn_position.includes('left') || sn_position.includes('right')) {
  121. this.elementWidth = ele.clientWidth - 16;
  122. this.elementHeight = ele.clientHeight + 30;
  123. } else {
  124. this.elementWidth = ele.clientWidth;
  125. this.elementHeight = ele.clientHeight;
  126. }
  127. if (ele.clientHeight <= 0) {
  128. this.elementHeight = this.data.min_height;
  129. }
  130. this.fileLen = this.data.file_list.length;
  131. this.isViewTopBottomBtn();
  132. return;
  133. }
  134. this.resizeObserver = new ResizeObserver((entries) => {
  135. if (!this.getDragStatus()) return;
  136. this.isResizing = true; // 标记为调整中
  137. for (let entry of entries) {
  138. window.requestAnimationFrame(() => {
  139. const sn_position = this.data.property.sn_position;
  140. // 序号在上方和下方减去序号高度
  141. let w = entry.contentRect.width - 16;
  142. let h = entry.contentRect.height;
  143. if (sn_position.includes('top') || sn_position.includes('bottom')) {
  144. w = entry.contentRect.width;
  145. h = entry.contentRect.height - 30;
  146. }
  147. // eslint-disable-next-line no-negated-condition
  148. if (this.elementWidth !== w) {
  149. this.elementWidth = w;
  150. } else {
  151. this.elementHeight = h;
  152. }
  153. });
  154. }
  155. // 防抖:100ms 后恢复监听
  156. setTimeout(() => {
  157. this.isResizing = false;
  158. }, 500);
  159. });
  160. this.resizeObserver.observe(this.$el);
  161. });
  162. },
  163. beforeDestroy() {
  164. this.resizeObserver.disconnect();
  165. },
  166. methods: {
  167. handleAudioClick(index) {
  168. // 获取 Carousel 实例
  169. const carousel = this.$refs.video_carousel;
  170. // 切换到对应索引的文件
  171. carousel.setActiveItem(index);
  172. this.curVideoIndex = index;
  173. },
  174. changeFile(type) {
  175. // 获取 Carousel 实例
  176. const carousel = this.$refs.video_carousel;
  177. // 切换到对应索引的文件
  178. if (type === 'prev') {
  179. carousel.prev();
  180. this.curVideoIndex += -1;
  181. } else {
  182. carousel.next();
  183. this.curVideoIndex += 1;
  184. }
  185. if (this.curVideoIndex >= this.data.file_id_list.length) {
  186. this.curVideoIndex = 0;
  187. }
  188. if (this.curVideoIndex < 0) {
  189. this.curVideoIndex = this.data.file_id_list.length - 1;
  190. }
  191. },
  192. handleResize() {
  193. const width = this.$refs.videoAreaBox.clientWidth;
  194. if (width !== this.elementWidth) {
  195. this.elementWidth = width;
  196. }
  197. },
  198. // 是否显示上下箭头
  199. isViewTopBottomBtn() {
  200. // 计算右侧列表图片高度
  201. let listHeight = this.fileLen * this.data.min_height + 20 * (this.fileLen - 1);
  202. if (listHeight > this.elementHeight) {
  203. this.viewTopBottomBtn = true;
  204. } else {
  205. this.viewTopBottomBtn = false;
  206. this.translateY = 0;
  207. }
  208. },
  209. // 滚动图片列表
  210. scroll(direction) {
  211. const minHeight = Number(this.data.min_height);
  212. const step = minHeight + 20; // 每次滚动的距离
  213. let _down = (minHeight + 20) * (this.fileLen - 1);
  214. // 计算滚动后的 translateY 值
  215. let newY = this.translateY + step * direction;
  216. // 检查是否超出上下边界
  217. if (newY > 0) {
  218. // 滚动到第一张图片时不再向上滚动
  219. this.translateY = 0;
  220. } else if (newY < -_down) {
  221. // 滚动到最后一张图片时不再向下滚动
  222. this.translateY = -_down;
  223. } else {
  224. // 在边界内时执行滚动
  225. this.translateY = newY;
  226. }
  227. },
  228. },
  229. };
  230. </script>
  231. <style lang="scss" scoped>
  232. @use '@/styles/mixin.scss' as *;
  233. .video-preview {
  234. @include preview-base;
  235. .view-independent {
  236. display: flex;
  237. flex-wrap: wrap;
  238. gap: 20px;
  239. width: 100%;
  240. > li {
  241. width: 248px;
  242. height: 139px;
  243. }
  244. }
  245. .view-list {
  246. display: flex;
  247. column-gap: 32px;
  248. :deep .el-carousel {
  249. background-color: #d9d9d9;
  250. &__container {
  251. height: 100%;
  252. }
  253. }
  254. .container-box {
  255. position: relative;
  256. overflow: hidden;
  257. .arrow {
  258. position: absolute;
  259. z-index: 10;
  260. width: 100%;
  261. height: 40px;
  262. text-align: center;
  263. background-color: rgba(0, 0, 0, 10%);
  264. border-radius: 0;
  265. }
  266. .arrow:hover {
  267. background-color: rgba(0, 0, 0, 30%);
  268. }
  269. .top {
  270. top: 0;
  271. }
  272. .bottom {
  273. bottom: 0;
  274. }
  275. .view-list-bottom {
  276. display: flex;
  277. flex-direction: column;
  278. row-gap: 20px;
  279. > li {
  280. width: 248px;
  281. height: calc(248px * 9 / 16);
  282. }
  283. }
  284. }
  285. }
  286. }
  287. </style>