微信小程序视频加轮播图示例

  • index.html
  1. <view style="padding:0" data-e="{{e}}" bindtouchstart="start" bindtouchend="end">
  2. <swiper wx:if="{{!videoSrc}}" current="{{current}}" class="swiper" circular="{{true}}" indicator-dots="{{true}}" data-e="{{e}}" bindchange="changeCurrent">
  3. <view wx:for="{{info}}" wx:key="this">
  4. <swiper-item>
  5. <image src="{{item.img}}" class="banner" mode='aspectFill' />
  6. <image class="play" wx:if="{{item.type == 'video'}}" src='/images/play.png'
  7. bindtap="play" data-item="{{item}}"/>
  8. </swiper-item>
  9. </view>
  10. </swiper>
  11. <!-- 视频的autoplay和controls属性要加上,不加有的安卓手机在缓冲的时候没有loading效果 -->
  12. <video enable-progress-gesture="{{false}}" custom-catch="{{false}}" wx:if="{{videoSrc}}" class="video" autoplay="{{true}}" src="{{videoSrc}}" controls="controls" bindpause="handleStop" ></video>
  13. </view>
  • index.js
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. info: [],
  7. videoSrc:'',
  8. videoImg:'',//视频封面,缓冲时会出现黑屏,加视频封面会提升用户体验
  9. autoplay:true,
  10. touchX:0,//手指按下时x的坐标
  11. touchY:0,//手指按下时y的坐标
  12. interval:null,//计时器
  13. time:0,//按下到松开的时间
  14. current:0//swiper的当前轮播图下标
  15. },
  16. //事件处理函数
  17. play: function(val) {
  18. this.setData({ videoSrc: val.currentTarget.dataset.item.video, autoplay: false, videoImg: val.currentTarget.dataset.item.img})
  19. },
  20. //禁止视频的手动控制进度属性,监听手指移动去滑动轮播图(手指滑动轮播图和控制视频进度事件冲突)
  21. //手指开始触屏
  22. start:function(e){
  23. //获取触摸的原始点
  24. this.setData({
  25. touchX: e.touches.length>0 ? e.touches[0].pageX : 0,
  26. touchY: e.touches.length > 0 ? e.touches[0].pageY : 0
  27. })
  28. let timeNew=this.data.time
  29. //开始记录时间
  30. this.data.interval=setInterval(()=>timeNew++,100)
  31. this.setData({time:timeNew})
  32. },
  33. //手指结束触屏
  34. end:function(e){
  35. let touchX = e.changedTouches.length > 0 ? e.changedTouches[0].pageX : 0
  36. let touchY = e.changedTouches.length > 0 ? e.changedTouches[0].pageY : 0
  37. let tmX = touchX - this.data.touchX
  38. let tmY = touchY - this.data.touchY
  39. if(this.data.time < 10){
  40. let absX = Math.abs(tmX)
  41. let absY = Math.abs(tmY)
  42. if(absX > 2*absY){
  43. console.log('5555')
  44. //滑动swiper,视频停止播放
  45. this.setData({
  46. autoplay:true,
  47. videoSrc:'',
  48. videoImg:''
  49. })
  50. if(tmX < 0){
  51. //左滑
  52. console.log('左滑')
  53. this.setData({
  54. current : this.data.current == (this.data.info.length-1) ? 0 : this.data.current+1
  55. })
  56. }else{
  57. //右滑
  58. console.log('右滑')
  59. this.setData({
  60. current : this.data.current>0 ? this.data.current-1 : this.data.info.length-1
  61. })
  62. }
  63. }
  64. }
  65. clearInterval(this.data.interval)
  66. this.setData({time:0})
  67. },
  68. handleStop:function(){
  69. this.setData({ videoSrc: '', autoplay: true, videoImg:''})
  70. },
  71. changeCurrent:function(e){
  72. //手指滑动轮播图已经在视频播放的时候做了,这里只需要做轮播图自动滚动,但是不停的调用setData可能会出现一些未知的bug,可根据需求场景设置
  73. if(e.detail.source == 'autoplay'){
  74. this.setData({current:e.detail.current})
  75. }
  76. },
  77. onShow: function() {
  78. //从后台拿回数据,写在onshow里面是因为返回此页面时,根据需求去判断是否拿swiper数据
  79. let info = [{
  80. id: '1',
  81. img: 'http://img4.imgtn.bdimg.com/it/u=3882508552,2527877926&fm=26&gp=0.jpg',
  82. type: 'video',
  83. video: 'https://interface.sina.cn/wap_api/video_location.d.html?cid=37766&table_id=36885&did=icezueu6231605&vt=4&creator_id=1001&vid=30447305701&video_id=304473057&r=video.sina.cn%2Fnews%2F2019-09-16%2Fdetail-iicezueu6231605.d.html&wm=30490005525198963&time=1577412073497&rd=0.018419081320328656'
  84. },
  85. {
  86. id: '2',
  87. img: 'http://img2.imgtn.bdimg.com/it/u=3355791600,110151422&fm=26&gp=0.jpg',
  88. type: 'img',
  89. video: ''
  90. },
  91. {
  92. id: '3',
  93. img: 'http://img3.imgtn.bdimg.com/it/u=2846566051,411513524&fm=26&gp=0.jpg',
  94. type: 'img',
  95. video: ''
  96. }
  97. ]
  98. this.setData({info})
  99. },
  100. onHide:function(){
  101. //小程序进入后台禁止轮播,防止swiper出现抽搐的bug
  102. this.setData({
  103. autoplay:false,
  104. videoSrc:'',
  105. })
  106. }
  107. })
  • index.wxss

    1. .swiper{
    2. width:100%;
    3. height:450rpx;
    4. }
    5. .banner{
    6. width:100%;
    7. height:100%;
    8. }
    9. .play{
    10. width:100rpx;
    11. height:100rpx;
    12. position:absolute;
    13. top:175rpx;
    14. left:50%;
    15. margin-left:-50rpx;
    16. }
    17. .video{
    18. width:100%;
    19. height:450rpx;
    20. position:absolute;
    21. top:0;
    22. }
  • 效果图

github 项目地址:https://github.com/kesixin/VideoDemo



评论 0

发表评论

Top