dgex-tantan.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 :animation="animationData" class="tantan-slide-img-box" :style="{height: winHeigh + 'px'}">
  22. <view >
  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 class="img-num-list" >
  30. <view class="num-item" :class="{'num-item-default':imgIndex===imgKey}" v-for="(img,imgIndex) in item.images"></view>
  31. </view>
  32. <view class="slide-data">
  33. <!-- 用户个性消息-->
  34. <view class="on-line-box">
  35. <image class="on-line-img" mode="aspectFill" src="/static/img/index/on-line.png" ></image>
  36. <view class="on-line-text sys-color-black-0 sys-weight-400">当前在线</view>
  37. </view>
  38. <view class="user-data">
  39. <text class="user-item sys-color-white sys-weight-600">Maple</text>
  40. <text class="user-item sys-color-white sys-weight-600">,</text>
  41. <text class="user-item sys-color-white sys-weight-600">19</text>
  42. </view>
  43. <view class="user-city">
  44. <text class="city-item sys-color-yellow sys-weight-600">17.16</text>
  45. <text class="city-item sys-color-white sys-weight-400">km</text>
  46. <text class="city-item sys-color-white sys-weight-600">重庆市九龙坡</text>
  47. </view>
  48. <view class="open-wechat">
  49. <image class="open-wechat-img" mode="aspectFill" src="/static/img/index/wechat.png" ></image>
  50. <view class="open-wechat-text sys-color-white sys-weight-400">yxk********</view>
  51. <image class="lock-wechat-img" mode="aspectFill" src="/static/img/index/lock.png" ></image>
  52. </view>
  53. </view>
  54. </view>
  55. <view v-if="index === currentIndex">
  56. <view class="tantan-slide-box-icon tantan-slide-box-dislike"
  57. :style="{
  58. opacity: dislike * 1.5,
  59. transform: 'scale('+ (dislike + 1 > 2 ? 2 : dislike + 1 ) +')',
  60. }">
  61. <image style="width: 30rpx;height: 30rpx;" src="/static/dgex-tantan/close.png"></image>
  62. </view>
  63. <view class="tantan-slide-box-icon tantan-slide-box-love" :style="{
  64. opacity: love * 1.5,
  65. transform: 'scale('+ (love + 1 > 2 ? 2 : love + 1 ) +')',
  66. }">
  67. <image style="width: 30rpx;height: 30rpx;" src="/static/dgex-tantan/like.png"></image>
  68. </view>
  69. </view>
  70. </template>
  71. </view>
  72. </template>
  73. </view>
  74. </view>
  75. </template>
  76. <script>
  77. import tools from "@/service/tools";
  78. export default {
  79. name: "slide",
  80. props: {
  81. list: {
  82. type: Array,
  83. default: () => []
  84. }
  85. },
  86. data() {
  87. return {
  88. winWidth: 0,
  89. winHeigh: 0,
  90. /*记录x y轴*/
  91. x: {
  92. start: 0,
  93. move: 0,
  94. end: 0
  95. },
  96. y: {
  97. start: 0,
  98. move: 0,
  99. end: 0
  100. },
  101. visible: 3,
  102. /*下标*/
  103. currentIndex: 0,
  104. /*滑动*/
  105. swipering: false,
  106. /*滑动中*/
  107. slideing: false,
  108. love: 0,
  109. dislike: 0,
  110. imgKey:0,
  111. overturnType:0,
  112. animationData:{}
  113. }
  114. },
  115. watch:{
  116. 'overturnType':function (){
  117. console.log('this.overturnType:'+this.overturnType)
  118. if(this.overturnType===1){
  119. tools.vibrate()
  120. }
  121. }
  122. },
  123. mounted() {
  124. const res = uni.getSystemInfoSync()
  125. console.log(res)
  126. this.winWidth = res.windowWidth
  127. this.winHeigh = res.windowHeight-46
  128. },
  129. methods: {
  130. setImgKey(item, type){
  131. if( this.overturnType<=0){
  132. let imgNum=item.images.length-1
  133. if(type===0){
  134. if(this.imgKey<=0){
  135. this.setOverturnTwo()
  136. }else {
  137. --this.imgKey
  138. this.setOverturnImg(item)
  139. }
  140. }else {
  141. if(this.imgKey>=imgNum){
  142. this.setOverturnTwo()
  143. }else {
  144. ++this.imgKey
  145. this.setOverturnImg(item)
  146. }
  147. }
  148. }
  149. },
  150. setOverturnImg(item){
  151. let animation = uni.createAnimation({
  152. duration: 100,
  153. timingFunction: 'ease',
  154. })
  155. animation.rotateY(3).scale(1.01).step()
  156. item.image=item.images[this.imgKey]
  157. tools.vibrate()
  158. this.animationData = animation.export()
  159. setTimeout(function() {
  160. animation.rotateY(0).scale(1).step()
  161. this.animationData = animation.export()
  162. }.bind(this), 100)
  163. },
  164. setOverturnTwo(){
  165. // tools.error('two')
  166. // return;
  167. let animation = uni.createAnimation({
  168. duration: 100,
  169. timingFunction: 'ease',
  170. })
  171. animation.rotateY(5).step()
  172. tools.vibrate()
  173. let overturnNum=1
  174. let overturnServe=setInterval(()=>{
  175. ++overturnNum
  176. // console.log('overturnNum:'+overturnNum)
  177. if(overturnNum>4){
  178. clearInterval(overturnServe)
  179. }else {
  180. let overturnType=(overturnNum%2===1)?1:2
  181. if(overturnType===1){
  182. animation.rotateY(-5).step()
  183. tools.vibrate()
  184. }else {
  185. animation.rotateY(0).step()
  186. }
  187. this.animationData = animation.export()
  188. }
  189. },80)
  190. },
  191. cardTransform(item, index) {
  192. let css = {};
  193. if (index === this.currentIndex) {
  194. if (this.slideing) {
  195. css["transitionDuration"] = `${!this.swipering ? 1000 : 0}ms`;
  196. } else {
  197. css["transitionDuration"] = `${!this.swipering ? 300 : 0}ms`;
  198. }
  199. }
  200. return css
  201. },
  202. touchStart(e, index) {
  203. if (this.slideing) return;
  204. if (typeof this.list[index].x === 'undefined' && typeof this.list[index].y === 'undefined') {
  205. this.$set(this.list[index], 'y', 0)
  206. this.$set(this.list[index], 'x', 0)
  207. }
  208. this.swipering = true;
  209. this.x.start = e.touches[0].pageX;
  210. this.y.start = e.touches[0].pageY;
  211. },
  212. touchMove(e, index) {
  213. if (this.slideing) return
  214. // 滑动状态/最后一个就不滑动
  215. if (this.list.length == index + 1) {
  216. return;
  217. }
  218. this.x.move = e.touches[0].pageX;
  219. this.y.move = e.touches[0].pageY;
  220. this.list[index].x = this.x.move - this.x.start
  221. this.list[index].y = this.y.move - this.y.start
  222. if (Number.parseInt(this.list[index].x) > 0) {
  223. this.love = Number.parseInt(this.list[index].x) / (100 * 2)
  224. } else {
  225. this.dislike = Math.abs(Number.parseInt(this.list[index].x) / (100 * 2))
  226. }
  227. },
  228. touchEnd(index) {
  229. if (this.slideing) return
  230. this.swipering = false;
  231. if (this.list.length == index + 1) {
  232. return;
  233. }
  234. if (
  235. this.list[index].x > 0 &&
  236. this.list[index].x > this.winWidth / 2 - this.winWidth / 5
  237. ) {
  238. this.touchEndNext(index);
  239. } else if (
  240. this.list[index].x < 0 &&
  241. this.list[index].x < -this.winWidth / 2 + this.winWidth / 5
  242. ) {
  243. this.touchEndNext(index);
  244. } else {
  245. this.list[index].x = 0;
  246. this.list[index].y = 0;
  247. this.slideing = false;
  248. this.love = 0;
  249. this.dislike = 0;
  250. }
  251. },
  252. touchEndNext(index) {
  253. this.slideing = true;
  254. this.list[index].x = this.list[index].x * 5;
  255. this.list[index].y = this.list[index].y * 5;
  256. this.touchEndDone()
  257. },
  258. touchEndDone() {
  259. return new Promise((resolve) => {
  260. setTimeout(() => {
  261. this.slideing = false
  262. this.$emit('onChange', {
  263. currentIndex: this.currentIndex,
  264. currentItem: this.list[this.currentIndex],
  265. type: this.love !== 0 ? 'love' : 'dislike'
  266. })
  267. this.currentIndex++
  268. this.x.move = 0
  269. this.y.move = 0
  270. this.slideing = false
  271. this.btnClickType = false
  272. this.love = 0
  273. this.dislike = 0
  274. resolve()
  275. }, 300);
  276. })
  277. },
  278. footerBtnClick(type) {
  279. if (this.btnClickType) {
  280. return
  281. }
  282. this.btnClickType = true
  283. let w = 0
  284. if (type === 'love') {
  285. w = this.winWidth * 1.5
  286. this.love = 1
  287. } else if (type === 'dislike') {
  288. w = -this.winWidth * 1.5
  289. this.dislike = 1
  290. }
  291. this.$set(this.list[this.currentIndex], 'x', w)
  292. this.touchEndDone()
  293. },
  294. clickImage() {
  295. this.$emit('onClickImage', {
  296. type: 'click',
  297. currentIndex: this.currentIndex,
  298. currentItem: this.list[this.currentIndex],
  299. })
  300. }
  301. }
  302. };
  303. </script>
  304. <style lang="scss">
  305. .tantan-slide {
  306. width: 100%;
  307. height: 100%;
  308. display: flex;
  309. justify-content: center;
  310. align-items: center;
  311. overflow: hidden;
  312. }
  313. .tantan-slide-box {
  314. position: relative;
  315. width: calc(100vw - 32rpx);
  316. height: 100%;
  317. perspective: 2100rpx;
  318. perspective-origin: 50% -30%;
  319. transform-style: preserve-3d;
  320. margin: auto;
  321. }
  322. .tantan-slide-box-item {
  323. transform-style: preserve-3d;
  324. display: flex;
  325. width: 100%;
  326. height: 100%;
  327. border-radius: 24rpx;
  328. position: absolute;
  329. opacity: 0;
  330. transform: translate3d(0px, 0px, 0px) rotate(0deg);
  331. transition: 300ms;
  332. color: #fff;
  333. }
  334. .tantan-slide-box-icon {
  335. position: absolute;
  336. width: 70rpx;
  337. height: 70rpx;
  338. top: 45rpx;
  339. border-radius: 100%;
  340. background-color: #fff;
  341. z-index: 1;
  342. opacity: 0;
  343. transition: 100ms;
  344. display: flex;
  345. align-items: center;
  346. justify-content: center;
  347. }
  348. .tantan-slide-box-love {
  349. left: 50rpx;
  350. }
  351. .tantan-slide-box-dislike {
  352. right: 50rpx;
  353. }
  354. .tantan-slide-img-box {
  355. position: relative;
  356. will-change: transform;
  357. width: 100%;
  358. height: 100%;
  359. border-radius: 40rpx;
  360. background-repeat: no-repeat;//不平铺
  361. background-position: center center;//居中
  362. background-size: cover;//随容器大小
  363. transform-style: preserve-3d; /* 为子元素开启 三维立体环境 */
  364. .tantan-slide-img{
  365. z-index: 10;
  366. border-radius: 40rpx;
  367. width: 100%;
  368. height: 100%;
  369. position: absolute;
  370. left: 0;
  371. top: 0;
  372. }
  373. .slide-img-click{
  374. z-index: 100;
  375. width: 100%;
  376. height: 100%;
  377. display: flex;
  378. justify-content: space-between;
  379. .slide-img-click-item{
  380. z-index: 100;
  381. width: 50%;
  382. height: 100%;
  383. }
  384. }
  385. .img-num-list{
  386. z-index: 101;
  387. position: absolute;
  388. width: calc(100vw - 32rpx);
  389. top: 32rpx;
  390. left: 0;
  391. display: flex;
  392. justify-content: center;
  393. .num-item{
  394. border-radius: 50%;
  395. width: 8rpx;
  396. height: 8rpx;
  397. background: rgba(255,255,255,0.4);
  398. margin-right: 6rpx;
  399. transition: .5s ease;
  400. }
  401. .num-item-default{
  402. width: 48rpx;
  403. border-radius: 20rpx;
  404. background: #FFFFFF;
  405. }
  406. }
  407. .slide-data{
  408. position: absolute;
  409. left: 28rpx;
  410. bottom: 32rpx;
  411. z-index: 101;
  412. .on-line-box{
  413. max-width: 160rpx;
  414. border-radius: 12rpx;
  415. background: #FFFFFF;
  416. display: flex;
  417. justify-content: center;
  418. padding: 4rpx 18rpx;
  419. margin-bottom: 24rpx;
  420. .on-line-img{
  421. width: 24rpx;
  422. height: 24rpx;
  423. margin-top: 10rpx;
  424. margin-right: 8rpx;
  425. }
  426. .on-line-text{
  427. height: 44rpx;
  428. line-height: 44rpx;
  429. font-size: 24rpx;
  430. }
  431. }
  432. .user-data{
  433. .user-item{
  434. font-size: 48rpx;
  435. }
  436. }
  437. .user-city{
  438. margin-top: 16rpx;
  439. height:44rpx;
  440. display:table-cell;
  441. vertical-align:bottom;
  442. .city-item{
  443. margin-right: 14rpx;
  444. font-size: 24rpx;
  445. }
  446. .city-item:first-child{
  447. font-size: 36rpx;
  448. }
  449. }
  450. .open-wechat{
  451. margin-top: 32rpx;
  452. display: flex;
  453. justify-content: space-between;
  454. padding: 8rpx;
  455. background: rgba(0,0,0,0.5);
  456. border-radius: 200rpx;
  457. .open-wechat-img{
  458. width: 64rpx;
  459. height: 64rpx;
  460. }
  461. .open-wechat-text{
  462. height: 64rpx;
  463. line-height: 64rpx;
  464. margin-left: 18rpx;
  465. margin-right: 40rpx;
  466. font-size: 28rpx;
  467. }
  468. .lock-wechat-img{
  469. width: 76rpx;
  470. height: 64rpx;
  471. }
  472. }
  473. }
  474. }
  475. </style>