• uniapp上下滑屏切换支持视频和图片轮播实现,类似抖音效果


    实现方法:

    swiper中嵌套swiper,通过给swiper设置自定义属性,判断数据类型是图片还是视频,从而控制视频自动播放和图片轮播效果:

    代码:

    1. <template>
    2. <swiper class="swiper" :vertical="true" @change="change" @touchstart="touchStart" @touchend="touchEnd">
    3. <block v-for="item of videoList" :key="item.id">
    4. <swiper-item ref="swiperItem" data-type="video" class="swiper-item" v-if="item.type === 'video'">
    5. <videoPlayer :video="item" ref="player">videoPlayer>
    6. swiper-item>
    7. <swiper-item ref="swiperItem" data-type="img" v-else>
    8. <swiper :style="{height:screenHeight+'px'}" class="img-boxs" circular :indicator-dots="true"
    9. :autoplay="true" :current="currentImgIndex" @change="changeswiper">
    10. <swiper-item v-for="(item, index) in item.imgList" :key="index">
    11. <image :src="item" mode="scaleToFill" class="swiper-img" :indicator-dots="true">image>
    12. swiper-item>
    13. swiper>
    14. swiper-item>
    15. block>
    16. swiper>
    17. template>
    18. <script>
    19. import videoPlayer from "./components/videoPlayer.vue"
    20. var time = null
    21. export default {
    22. props: ["myList"],
    23. components: {
    24. videoPlayer,
    25. },
    26. name: "video-list",
    27. data() {
    28. return {
    29. videoList: [{
    30. id: 7,
    31. type: "image",
    32. imgList: [
    33. 'https://img-blog.csdnimg.cn/img_convert/4ce460a31366926464acc88c991721d9.jpeg',
    34. 'https://img-blog.csdnimg.cn/img_convert/a8d701d5adc9008ca018905f43c34145.jpeg',
    35. 'https://img-blog.csdnimg.cn/img_convert/3090b46bd91e5c7ee5d78d988fbae0bd.jpeg',
    36. "https://img-blog.csdnimg.cn/img_convert/08acb513a12e1d7d654cc43cf4e2ee48.jpeg",
    37. "https://img-blog.csdnimg.cn/img_convert/eebf7eefba5849ef9788e32c148061c8.jpeg"
    38. ],
    39. }, {
    40. id: 1,
    41. type: "video",
    42. preImg: "http://p1-q.mafengwo.net/s16/M00/8D/4D/CoUBUmFZOWKAA8mQAA8Oww0vs7k240.jpg",
    43. src: "https://txmov2.a.yximgs.com/upic/2020/03/14/16/BMjAyMDAzMTQxNjIwMDlfMTI0OTQzNzMzOV8yNDk0OTExNDY1NF8xXzM=_b_B8cf5a4391531e161385a32cdadfef87c.mp4"
    44. },
    45. {
    46. id: 8,
    47. type: "image",
    48. imgList: [
    49. 'https://img-blog.csdnimg.cn/img_convert/4ce460a31366926464acc88c991721d9.jpeg',
    50. 'https://img-blog.csdnimg.cn/img_convert/a8d701d5adc9008ca018905f43c34145.jpeg',
    51. 'https://img-blog.csdnimg.cn/img_convert/3090b46bd91e5c7ee5d78d988fbae0bd.jpeg',
    52. "https://img-blog.csdnimg.cn/img_convert/08acb513a12e1d7d654cc43cf4e2ee48.jpeg",
    53. "https://img-blog.csdnimg.cn/img_convert/eebf7eefba5849ef9788e32c148061c8.jpeg"
    54. ],
    55. }, {
    56. id: 2,
    57. type: "video",
    58. preImg: "http://b1-q.mafengwo.net/s16/M00/23/F3/CoUBUmFbN5OAGqEkAA4O0V-U1uo031.jpg",
    59. src: "https://txmov2.a.yximgs.com/upic/2020/10/02/09/BMjAyMDEwMDIwOTAwMDlfMTIyMjc0NTk0Ml8zNjk3Mjg0NjcxOF8xXzM=_b_B28a4518e86e2cf6155a6c1fc9cf79c6d.mp4"
    60. },
    61. {
    62. id: 3,
    63. type: "video",
    64. preImg: "http://p1-q.mafengwo.net/s16/M00/23/F4/CoUBUmFbN5WAbMikAA5cYlWno5U709.jpg",
    65. src: "https://txmov6.a.yximgs.com/upic/2020/08/23/00/BMjAyMDA4MjMwMDMyNDRfMTYzMzY5MDA0XzM0ODI4MDcyMzQ5XzFfMw==_b_B9a1c9d4e3a090bb2815994d7f33a906a.mp4"
    66. },
    67. {
    68. id: 4,
    69. type: "video",
    70. preImg: "http://p1-q.mafengwo.net/s18/M00/E6/C0/CoUBYGFceTyAOSBqABFXcMPFJ1w112.jpg",
    71. src: "https://alimov2.a.yximgs.com/upic/2020/03/04/15/BMjAyMDAzMDQxNTU3MTdfMzE3MDM5OTAzXzI0NDUwNjQ1MzgxXzFfMw==_b_B53c20819cb9a2103b9805b0144f545cf.mp4"
    72. },
    73. {
    74. id: 5,
    75. type: "video",
    76. preImg: "http://p1-q.mafengwo.net/s18/M00/E6/C0/CoUBYGFceTyAOSBqABFXcMPFJ1w112.jpg",
    77. src: "https://txmov2.a.yximgs.com/upic/2020/07/14/16/BMjAyMDA3MTQxNjEyNDBfMTU3MDQyNjA0XzMyMzUyOTQ4NzM0XzJfMw==_b_Bd53949260b17aaa7b712526aa61e590a.mp4"
    78. },
    79. {
    80. id: 6,
    81. type: "video",
    82. preImg: "http://p1-q.mafengwo.net/s18/M00/E6/C0/CoUBYGFceTyAOSBqABFXcMPFJ1w112.jpg",
    83. src: "https://txmov2.a.yximgs.com/upic/2020/09/28/15/BMjAyMDA5MjgxNTAwMTlfNjk3OTM4NjUzXzM2NzQyNzU5MzA2XzFfMw==_b_B6e8aca57e704ac40b34856eb663b8d57.mp4"
    84. },
    85. ],
    86. screenHeight: 0,
    87. currentImgIndex: 0
    88. };
    89. },
    90. mounted() {
    91. uni.getSystemInfo({
    92. success: (res) => {
    93. console.log("首页获取到的页面高度:windowHeight", res.windowHeight);
    94. this.screenHeight = res.windowHeight;
    95. }
    96. });
    97. },
    98. methods: {
    99. change(res) {
    100. clearTimeout(time)
    101. this.page = res.detail.current
    102. let swiperPre = this.$refs.swiperItem[this.page - 1]
    103. let swiperItem = this.$refs.swiperItem[this.page]
    104. let swiperNext = this.$refs.swiperItem[this.page + 1]
    105. let currentType = swiperItem.$attrs["data-type"]
    106. time = setTimeout(() => {
    107. if (this.pageStartY > this.pageEndY) {
    108. console.log("向上滑动当前页:" + this.page);
    109. console.log("改变当前显示图片索引:", this.currentImgIndex);
    110. if (currentType === "video") {
    111. swiperItem.$children[0].playVideo()
    112. this.pageStartY = 0
    113. this.pageEndY = 0
    114. } else {
    115. console.log("改变当前显示图片索引:", this.currentImgIndex);
    116. this.$nextTick(() => {
    117. this.currentImgIndex = 0
    118. })
    119. }
    120. if (swiperPre.$attrs["data-type"] === "video") {
    121. swiperPre.$children[0].pauseVideo()
    122. }
    123. } else {
    124. console.log("向下滑动" + this.page);
    125. console.log("改变当前显示图片索引:", this.currentImgIndex);
    126. if (currentType === "video") {
    127. swiperItem.$children[0].playVideo()
    128. this.pageStartY = 0
    129. this.pageEndY = 0
    130. } else {
    131. console.log("改变当前显示图片索引:", this.currentImgIndex);
    132. this.$nextTick(() => {
    133. this.currentImgIndex = 0
    134. })
    135. }
    136. if (swiperNext.$attrs["data-type"] === "video") {
    137. swiperNext.$children[0].pauseVideo()
    138. }
    139. }
    140. }, 1)
    141. },
    142. touchStart(res) {
    143. this.pageStartY = res.changedTouches[0].pageY;
    144. console.log(this.pageStartY);
    145. },
    146. touchEnd(res) {
    147. this.pageEndY = res.changedTouches[0].pageY;
    148. console.log(this.pageEndY);
    149. },
    150. changeswiper(e) {
    151. this.currentImgIndex = e.detail.current
    152. }
    153. },
    154. watch: {
    155. myList() {
    156. this.list = this.myList;
    157. }
    158. }
    159. }
    160. script>
    161. <style>
    162. .swiper {
    163. width: 100vw;
    164. height: 100vh;
    165. }
    166. .swiper-item {
    167. width: 100vw;
    168. height: 100vh;
    169. z-index: 9;
    170. }
    171. .swiper-img {
    172. width: 100%;
    173. height: 100%;
    174. }
    175. .img-boxs {
    176. /* height: 100%; */
    177. }
    178. /* .left-box {
    179. z-index: 20;
    180. position: absolute;
    181. bottom: 50px;
    182. left: 10px;
    183. }
    184. .right-box {
    185. z-index: 20;
    186. position: absolute;
    187. bottom: 50px;
    188. right: 10px;
    189. } */
    190. style>

  • 相关阅读:
    计算机视觉与深度学习 | SLAM国内外研究现状
    mysql数据库的基本管理
    C/C++面试题分享「虚函数、多态、内存管理与软件调试篇」
    LeetCode - 解题笔记 - 212 - Word Search II
    目标检测(7)—— YOLO系列V3
    JavaSE进阶21天---第十七天---JavaSE(打印流、FileUtils工具类、 ImageIO(验证码、二维码))
    Mallab勒索病毒的最新威胁:如何恢复您的数据?
    ppt插入html(用office而不是wps)
    SkiaSharp 之 WPF 自绘 弹动小球(案例版)
    路径总和II-力扣113-C++
  • 原文地址:https://blog.csdn.net/weixin_44786530/article/details/126021351