swiper-vertical.nvue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <view class="page">
  3. <swiper class="swiper" :circular="circular" :vertical="true" @change="onSwiperChange">
  4. <swiper-item v-for="item in videoList" :key="item.id">
  5. <video class="video" :id="item.id" :ref="item.id" :src="item.src" :controls="false" :loop="true"
  6. :show-center-play-btn="false"></video>
  7. </swiper-item>
  8. </swiper>
  9. </view>
  10. </template>
  11. <script>
  12. const videoData = [{
  13. src: 'https://img.cdn.aliyun.dcloud.net.cn/guide/uniapp/hellouniapp/hello-nvue-swiper-vertical-01.mp4'
  14. },
  15. {
  16. src: 'https://img.cdn.aliyun.dcloud.net.cn/guide/uniapp/hellouniapp/hello-nvue-swiper-vertical-02.mp4'
  17. },
  18. {
  19. src: 'https://img.cdn.aliyun.dcloud.net.cn/guide/uniapp/hellouniapp/hello-nvue-swiper-vertical-03.mp4'
  20. },
  21. {
  22. src: 'https://img.cdn.aliyun.dcloud.net.cn/guide/uniapp/hellouniapp/hello-nvue-swiper-vertical-01.mp4'
  23. },
  24. {
  25. src: 'https://img.cdn.aliyun.dcloud.net.cn/guide/uniapp/hellouniapp/hello-nvue-swiper-vertical-02.mp4'
  26. },
  27. {
  28. src: 'https://img.cdn.aliyun.dcloud.net.cn/guide/uniapp/hellouniapp/hello-nvue-swiper-vertical-03.mp4'
  29. }
  30. ];
  31. export default {
  32. data() {
  33. return {
  34. circular: true,
  35. videoList: [{
  36. id: "video0",
  37. src: "",
  38. img: ""
  39. },
  40. {
  41. id: "video1",
  42. src: "",
  43. img: ""
  44. },
  45. {
  46. id: "video2",
  47. src: "",
  48. img: ""
  49. }
  50. ],
  51. videoDataList: []
  52. }
  53. },
  54. onLoad(e) {},
  55. onReady() {
  56. this.init();
  57. this.getData();
  58. },
  59. methods: {
  60. init() {
  61. this._videoIndex = 0;
  62. this._videoContextList = [];
  63. for (var i = 0; i < this.videoList.length; i++) {
  64. this._videoContextList.push(uni.createVideoContext('video' + i, this));
  65. }
  66. this._videoDataIndex = 0;
  67. },
  68. getData(e) {
  69. this.videoDataList = videoData;
  70. setTimeout(() => {
  71. this.updateVideo(true);
  72. }, 200)
  73. },
  74. onSwiperChange(e) {
  75. let currentIndex = e.detail.current;
  76. if (currentIndex === this._videoIndex) {
  77. return;
  78. }
  79. let isNext = false;
  80. if (currentIndex === 0 && this._videoIndex === this.videoList.length - 1) {
  81. isNext = true;
  82. } else if (currentIndex === this.videoList.length - 1 && this._videoIndex === 0) {
  83. isNext = false;
  84. } else if (currentIndex > this._videoIndex) {
  85. isNext = true;
  86. }
  87. if (isNext) {
  88. this._videoDataIndex++;
  89. } else {
  90. this._videoDataIndex--;
  91. }
  92. if (this._videoDataIndex < 0) {
  93. this._videoDataIndex = this.videoDataList.length - 1;
  94. } else if (this._videoDataIndex >= this.videoDataList.length) {
  95. this._videoDataIndex = 0;
  96. }
  97. this.circular = (this._videoDataIndex != 0);
  98. if (this._videoIndex >= 0) {
  99. this._videoContextList[this._videoIndex].pause();
  100. this._videoContextList[this._videoIndex].seek(0);
  101. }
  102. this._videoIndex = currentIndex;
  103. setTimeout(() => {
  104. this.updateVideo(isNext);
  105. }, 200);
  106. },
  107. getNextIndex(isNext) {
  108. let index = this._videoIndex + (isNext ? 1 : -1);
  109. if (index < 0) {
  110. return this.videoList.length - 1;
  111. } else if (index >= this.videoList.length) {
  112. return 0;
  113. }
  114. return index;
  115. },
  116. getNextDataIndex(isNext) {
  117. let index = this._videoDataIndex + (isNext ? 1 : -1);
  118. if (index < 0) {
  119. return this.videoDataList.length - 1;
  120. } else if (index >= this.videoDataList.length) {
  121. return 0;
  122. }
  123. return index;
  124. },
  125. updateVideo(isNext) {
  126. this.$set(this.videoList[this._videoIndex], 'src', this.videoDataList[this._videoDataIndex].src);
  127. this.$set(this.videoList[this.getNextIndex(isNext)], 'src', this.videoDataList[this.getNextDataIndex(isNext)].src);
  128. setTimeout(() => {
  129. this._videoContextList[this._videoIndex].play();
  130. }, 200);
  131. console.log("v:" + this._videoIndex + " d:" + this._videoDataIndex + "; next v:" + this.getNextIndex(
  132. isNext) + " next d:" + this.getNextDataIndex(isNext));
  133. }
  134. }
  135. }
  136. </script>
  137. <style>
  138. /* #ifndef H5 */
  139. /* page {
  140. width: 100%;
  141. min-height: 100%;
  142. display: flex;
  143. } */
  144. /* #endif */
  145. .page {
  146. flex: 1;
  147. /* width: 750rpx; */
  148. }
  149. .swiper {
  150. flex: 1;
  151. background-color: #007AFF;
  152. }
  153. .swiper-item {
  154. flex: 1;
  155. }
  156. .video {
  157. flex: 1;
  158. /* #ifndef APP-PLUS */
  159. width: 100%;
  160. /* #endif */
  161. }
  162. </style>