VideoPreview.vue 8.4 KB

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