scroll-video.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // components/scroll-video.js
  2. Component({
  3. options: {
  4. multipleSlots: true // 在组件定义时的选项中启用多slot支持
  5. },
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. //父组件传入的视频列表
  11. videoList:{
  12. type: Array,
  13. value: [],
  14. observer: function(newVal, oldVal){
  15. this.setData({
  16. videoSize: newVal.length
  17. });
  18. }
  19. },
  20. //视频或者直播适配页面方式
  21. fitType:{
  22. type: String,
  23. value: 'contain',
  24. },
  25. //滑动距离的设置 超过该距离回出现页面下滑或者上滑的情况
  26. thresholdValue:{
  27. type: Number,
  28. value: 100,
  29. observer: function (newVal, oldVal) {
  30. console.log(newVal,oldVal);
  31. }
  32. },
  33. //播放器类型
  34. playerType:{
  35. type: String,
  36. value: 'video',
  37. observer: function (newVal, oldVal) {
  38. }
  39. }
  40. },
  41. /**
  42. * 组件的初始数据
  43. */
  44. data: {
  45. startY: 0,//开始y点
  46. screenHeight:0,//获取当前屏幕高度
  47. screenWidth:0,
  48. scrollAnimate:0,
  49. videoidx:0,//保存切换下标
  50. videoSize:0,//视频列表的长度
  51. },
  52. ready:function(){
  53. this.animation = wx.createAnimation({
  54. duration:600,
  55. timingFunction:'linear',
  56. });
  57. let that = this;
  58. wx.getSystemInfo({
  59. success: function (res) {
  60. that.setData({
  61. screenHeight: res.windowHeight,
  62. screenWidth: res.windowWidth
  63. })
  64. }
  65. })
  66. console.log(this.properties.videoList.length);
  67. this.setData({
  68. videoSize:this.properties.videoList.length
  69. });
  70. },
  71. /**
  72. * 组件的方法列表
  73. */
  74. methods: {
  75. buttonhandle:function(e){
  76. const { buttontype, buttonname, itemid } = e.detail;
  77. this.triggerEvent('menuTap', { buttontype, buttonname, itemid });
  78. },
  79. onTouchStart: function (e) {
  80. const {pageY } = e.changedTouches[0]; //记录手指位置
  81. this.setData({
  82. startY: pageY
  83. });
  84. },
  85. onTouchEnd: function (e) {
  86. let { videoidx } = e.currentTarget.dataset;
  87. videoidx = parseInt(videoidx)
  88. console.log(videoidx);
  89. let thresholdValue = this.properties.thresholdValue;
  90. const {startY } = this.data;
  91. let movey = e.changedTouches[0].pageY;
  92. let changeY = movey - startY;
  93. if (changeY > 0) {
  94. if (changeY >= thresholdValue) {
  95. if (videoidx===0){
  96. this.triggerEvent('swipeToStart', {
  97. oldindex: 0,
  98. newindex: videoidx,
  99. playerType: this.properties.playerType
  100. });
  101. return false;
  102. }
  103. let top_height = -((videoidx - 1) * this.data.screenHeight);
  104. console.log('手指向下滑动,往上切换视频');
  105. this.triggerEvent('swipeDown',{
  106. oldindex: videoidx,
  107. newindex: videoidx-1,
  108. playerType: this.properties.playerType
  109. });
  110. this.animation.translateY(top_height).step();
  111. this.setData({
  112. scrollAnimate: this.animation.export(),
  113. videoidx: videoidx,
  114. });
  115. }
  116. }else{
  117. let abschangeY = Math.abs(changeY);
  118. if (abschangeY >= thresholdValue) {
  119. if (videoidx+1 === this.data.videoSize) {
  120. this.triggerEvent('swipeToEnd', {
  121. oldindex: videoidx + 1,
  122. newindex:videoidx,
  123. playerType: this.properties.playerType
  124. });
  125. return false;
  126. }
  127. let btm_height = -((videoidx + 1) * this.data.screenHeight);
  128. this.triggerEvent('swipeUpper', {
  129. oldindex: videoidx,
  130. newindex: videoidx +1,
  131. playerType: this.properties.playerType
  132. });
  133. this.animation.translateY(btm_height).step();
  134. console.log('向上滑动,往下切换视频');
  135. this.setData({
  136. scrollAnimate:this.animation.export(),
  137. videoidx: videoidx,
  138. });
  139. }
  140. }
  141. },
  142. }
  143. })