contrastImg.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template src="./contrastImg.html">
  2. </template>
  3. <script>
  4. export default {
  5. name: "contrastImg",
  6. props: {
  7. leftSrc: {
  8. type: String,
  9. default: '',
  10. },
  11. rightSrc: {
  12. type: String,
  13. default: '',
  14. },
  15. cStyle: {
  16. type: String,
  17. default: '',
  18. },
  19. mode: {
  20. type: String,
  21. default: 'aspectFill',
  22. },
  23. type: {
  24. type: String,
  25. default: '1',
  26. },
  27. modelType: {
  28. type: String,
  29. default: '1',
  30. }
  31. },
  32. data() {
  33. return {
  34. loaded: false,
  35. loadNum: 1, //尝试加载次数
  36. thumbLoaded: false,
  37. newUrl: '',
  38. // typeCompressString:"imageMogr2/auto-orient/thumbnail/200x200/blur/1x0/quality/75",
  39. typeString: "imageMogr2/auto-orient/format/webp/blur/1x0/quality/75",
  40. showSwiper: false, //是否显示切换视图状态,默认不显示
  41. unit: 1,
  42. translateX: 0, //原图左右移动距离
  43. startX: 0,
  44. lastX: 0, //上一次的距离
  45. needAni: false, //回弹动画是否需要
  46. width: window.innerWidth,
  47. }
  48. },
  49. mounted() { //生命周期不要更换,此处逻辑必须是创建时处理
  50. this.newUrl = this.webpImage(this.rightSrc);
  51. // var str = "width: 688rpx;height: 340rpx;";
  52. let startWidth = window.innerWidth;
  53. var match = this.cStyle.match(/width:\s*(\d+px)/);
  54. if (match) {
  55. this.width = parseFloat(match[1])
  56. } else {
  57. console.log("未找到匹配的width属性值");
  58. this.width = startWidth;
  59. }
  60. // this.translateX = this.width;
  61. if (this.type == '2') { //直接进入拉伸状态
  62. this.translateX = this.width / 2;
  63. this.showSwiper = true;
  64. }
  65. },
  66. methods: {
  67. touchstart(e) {
  68. this.lastX = e.changedTouches[0].clientX;
  69. this.startX = e.changedTouches[0].clientX;
  70. // let currPage = getCurrentPages()[getCurrentPages().length - 1] ? getCurrentPages()[getCurrentPages().length - 1].$vm : null;
  71. // if(currPage && currPage.hasOwnProperty('contrastAutoPlay')){
  72. // currPage.contrastAutoPlay = false;
  73. // }
  74. },
  75. touchmove(e) {
  76. let touchMoveX = e.changedTouches[0].clientX; //滑动变化坐标
  77. let moveX = (touchMoveX - this.lastX) * this.unit; //相对于上一次偏移的距离-转为rpx单位
  78. this.translateX = Number(this.translateX) + moveX;
  79. if (this.translateX < 0) {
  80. this.translateX = 0;
  81. } else if (this.translateX > this.width) {
  82. this.translateX = this.width;
  83. }
  84. this.lastX = touchMoveX;
  85. },
  86. touchend(e) {
  87. // let touchMoveX = e.changedTouches[0].clientX; //滑动变化坐标
  88. // if(touchMoveX == this.startX){//释放时的坐标和开始时一致,表示点击行为
  89. // console.warn("***clickHandle***")
  90. // this.$emit('clickHandle');
  91. // }
  92. // let currPage = getCurrentPages()[getCurrentPages().length - 1] ? getCurrentPages()[getCurrentPages().length - 1].$vm : null;
  93. // if(currPage && currPage.hasOwnProperty('contrastAutoPlay')){
  94. // currPage.contrastAutoPlay = true;
  95. // }
  96. },
  97. catchTouchMove: function() {
  98. return false;
  99. },
  100. clear(e) {
  101. // TODO nvue 取消冒泡
  102. e.stopPropagation()
  103. },
  104. clickHandle(e) {
  105. this.$emit('clickHandle');
  106. // let leftDistance = 0; // 获取组件距离屏幕左边的距离
  107. // 获取当前点击的组件信息
  108. // const query = uni.createSelectorQuery().in(this);
  109. // query.select('.containView').boundingClientRect(data => {
  110. // if (data) {
  111. // leftDistance = data.left; // 获取组件距离屏幕左边的距离-单位px
  112. // console.log('组件距离屏幕左边的距离:', leftDistance);
  113. // console.warn("***clickHandle***",e,this.translateX,leftDistance)
  114. // let pointX = e.detail.x - leftDistance;
  115. // let position = "left";
  116. // if(pointX < (this.translateX/2)){//点击的是左边
  117. // position = "left";
  118. // }else{//点击的是右边
  119. // position = "right";
  120. // }
  121. // this.$emit('clickHandle',{position});
  122. // }
  123. // }).exec();
  124. },
  125. compareAction() {
  126. this.showSwiper = !this.showSwiper;
  127. if (this.showSwiper) { //显示
  128. this.needAni = true;
  129. this.$nextTick(() => {
  130. this.translateX = this.width / 2;
  131. })
  132. // 动画结束后,清除动画的class,确保不影响滑动效果
  133. setTimeout(() => {
  134. this.needAni = false;
  135. }, 510);
  136. } else {
  137. this.needAni = true;
  138. this.$nextTick(() => {
  139. this.translateX = 0;
  140. })
  141. // 动画结束后,清除动画的class,确保不影响滑动效果
  142. setTimeout(() => {
  143. this.needAni = false;
  144. }, 510);
  145. }
  146. this.$emit('compareAction', this.showSwiper)
  147. },
  148. // 获取无损压缩图-webp格式
  149. webpImage(url) {
  150. let result = url;
  151. if (result.indexOf('https://elab-marketing-web.oss') != -1) {
  152. this.typeString = "x-oss-process=image/auto-orient,1/quality,Q_46/format,jpg";
  153. } else if (result.indexOf('https://dm.static') != -1) {
  154. this.typeString = "imageMogr2/auto-orient/format/webp/blur/1x0/quality/75";
  155. }
  156. if (result) {
  157. if (this.typeString) {
  158. if (result.indexOf('?') > -1) {
  159. return result + "&" + this.typeString;
  160. } else {
  161. return result + "?" + this.typeString;
  162. }
  163. } else {
  164. return result
  165. }
  166. } else {
  167. return ""
  168. }
  169. },
  170. //加载失败,重复三次
  171. errorLoad(e) {
  172. this.loadNum++;
  173. if (this.loadNum > 3) {
  174. return false;
  175. }
  176. console.log(e);
  177. let random = Math.random();
  178. this.error = true;
  179. // this.src = this.src + (this.src.indexOf('?') > -1 ? '&' + random : '?' + random);
  180. console.warn("***errorLoad***", this.src);
  181. },
  182. }
  183. }
  184. </script>
  185. <style lang="scss" scoped>
  186. @import "./contrastImg.scss";
  187. </style>