index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. Component({
  2. /**
  3. * 组件的属性列表
  4. */
  5. properties: {
  6. previewImgList: {
  7. type: Array,
  8. value: [],
  9. observer: function(newVal) {
  10. this.initializeImgIndex();
  11. }
  12. },
  13. previewImg: {
  14. type: String,
  15. value: '',
  16. observer: function(newVal) {
  17. this.initializeImgIndex();
  18. }
  19. },
  20. previewHideStatus: {
  21. type: Boolean,
  22. value: false
  23. }
  24. },
  25. /**
  26. * 组件的初始数据
  27. */
  28. data: {
  29. index: 0,
  30. imgindex: 1,
  31. left: '0%',
  32. scaleObj: {
  33. scale: 1,
  34. x: 0,
  35. y: 0,
  36. yes: true
  37. },
  38. touchS: [],
  39. touchE: [],
  40. preview_box_top: '0%' // 初始化预览框位置
  41. },
  42. lifetimes: {
  43. detached: function () {
  44. // 在组件实例被从页面节点树移除时执行
  45. },
  46. attached: async function () {
  47. // 在组件实例进入页面节点树时执行
  48. this.initializeImgIndex();
  49. },
  50. },
  51. /**
  52. * 组件的方法列表
  53. */
  54. methods: {
  55. initializeImgIndex: function () {
  56. const {
  57. previewImgList,
  58. previewImg
  59. } = this.properties;
  60. console.log(this.properties);
  61. if (previewImgList.length > 0 && previewImg) {
  62. const index = previewImgList.findIndex(img => img === previewImg);
  63. if (index !== -1) {
  64. this.setData({
  65. index: index,
  66. imgindex: index + 1, // imgindex 从 1 开始
  67. left: '-' + index + '00%;transition: all 0s;'
  68. });
  69. } else {
  70. // 如果没有找到,可以选择处理的逻辑,比如重置 index
  71. this.setData({
  72. index: 0,
  73. imgindex: 1,
  74. left: '0%;transition: all .5s;'
  75. });
  76. }
  77. }
  78. },
  79. touchStart: function (e) {
  80. this.data.touchStartTime = e.timeStamp; // 时间点
  81. let sx = e.touches[0].pageX;
  82. let sy = e.touches[0].pageY;
  83. this.data.touchS = [sx, sy];
  84. },
  85. touchMove: function (e) {
  86. let start = this.data.touchS;
  87. let sx = e.touches[0].pageX;
  88. let sy = e.touches[0].pageY;
  89. this.data.touchE = [sx, sy];
  90. },
  91. touchEnd: function (e) {
  92. let start = this.data.touchS;
  93. let end = this.data.touchE;
  94. let scaleObj = this.data.scaleObj;
  95. if (scaleObj.yes) {
  96. if (end[0] === 0) {
  97. console.log('点击');
  98. this.jingzhi()
  99. } else if ((start[0] < end[0] - 50) && (scaleObj.scale === 1 && scaleObj.x === 0 && scaleObj.y === 0)) {
  100. if (this.data.index > 0) {
  101. this.data.index -= 1;
  102. this.data.imgindex -= 1;
  103. this.updateView();
  104. }
  105. } else if ((start[0] > end[0] + 50) && (scaleObj.scale === 1 && scaleObj.x === 0 && scaleObj.y === 0)) {
  106. if (this.data.index < this.data.previewImgList.length - 1) {
  107. this.data.index += 1;
  108. this.data.imgindex += 1;
  109. this.updateView();
  110. }
  111. } else {
  112. console.log('下滑/上滑');
  113. // this.jingzhi()
  114. }
  115. this.data.touchE = [0, 0];
  116. }
  117. setTimeout(() => {
  118. if (this.data.scaleObj.x === 0 && this.data.scaleObj.y === 0 && this.data.scaleObj.scale === 1) {
  119. this.data.scaleObj.yes = true;
  120. }
  121. }, 500);
  122. },
  123. updateView: function () {
  124. this.setData({
  125. index: this.data.index,
  126. left: '-' + this.data.index + '00%;transition: all .5s;',
  127. imgindex: this.data.imgindex
  128. });
  129. },
  130. jingzhi: function () {
  131. // 处理点击事件
  132. console.log('点击图片');
  133. this.setData({
  134. preview_box_top: '0%',
  135. previewHideStatus: false ,// 点击后重新显示预览框
  136. previewImgList:[],
  137. previewImg:''
  138. });
  139. }
  140. }
  141. });