当前位置:   article > 正文

微信小程序swiper 视频中间大,两边小,轮播滑到中间视频自动播放组件教程_微信小程序swiper插件

微信小程序swiper插件

静态效果:

 进入下面小程序可以体验效果,点击底部 看剧 栏目 

 一、创建小程序组件

二、代码

1、WXML

  1. <view class="swiper-wrapper">
  2. <swiper
  3. class="main-sw"
  4. autoplay="{{false}}"
  5. circular="{{true}}"
  6. interval="{{5000}}"
  7. duration="{{500}}"
  8. current="{{posterList.length>2?1:0}}"
  9. previous-margin="255rpx"
  10. next-margin="255rpx"
  11. bindchange="swiperChange"
  12. >
  13. <block wx:for="{{posterList}}" wx:key="index">
  14. <swiper-item >
  15. <view class="swiper-item {{currentIndex==index?'swiper-item-active':'swiper-item-noactive'}}">
  16. <video
  17. class="vie"
  18. id="{{'at_'+index}}"
  19. custom-cache="{{false}}"
  20. autoplay="{{currentIndex==index}}"
  21. data-index="{{index}}"
  22. bindplay="videoPlaying"
  23. play-btn-position="center"
  24. show-bottom-progress="{{false}}"
  25. loop="{{currentIndex==index}}"
  26. enable-progress-gesture="{{false}}"
  27. show-fullscreen-btn="{{false}}"
  28. show-play-btn="{{currentIndex==index}}"
  29. show-center-play-btn="{{currentIndex==index}}"
  30. object-fit="cover"
  31. src="{{item.url}}">
  32. <image wx:if="{{currentIndex!=index}}" style="width: 100%;" mode="aspectFill" src="{{item.coverUrl}}"></image>
  33. </video>
  34. <view wx:if="{{currentIndex==index}}" class="full-vw" data-oj="{{item}}" bind:tap="fullView">点击全屏</view>
  35. </view>
  36. </swiper-item>
  37. </block>
  38. </swiper>
  39. </view>

