当前位置:   article > 正文

Vue 视频与图片混合自动轮播_vue实现图片和视频混合播放

vue实现图片和视频混合播放

结合vue-awesome-swiper的多媒体自动轮播、自动播放组件。

1、安装vue-awesome-swiper

npm i vue-awesome-swiper@3 -S

2、引入vue-awesome-swiper

页面引入:

  1. <script>
  2. //页面引入swiper
  3. import 'swiper/dist/css/swiper.css'
  4. import { swiper, swiperSlide } from 'vue-awesome-swiper'
  5. export default {
  6. components: {
  7. swiper,
  8. swiperSlide
  9. },
  10. };
  11. </script>

全局引入:

main.js

  1. import Vue from 'vue'
  2. import VueAwesomeSwiper from 'vue-awesome-swiper'
  3. //引入样式
  4. import 'swiper/css/swiper.css'
  5. Vue.use(VueAwesomeSwiper, /* { 全局组件的默认选项 } */)

3、主要代码

  1. <template>
  2. <div id="app">
  3. <div class="test">
  4. <div class="autoplay">
  5. <swiper :options="swiperOption"
  6. ref="videoSwiper"
  7. v-if="initOrNot">
  8. <swiper-slide v-for="( item , index ) in mediaNews"
  9. :key="index">
  10. <video v-if="item.type===1"
  11. :src="item.url"
  12. controls
  13. muted
  14. @ended="endVideo(index)"
  15. class="multimedia"></video>
  16. <img v-else
  17. :src="item.url"
  18. class="multimedia">
  19. </swiper-slide>
  20. <div class="swiper-pagination"
  21. v-for="(item,index) in mediaNews"
  22. :key="index"
  23. slot="pagination"></div>
  24. </swiper>
  25. </div>
  26. </div>
  27. </div>
  28. </template>
  29. <script>
  30. //页面引入swiper
  31. import 'swiper/dist/css/swiper.css'
  32. import { swiper, swiperSlide } from 'vue-awesome-swiper'
  33. export default {
  34. name: 'App',
  35. components: { swiper, swiperSlide },
  36. data() {
  37. return {
  38. mediaLastIndex: 0,
  39. swiperOption: {
  40. speed: 1000,
  41. loop: false,
  42. observer: true,
  43. observeParents: true,
  44. autoplayDisableOnInteraction: false,
  45. allowTouchMove: false,
  46. pagination: {
  47. el: '.swiper-pagination',
  48. clickable: true
  49. },
  50. on: {
  51. slideChangeTransitionEnd: () => {
  52. this.slideChangeTransitionEndHandle()
  53. },
  54. slideChangeTransitionStart: () => {
  55. this.slideChangeTransitionStartHandle()
  56. },
  57. //控制第一个slide切换
  58. init: () => {
  59. this.initHandle()
  60. }
  61. }
  62. },
  63. initOrNot: false, //mediaNews数据改变的时候确保swiper会init一次
  64. mediaNews: [
  65. // 1为视频类2为图片类
  66. // { url: require('./assets/莫干山1.jpg'), type: 2 },
  67. // { url: require('./assets/v1.mp4'), type: 1 },
  68. // { url: require('./assets/v1.mp4'), type: 1 },
  69. // { url: require('./assets/莫干山1.jpg'), type: 2 }
  70. ]
  71. }
  72. },
  73. methods: {
  74. initHandle() {
  75. let that = this
  76. // console.log(this.mediaNews)
  77. setTimeout(function () {
  78. console.log(that.$refs.videoSwiper)
  79. let swiper = that.$refs.videoSwiper.swiper
  80. if (that.mediaNews[0]?.type === 2) {
  81. that.mediaNewsImgHandle(swiper)
  82. } else {
  83. document.getElementsByClassName('multimedia')[0]?.play()
  84. }
  85. }, 200)
  86. },
  87. mediaNewsImgHandle(swiper) {
  88. let that = this
  89. //刚切换到的activeIndex
  90. let changePointActiveIndex = swiper.activeIndex
  91. if (swiper.activeIndex < this.mediaNews.length - 1) {
  92. setTimeout(function () {
  93. //要确认changePointActiveIndex是不是还是目前的activeIndex,是的话计时后执行,不是的话不执行
  94. if (changePointActiveIndex === swiper.activeIndex) {
  95. swiper.slideNext()
  96. }
  97. }, 10000)
  98. } else {
  99. setTimeout(function () {
  100. if (changePointActiveIndex === swiper.activeIndex) {
  101. swiper.slideTo(0, 0)
  102. }
  103. }, 10000)
  104. }
  105. },
  106. slideChangeTransitionStartHandle() {
  107. let swiper = this.$refs.videoSwiper.swiper
  108. if (this.mediaNews[this.mediaLastIndex].type === 1) {
  109. document.getElementsByClassName('multimedia')[this.mediaLastIndex].currentTime = 0
  110. } else {
  111. }
  112. },
  113. slideChangeTransitionEndHandle() {
  114. // console.log('end..')
  115. let that = this
  116. let swiper = that.$refs.videoSwiper.swiper
  117. if (this.mediaNews[this.mediaLastIndex].type === 1) {
  118. document.getElementsByClassName('multimedia')[this.mediaLastIndex].pause()
  119. }
  120. if (this.mediaNews[swiper.activeIndex].type === 2) {
  121. this.mediaNewsImgHandle(swiper)
  122. } else {
  123. document.getElementsByClassName('multimedia')[swiper.activeIndex].play()
  124. }
  125. this.mediaLastIndex = swiper.activeIndex
  126. },
  127. endVideo(index) {
  128. let swiper = this.$refs.videoSwiper.swiper
  129. if (index === swiper.activeIndex) {
  130. if (swiper.activeIndex < this.mediaNews.length - 1) {
  131. swiper.slideNext()
  132. if (this.mediaNews[swiper.activeIndex].type === 1) {
  133. document.getElementsByClassName('multimedia')[swiper.activeIndex].play()
  134. } else {
  135. this.mediaNewsImgHandle(swiper)
  136. }
  137. } else {
  138. swiper.slideTo(0, 0)
  139. if (this.mediaNews[swiper.activeIndex].type === 1) {
  140. document.getElementsByClassName('multimedia')[swiper.activeIndex].play()
  141. } else {
  142. this.mediaNewsImgHandle(swiper)
  143. }
  144. }
  145. }
  146. }
  147. },
  148. watch: {
  149. mediaNews: {
  150. handler(newName, oldName) {
  151. this.initOrNot = false
  152. this.$nextTick(() => {
  153. this.initOrNot = true
  154. })
  155. },
  156. immediate: true,
  157. deep: true
  158. }
  159. }
  160. }
  161. </script>
  162. <style>
  163. </style>

4、注意:在vue-awesome-swiper@4 版本中 需要将this.$refs.videoSwiper.swiper 换成this.$refs.videoSwiper.$swiper

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/251036
推荐阅读
相关标签
  

闽ICP备14008679号