drop-card.uvue 7.1 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2 3 4 5 6 7 8 9 10 11
  <view class="root">
    <template v-for="(item,index) in cardList" :key="index">
      <view class="card" ref="card" @touchstart="touchstart($event as TouchEvent,index)" @touchmove="touchmove($event as TouchEvent,index)" @touchend="touchend" @touchcancel="touchend">
        <image class="card-img" ref="card-img" :src="item"></image>
        <template v-if="index == 2">
          <view class="state">
            <image class="state-icon like" ref="state-icon-like" src="/static/template/drop-card/like.png" mode="widthFix"></image>
            <image class="state-icon dislike" ref="state-icon-dislike" src="/static/template/drop-card/dislike.png" mode="widthFix"></image>
          </view>
        </template>
DCloud_JSON's avatar
DCloud_JSON 已提交
12
      </view>
13 14
    </template>
    
DCloud_JSON's avatar
DCloud_JSON 已提交
15 16 17 18
  </view>
</template>
<script lang="ts">
  let sX : number = 0,
19 20
    sY : number = 0,
    screenWidth : number = 1,
21
    nodesMap = new Map<string, INode[]>()
DCloud_JSON's avatar
DCloud_JSON 已提交
22 23 24 25
  export default {
    data() {
      return {
        x: 0,
26 27 28 29 30 31 32
        y: 0,
        cardList: [
          '/static/template/drop-card/1.jpg',
          '/static/template/drop-card/2.jpg',
          '/static/template/drop-card/3.jpg'
        ] as string[],
        NodesMap: new Map<string, INode[]>(),
DCloud_JSON's avatar
DCloud_JSON 已提交
33 34
      }
    },
35
    onReady() {
DCloud_JSON's avatar
DCloud_JSON 已提交
36 37
      uni.getSystemInfo({
        success: (e) => {
38 39 40 41
          // console.log('e',e);
          screenWidth = e.screenWidth;
          let height = e.screenHeight - 200 + 'px'
          for (var i = 0; i < 3; i++) {
DCloud_JSON's avatar
DCloud_JSON 已提交
42
            this.setStyle('card',i,'margin-top', 100 - 40*i+'px');
43 44
            this.setStyle('card',i,'height', height);
            this.setStyle('card-img',i,'height', height);
45 46 47 48 49 50 51 52 53 54 55
            
            this.setStyle('card',i,'transform', 'scale('+(0.8+0.1*i)+')')
            this.setStyle('card',i,'transitionTimingFunction','ease-in-out');
            this.setStyle('card',i,'transitionProperty','transform');
          }
          
          setTimeout(()=>{
            let time = 600;
            this.setStyle('card',0,'transitionDuration',time.toFixed(0));
            this.setStyle('card',1,'transitionDuration',time.toFixed(0));
          },0)
DCloud_JSON's avatar
DCloud_JSON 已提交
56
        }
57 58
      })
    },
59 60 61 62 63 64 65 66
    // watch: {
    //   x() {
    //     this.afterMove()
    //   },
    //   y() {
    //     this.afterMove()
    //   }
    // },
67 68 69 70 71 72 73 74 75 76
    computed: {
      movePercent() : number {
        return Math.abs(this.x) / (screenWidth/2*3)
      },
      likeOpacity() : number {
        return this.x < 0 ? 0 : this.movePercent * 100
      },
      dislikeOpacity() : number {
        return this.x > 0 ? 0 : this.movePercent * 100
      }
DCloud_JSON's avatar
DCloud_JSON 已提交
77
    },
78 79 80 81 82 83 84 85 86 87
    methods: {
      setStyle(refName:string,index:number,propertyName:string,propertyStyle:any):void{
        let nodes:INode[]|null = nodesMap.get(refName)
        if(nodes == null){
          nodes = this.$refs.get(refName) as INode[]
          nodesMap.set(refName,nodes)
        }else{
          // console.log('直接拿');
        }
        (nodes)[index].style.setProperty(propertyName,propertyStyle);
DCloud_JSON's avatar
DCloud_JSON 已提交
88
      },
89 90 91 92 93 94
      afterMove() {
        // console.log('afterMove');
        this.setStyle('card',0,'transform', 'scale('+(this.movePercent/20+0.8)+')')
        this.setStyle('card',1,'transform', 'scale('+(this.movePercent/20+0.9)+')')
        
        for (var i = 0; i < 3; i++) {
DCloud_JSON's avatar
DCloud_JSON 已提交
95
          this.setStyle('card',i,'margin-top', 100 - 40*i - this.x/screenWidth * 10 +'px');
96
        }
97
        
98
        this.setStyle('card',2,'transform', 'translateX('+this.x+'px) translateY('+this.y+'px) rotate('+this.x/-30+'deg) scale(1)')
99 100 101 102 103 104 105
        this.setStyle('state-icon-like',0,'opacity', x < 0 ? 0 : movePercent * 10)
        this.setStyle('state-icon-dislike',0,'opacity', x > 0 ? 0 : movePercent * 10)
      },
      touchstart(e : TouchEvent,index:number) {
        if(index != 2){
          return 
        }
DCloud_JSON's avatar
DCloud_JSON 已提交
106
        sX = e.touches[0].screenX;
107 108 109
        sY = e.touches[0].screenY;
        this.x = 0
        this.y = 0
DCloud_JSON's avatar
DCloud_JSON 已提交
110
      },
111 112 113 114
      touchmove(e : TouchEvent,index:number) {
        if(index != 2){
          return 
        }
DCloud_JSON's avatar
DCloud_JSON 已提交
115 116 117
        this.x += e.touches[0].screenX - sX;
        this.y += e.touches[0].screenY - sY;
        sX = e.touches[0].screenX;
118 119
        sY = e.touches[0].screenY;
        this.afterMove()
DCloud_JSON's avatar
DCloud_JSON 已提交
120
      },
121 122
      touchend() {
        // console.log('touchend');
123

124 125 126 127 128 129 130 131
        // 设置释放之后飘走的方向 0回到坐标中心 1向右 2向左
        let k:number = 0;
        if (this.x > screenWidth / 6 ) {
          k = 1
        }else if(this.x < screenWidth * -1 / 6){
          k = -1
        }
        
132 133
        // 设置动画时间
        let time = 300;
134
        
135 136 137 138 139 140 141 142 143 144 145
        /**
         * @description 卡片2 transform 的 transition
         */
        const card2TT = {
          open(){
            this.setStyle('card',2,'transitionProperty','transform');
            this.setStyle('card',2,'transitionDuration',time.toFixed(0));
          },
          close(){
            this.setStyle('card',2,'transitionProperty','');
            this.setStyle('card',2,'transitionDuration',0);
146
          }
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        }
        
        card2TT.open()
          
        /**
         * @description 卡片2回到原点
         */
        const card2To0 = ()=> {
          // 设置横纵坐标归0
          this.x = 0
          this.y = 0
          //执行移动后的相应效果
          this.afterMove()
          
          setTimeout(()=>{
            //动画结果关闭
            card2TT.close()
          },time)
        }
        
        if(k == 0){
          card2To0()
        }else{
          // 卡片飘出界面
          this.setStyle('card',2,'transform', `translate(${screenWidth * 1.5 * k},${Math.abs(screenWidth * 0.5 * k)})`)
          
          setTimeout(()=>{
            //动画结果关闭
           card2TT.close()
            
            // 更改置顶卡片内容
            let newArr = this.cardList.slice();
            this.cardList[2] = newArr[1];
            
            setTimeout(()=>{
              // 将置顶卡片归c位
              // this.setStyle('card',2,'margin-top', '50px');
              this.setStyle('card',2,'transform', 'scale(0.95)')
              
              // 更改非置顶 卡片内容
              this.cardList[0] = newArr[2]
              this.cardList[1] = newArr[0]
              
              // 开启动画
              card2TT.open()
              card2To0()
            },100)
            
          },time)
        }
        
DCloud_JSON's avatar
DCloud_JSON 已提交
198 199 200 201 202 203 204 205 206 207 208
      }
    }
  }
</script>
<style>
  .root {
    flex: 1;
    position: relative;
  }

  .card {
209
    width: 700rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
210 211 212
    height: 750rpx;
    position: absolute;
    margin: 0 25rpx;
213
    margin-top: 50px;
DCloud_JSON's avatar
DCloud_JSON 已提交
214
    border-radius: 10px;
215 216 217
    color: #FFF;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    z-index: 100;
DCloud_JSON's avatar
DCloud_JSON 已提交
218 219
  }
  
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  .card-img {
    border-radius: 10px;
  }

  .state {
    top: 20rpx;
    left: 20rpx;
    width: 650rpx;
    padding: 4px;
    position: absolute;
    flex-direction: row;
    justify-content: space-between;
  }

  .state-icon {
    width: 30px;
    height: 30px;
    border: 1px solid #FFF;
    background-color: #FFF;
    padding: 3px;
    border-radius: 100px;
    box-shadow: 0 0 1px #EBEBEB;
    opacity: 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
243 244
  }
</style>