2、wxss

  1. .swiper-wrapper{
  2. flex: 1;
  3. width: 100%;
  4. height: 100%;
  5. margin: 0 auto;
  6. border-radius: 20rpx;
  7. background-image:url(https://pic1.zhimg.com/80/v2-1476ad08ba6e8e0aa062d8a6de699e72_720w.jpeg?source=d16d100b);
  8. background-size: 100% 100%;
  9. }
  10. .main-sw{
  11. width: 100%;
  12. height: 430rpx;
  13. }
  14. .swiper-item{
  15. height: 450rpx;
  16. display: flex;
  17. align-items: center;
  18. }
  19. .swiper-item .vie{
  20. width: 180rpx;
  21. height: 300rpx;
  22. margin: 0 auto;
  23. border-radius: 20rpx;
  24. transition: all 0.6s;
  25. }
  26. .swiper-item-noactive{
  27. padding-top: 30rpx;
  28. transition: all 0.6s;
  29. }
  30. .swiper-item-active{
  31. transition: all 0.6s;
  32. }
  33. .swiper-item-active .vie{
  34. width: 100%;
  35. height: 360rpx;
  36. transition: all 0.6s;
  37. }
  38. .full-vw{
  39. right: 20rpx;
  40. width: 130rpx;
  41. position: fixed;
  42. bottom: 15rpx;
  43. color: white;
  44. border-radius: 50rpx;
  45. align-items: center;
  46. justify-content: center;
  47. margin: 20rpx;
  48. display: flex;
  49. font-family: auto;
  50. font-size: small;
  51. padding: 2px;
  52. backdrop-filter: blur(10px);
  53. background: #65656594;
  54. }

 3、JS

  1. // components/swiper-video/swiper-video.js
  2. Component({
  3. behaviors: ['wx://component-export'],
  4. export() {
  5. return this;
  6. },
  7. lifetimes: {
  8. ready: function() {
  9. }
  10. },
  11. /**
  12. * 组件的属性列表
  13. */
  14. properties: {
  15. },
  16. /**
  17. * 组件的初始数据
  18. */
  19. data: {
  20. currentIndex: 1,
  21. preIndex:-1,
  22. posterList: [
  23. {
  24. url: 'https://tx2.a.kwimgs.com/upic/2024/02/15/15/BMjAyNDAyMTUxNTQ0NTNfMjI1MzQ2MDQxMV8xMjUwMDcyMDgyODZfMV8z_b_B5e4c6a553c4e284e7941a0ded6c00abb.mp4?tag=1-1708249770-unknown-0-p3ytzdyhr9-044ff73795d79af2&clientCacheKey=3xypn8bt5vm7w5q_b.mp4&bp=10000&tt=b&ss=vp',
  25. title:'01',
  26. coverUrl:'https://pic3.zhimg.com/v2-e392c3b92ab5e833e9b6911d4d512073_r.jpg?source=1940ef5c'
  27. },
  28. {
  29. url: 'https://tx2.a.kwimgs.com/upic/2024/01/30/16/BMjAyNDAxMzAxNjQwMzJfMzgyMzQ3MjQ1XzEyMzUyMTMwODU4NF8xXzM=_b_Be0f3bfddfb287f120ec484218179d214.mp4?tag=1-1708249795-unknown-0-4piuyvkt6d-783157ecbf33a1a3&clientCacheKey=3xpwz2fks5az6r4_b.mp4&bp=10000&tt=b&ss=vp',
  30. title:'02',
  31. coverUrl:'https://pic3.zhimg.com/v2-e392c3b92ab5e833e9b6911d4d512073_r.jpg?source=1940ef5c'
  32. },
  33. {
  34. url: 'https://tx2.a.kwimgs.com/upic/2024/02/17/14/BMjAyNDAyMTcxNDI0NDZfMTI5OTg4NTg4NV8xMjUxODQ5NTczNDRfMF8z_b_B3c596afe9e6ddc96e88d71351da052ac.mp4?tag=1-1708249822-unknown-0-ab5yaqiqon-f89388c9a0fbea5f&clientCacheKey=3x8669dhkxfztyc_b.mp4&bp=10000&tt=b&ss=vp',
  35. title:'03',
  36. coverUrl:'https://pic3.zhimg.com/v2-e392c3b92ab5e833e9b6911d4d512073_r.jpg?source=1940ef5c'
  37. },
  38. {
  39. url: 'https://tx2.a.kwimgs.com/upic/2024/02/17/17/BMjAyNDAyMTcxNzExMDRfMjM0NzM2OTY3OV8xMjUxOTkzNTUwOTlfMF8z_b_B531ec3a51ae00fdf805e892aefbf6733.mp4?tag=1-1708249852-unknown-0-jde8ia4ef8-80944aff380e6a62&clientCacheKey=3xzj5p23mfrrsm9_b.mp4&bp=10000&tt=b&ss=vp'
  40. title:'04',
  41. coverUrl:'https://pic3.zhimg.com/v2-e392c3b92ab5e833e9b6911d4d512073_r.jpg?source=1940ef5c'
  42. },
  43. {
  44. url: 'https://tx2.a.kwimgs.com/upic/2024/02/18/11/BMjAyNDAyMTgxMTAyMzdfMTYzNTI5NjEzNV8xMjUyNjE0MjQ4NTJfMF8z_b_B3da4064bfef058d0c2821f64f24d863e.mp4?tag=1-1708249875-unknown-0-oudstqms9g-770ac56e97209131&clientCacheKey=3xfywqaqhr4xav4_b.mp4&bp=10000&tt=b&ss=vp',
  45. adUrl:'https://kurl01.cn/7wcG5c',
  46. title:'05',
  47. coverUrl:'https://pic3.zhimg.com/v2-e392c3b92ab5e833e9b6911d4d512073_r.jpg?source=1940ef5c'
  48. },
  49. {
  50. url: 'https://tx2.a.kwimgs.com/upic/2023/12/08/14/BMjAyMzEyMDgxNDA1NThfMjEwNjQzMjExMV8xMTkxMTE3NDE1NzVfMV8z_b_Ba62e8a6ff0405a74160c6887700f4d6c.mp4?tag=1-1708177895-unknown-0-9bhguogrkc-94f990275f193667&clientCacheKey=3xsxvbspa44ezue_b.mp4&bp=10000&tt=b&ss=vp',
  51. title:'06',
  52. coverUrl:'https://pic3.zhimg.com/v2-e392c3b92ab5e833e9b6911d4d512073_r.jpg?source=1940ef5c'
  53. },
  54. {
  55. url: 'https://tx2.a.kwimgs.com/upic/2024/01/17/13/BMjAyNDAxMTcxMzMyMDZfMzg1MTI1NDMwMV8xMjIzMjA4MTg1MzJfMl8z_b_B64de3e477a3c56931c2e1c869455bb8a.mp4?tag=1-1708249935-unknown-0-8hoxqf5mqv-06a7d7fd75d38e4b&clientCacheKey=3xns4fd3r5iw3mq_b.mp4&bp=10000&tt=b&ss=vp',
  56. adUrl:'https://kurl01.cn/7wcG5c',
  57. title:'07',
  58. coverUrl:'https://pic3.zhimg.com/v2-e392c3b92ab5e833e9b6911d4d512073_r.jpg?source=1940ef5c'
  59. },
  60. {
  61. url: 'https://tx2.a.kwimgs.com/upic/2024/01/02/19/BMjAyNDAxMDIxOTIxMDNfMzg4MTc4NzIwM18xMjEyMTY2NjYzOTNfMl8z_b_B7243f24b02219e4fafb2aa4e92cc5395.mp4?tag=1-1708249950-unknown-0-p2dy8mumai-fe39fb7133a179af&clientCacheKey=3xjxfvd5cnhip2u_b.mp4&bp=10000&tt=b&ss=vp',
  62. title:'08',
  63. coverUrl:'https://pic3.zhimg.com/v2-e392c3b92ab5e833e9b6911d4d512073_r.jpg?source=1940ef5c'
  64. },
  65. {
  66. url: 'https://tx2.a.kwimgs.com/upic/2024/02/16/17/BMjAyNDAyMTYxNzU3MTRfMTU2ODQ2MzM2Ml8xMjUxMTA1MDIwMDZfMV8z_b_B74927e5fb4232fc899c57f2e68fa5582.mp4?tag=1-1708249967-unknown-0-vrnqcigp6e-3d0aa458f1aaf315&clientCacheKey=3xu5uaiuju6y5xu_b.mp4&bp=10000&tt=b&ss=vp',
  67. title:'09',
  68. coverUrl:'https://pic3.zhimg.com/v2-e392c3b92ab5e833e9b6911d4d512073_r.jpg?source=1940ef5c'
  69. },
  70. {
  71. url: 'https://tx2.a.kwimgs.com/upic/2024/01/01/11/BMjAyNDAxMDExMTAxMjhfMTk3MDc5MTUxMV8xMjExMDExOTE1ODVfMV8z_b_B6fc046ba1a3cc0b114ffb7a3feb87c68.mp4?tag=1-1708250014-unknown-0-glxnbdhuvo-79c4b0891fd217ee&clientCacheKey=3x43rid7dz3y7pk_b.mp4&bp=10000&tt=b&ss=vp',
  72. title:'10',
  73. coverUrl:'https://pic3.zhimg.com/v2-e392c3b92ab5e833e9b6911d4d512073_r.jpg?source=1940ef5c'
  74. },
  75. ]
  76. },
  77. /**
  78. * 组件的方法列表
  79. */
  80. methods: {
  81. //视频切换
  82. swiperChange(event){
  83. if(this.data.preIndex>-1){
  84. var cxt = wx.createVideoContext('at_'+this.data.preIndex, this);
  85. cxt.stop();
  86. //将视频重头开始播放
  87. cxt.seek(10000);
  88. }
  89. let {current} = event.detail;
  90. var cxt = wx.createVideoContext('at_'+current, this);
  91. cxt.play();
  92. this.setData({
  93. currentIndex: current,
  94. preIndex: current
  95. })
  96. },
  97. //当前正在播放视频
  98. videoPlaying(e){
  99. this.setData({
  100. preIndex: e.currentTarget.dataset.index
  101. })
  102. },
  103. /**
  104. * 当前视频暂停(父组件可调用)
  105. */
  106. videoStop(){
  107. var cxt = wx.createVideoContext('at_'+this.data.currentIndex, this);
  108. cxt.stop();
  109. //将视频重头开始播放
  110. cxt.seek(10000);
  111. },
  112. /**
  113. * 当前视频播放(父组件可调用)
  114. */
  115. videoPlay(){
  116. var cxt = wx.createVideoContext('at_'+this.data.currentIndex, this);
  117. cxt.play();
  118. },
  119. /**
  120. * 全屏观看
  121. */
  122. fullView(e){
  123. wx.navigateTo({
  124. url: '/pages/videoview/videoview',
  125. success: function(res) {
  126. // 通过eventChannel向被打开页面传送数据
  127. res.eventChannel.emit('acceptDataFromOpenerPage', {
  128. oj:e.currentTarget.dataset.oj
  129. })
  130. }
  131. })
  132. }
  133. }
  134. })

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

闽ICP备14008679号