dgex-tantan.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <view class="tantan-slide" :style="{
  3. width: winWidth + 'px',
  4. height: winHeigh + 'px',
  5. }">
  6. <view @touchstart.capture="touchStart($event,currentIndex)"
  7. @touchmove.stop.capture="touchMove($event,currentIndex)"
  8. @touchend.capture="touchEnd(currentIndex)" class="tantan-slide-box">
  9. <template v-for="(item, index) in list">
  10. <view class="tantan-slide-box-item"
  11. :key="index"
  12. v-if="currentIndex + visible >= index"
  13. @click.stop="clickImage"
  14. :style="[cardTransform(item, index), {
  15. 'zIndex': list.length - index,
  16. 'opacity': currentIndex + visible - 1 >= index && currentIndex <= index ? 1 : 0,
  17. 'transform': 'rotate(' + ((item.x || 0) / 30 ) + 'deg) translate3d(' + (item.x || 0) + 'px,' + (item.y || 0) + 'px, '+ 0 +'px)'
  18. }]">
  19. <!-- 加载图片会闪屏 双if避免 -->
  20. <template v-if="currentIndex + visible >= index && currentIndex <= index">
  21. <view class="tantan-slide-img-box" :style="{height: winHeigh + 'px'}">
  22. <view :animation="animationData">
  23. <image class="tantan-slide-img" v-show="imgIndex===imgKey" v-for="(img,imgIndex) in item.images" :style="{height: winHeigh + 'px'}" mode="aspectFill" :src="img" ></image>
  24. </view>
  25. <view class="slide-img-click">
  26. <view class="slide-img-click-item" @click.stop="setImgKey(item,0)"></view>
  27. <view class="slide-img-click-item" @click.stop="setImgKey(item,1)"></view>
  28. </view>
  29. </view>
  30. <view v-if="index === currentIndex">
  31. <view class="tantan-slide-box-icon tantan-slide-box-dislike"
  32. :style="{
  33. opacity: dislike * 1.5,
  34. transform: 'scale('+ (dislike + 1 > 2 ? 2 : dislike + 1 ) +')',
  35. }">
  36. <image style="width: 30rpx;height: 30rpx;" src="/static/dgex-tantan/close.png"></image>
  37. </view>
  38. <view class="tantan-slide-box-icon tantan-slide-box-love" :style="{
  39. opacity: love * 1.5,
  40. transform: 'scale('+ (love + 1 > 2 ? 2 : love + 1 ) +')',
  41. }">
  42. <image style="width: 30rpx;height: 30rpx;" src="/static/dgex-tantan/like.png"></image>
  43. </view>
  44. </view>
  45. </template>
  46. </view>
  47. </template>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. import tools from "@/service/tools";
  53. export default {
  54. name: "slide",
  55. props: {
  56. list: {
  57. type: Array,
  58. default: () => []
  59. }
  60. },
  61. data() {
  62. return {
  63. winWidth: 0,
  64. winHeigh: 0,
  65. /*记录x y轴*/
  66. x: {
  67. start: 0,
  68. move: 0,
  69. end: 0
  70. },
  71. y: {
  72. start: 0,
  73. move: 0,
  74. end: 0
  75. },
  76. visible: 3,
  77. /*下标*/
  78. currentIndex: 0,
  79. /*滑动*/
  80. swipering: false,
  81. /*滑动中*/
  82. slideing: false,
  83. love: 0,
  84. dislike: 0,
  85. imgKey:0,
  86. overturnType:0,
  87. animationData:{}
  88. }
  89. },
  90. watch:{
  91. 'overturnType':function (){
  92. console.log('this.overturnType:'+this.overturnType)
  93. if(this.overturnType===1){
  94. tools.vibrate()
  95. }
  96. }
  97. },
  98. mounted() {
  99. const res = uni.getSystemInfoSync()
  100. console.log(res)
  101. this.winWidth = res.windowWidth
  102. this.winHeigh = res.windowHeight-46
  103. },
  104. methods: {
  105. setImgKey(item, type){
  106. if( this.overturnType<=0){
  107. let imgNum=item.images.length-1
  108. if(type===0){
  109. if(this.imgKey<=0){
  110. this.setOverturnTwo()
  111. }else {
  112. --this.imgKey
  113. this.setOverturnImg(item)
  114. }
  115. }else {
  116. if(this.imgKey>=imgNum){
  117. this.setOverturnTwo()
  118. }else {
  119. ++this.imgKey
  120. this.setOverturnImg(item)
  121. }
  122. }
  123. }
  124. },
  125. setOverturnImg(item){
  126. // tools.success('one')
  127. // return;
  128. let animation = uni.createAnimation({
  129. duration: 100,
  130. timingFunction: 'ease',
  131. })
  132. animation.rotateY(3).scale(1.01).step()
  133. item.image=item.images[this.imgKey]
  134. tools.vibrate()
  135. this.animationData = animation.export()
  136. setTimeout(function() {
  137. animation.rotateY(0).scale(1).step()
  138. this.animationData = animation.export()
  139. }.bind(this), 100)
  140. },
  141. setOverturnTwo(){
  142. // tools.error('two')
  143. // return;
  144. let animation = uni.createAnimation({
  145. duration: 100,
  146. timingFunction: 'ease',
  147. })
  148. animation.rotateY(5).step()
  149. tools.vibrate()
  150. let overturnNum=1
  151. let overturnServe=setInterval(()=>{
  152. ++overturnNum
  153. if(overturnNum>4){
  154. clearInterval(overturnServe)
  155. }else {
  156. let overturnType=(overturnNum%2===1)?1:2
  157. if(overturnType===1){
  158. animation.rotateY(3).step()
  159. tools.vibrate()
  160. }else {
  161. animation.rotateY(0).step()
  162. }
  163. this.animationData = animation.export()
  164. }
  165. },80)
  166. },
  167. cardTransform(item, index) {
  168. let css = {};
  169. if (index === this.currentIndex) {
  170. if (this.slideing) {
  171. css["transitionDuration"] = `${!this.swipering ? 1000 : 0}ms`;
  172. } else {
  173. css["transitionDuration"] = `${!this.swipering ? 300 : 0}ms`;
  174. }
  175. }
  176. return css
  177. },
  178. touchStart(e, index) {
  179. if (this.slideing) return;
  180. if (typeof this.list[index].x === 'undefined' && typeof this.list[index].y === 'undefined') {
  181. this.$set(this.list[index], 'y', 0)
  182. this.$set(this.list[index], 'x', 0)
  183. }
  184. this.swipering = true;
  185. this.x.start = e.touches[0].pageX;
  186. this.y.start = e.touches[0].pageY;
  187. },
  188. touchMove(e, index) {
  189. if (this.slideing) return
  190. // 滑动状态/最后一个就不滑动
  191. if (this.list.length == index + 1) {
  192. return;
  193. }
  194. this.x.move = e.touches[0].pageX;
  195. this.y.move = e.touches[0].pageY;
  196. this.list[index].x = this.x.move - this.x.start
  197. this.list[index].y = this.y.move - this.y.start
  198. if (Number.parseInt(this.list[index].x) > 0) {
  199. this.love = Number.parseInt(this.list[index].x) / (100 * 2)
  200. } else {
  201. this.dislike = Math.abs(Number.parseInt(this.list[index].x) / (100 * 2))
  202. }
  203. },
  204. touchEnd(index) {
  205. if (this.slideing) return
  206. this.swipering = false;
  207. if (this.list.length == index + 1) {
  208. return;
  209. }
  210. if (
  211. this.list[index].x > 0 &&
  212. this.list[index].x > this.winWidth / 2 - this.winWidth / 5
  213. ) {
  214. this.touchEndNext(index);
  215. } else if (
  216. this.list[index].x < 0 &&
  217. this.list[index].x < -this.winWidth / 2 + this.winWidth / 5
  218. ) {
  219. this.touchEndNext(index);
  220. } else {
  221. this.list[index].x = 0;
  222. this.list[index].y = 0;
  223. this.slideing = false;
  224. this.love = 0;
  225. this.dislike = 0;
  226. }
  227. },
  228. touchEndNext(index) {
  229. this.slideing = true;
  230. this.list[index].x = this.list[index].x * 5;
  231. this.list[index].y = this.list[index].y * 5;
  232. this.touchEndDone()
  233. },
  234. touchEndDone() {
  235. return new Promise((resolve) => {
  236. setTimeout(() => {
  237. this.slideing = false
  238. this.$emit('onChange', {
  239. currentIndex: this.currentIndex,
  240. currentItem: this.list[this.currentIndex],
  241. type: this.love !== 0 ? 'love' : 'dislike'
  242. })
  243. this.currentIndex++
  244. this.x.move = 0
  245. this.y.move = 0
  246. this.slideing = false
  247. this.btnClickType = false
  248. this.love = 0
  249. this.dislike = 0
  250. resolve()
  251. }, 300);
  252. })
  253. },
  254. footerBtnClick(type) {
  255. if (this.btnClickType) {
  256. return
  257. }
  258. this.btnClickType = true
  259. let w = 0
  260. if (type === 'love') {
  261. w = this.winWidth * 1.5
  262. this.love = 1
  263. } else if (type === 'dislike') {
  264. w = -this.winWidth * 1.5
  265. this.dislike = 1
  266. }
  267. this.$set(this.list[this.currentIndex], 'x', w)
  268. this.touchEndDone()
  269. },
  270. clickImage() {
  271. this.$emit('onClickImage', {
  272. type: 'click',
  273. currentIndex: this.currentIndex,
  274. currentItem: this.list[this.currentIndex],
  275. })
  276. }
  277. }
  278. };
  279. </script>
  280. <style lang="scss">
  281. .tantan-slide {
  282. width: 100%;
  283. height: 100%;
  284. display: flex;
  285. justify-content: center;
  286. align-items: center;
  287. overflow: hidden;
  288. }
  289. .tantan-slide-box {
  290. position: relative;
  291. width: calc(100vw - 32rpx);
  292. height: 100%;
  293. perspective: 2100rpx;
  294. perspective-origin: 50% -30%;
  295. transform-style: preserve-3d;
  296. margin: auto;
  297. }
  298. .tantan-slide-box-item {
  299. transform-style: preserve-3d;
  300. display: flex;
  301. width: 100%;
  302. height: 100%;
  303. border-radius: 24rpx;
  304. position: absolute;
  305. opacity: 0;
  306. transform: translate3d(0px, 0px, 0px) rotate(0deg);
  307. transition: 300ms;
  308. color: #fff;
  309. }
  310. .tantan-slide-box-icon {
  311. position: absolute;
  312. width: 70rpx;
  313. height: 70rpx;
  314. top: 45rpx;
  315. border-radius: 100%;
  316. background-color: #fff;
  317. z-index: 1;
  318. opacity: 0;
  319. transition: 100ms;
  320. display: flex;
  321. align-items: center;
  322. justify-content: center;
  323. }
  324. .tantan-slide-box-love {
  325. left: 50rpx;
  326. }
  327. .tantan-slide-box-dislike {
  328. right: 50rpx;
  329. }
  330. .tantan-slide-img-box {
  331. position: relative;
  332. will-change: transform;
  333. width: 100%;
  334. height: 100%;
  335. border-radius: 40rpx;
  336. background-repeat: no-repeat;//不平铺
  337. background-position: center center;//居中
  338. background-size: cover;//随容器大小
  339. transform-style: preserve-3d; /* 为子元素开启 三维立体环境 */
  340. .tantan-slide-img{
  341. z-index: 10;
  342. border-radius: 40rpx;
  343. width: 100%;
  344. height: 100%;
  345. position: absolute;
  346. left: 0;
  347. top: 0;
  348. }
  349. .slide-img-click{
  350. z-index: 100;
  351. width: 100%;
  352. height: 100%;
  353. display: flex;
  354. justify-content: space-between;
  355. .slide-img-click-item{
  356. z-index: 100;
  357. width: 50%;
  358. height: 100%;
  359. }
  360. }
  361. }
  362. </style>