pull-zoom-image.uvue 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
<template>
<view @click="back" class="nav-back">
  <image class="back-img" src="/static/template/scroll-fold-nav/back.png" mode="widthFix"></image>
</view>
<view style="flex:1" @touchstart="touchstart($event as TouchEvent)" @touchmove="touchmove($event as TouchEvent)" @touchend="touchend" @touchcancel="touchend">
  <image class="head-img" ref="head-img" src="../../../static/template/pull-zoom-image/head-img.jpg" mode="scaleToFill"></image>
  <view class="body" ref="body">
    <view class="user-info">
      <image class="user-avatar" src="../../../static/test-image/logo.png" mode="widthFix"></image>
      <view class="font-box">
        <text class="username">uni-app-x</text>
        <text class="slogan">一次开发,多端覆盖</text>
      </view>
    </view>
    <view class="content">
      <view class="content-item">
        <text class="text">
          1. 当前示例监听了整个页面的touchstart、touchmove、touchend事件 ,实现监听拖动事件。
        </text>
      </view>
      <view class="content-item">
        <text class="text">
          2. 在拖动事件,使用 ref 直接获取 ref="body" 元素的节点,通过节点的 setProperty 方法来修改 transform的translateY 的值(根据在Y轴方向拖动的距离),从而达到页面跟随手指上下拖动的效果。
        </text>
      </view>
      <view class="content-item">
        <text class="text">
          3. 在拖动事件,使用 ref 直接获取 ref="head-img" 元素的节点,通过节点的 setProperty 方法来修改 transform的scale 的值(根据在Y轴方向拖动的距离),从而达到手指上下拖动时图片缩放的效果。
        </text>
      </view>
      <view class="content-item">
        <text class="text">
          4. 在拖动完成事件,通过帧动画,设置上述两个元素回到原来的位置。
        </text>
      </view>
      <view class="content-item">
        <text class="text">
          5. 请向上\向下拖动页面观察效果。
        </text>
      </view>
      <view class="content-item" v-for="(item,index) in 10" :key="index">
        <text class="text">content-{{item}}</text>
      </view>
    </view>
  </view>
</view>
47 48 49
</template>

<script>
50 51 52
  let sY : number = 0,
      dY : number = 0,
      maxScrollTop:number = 0;
53 54 55 56
  export default {
    data() {
      return {
        $nodeMap: new Map<string, INode>(),
57 58
        y: 0 as number,
        scrollTop:0 as number
59
      }
60 61 62 63 64 65 66
    },
    onReady() {
      uni.getSystemInfo({
        success: (e) => {
          maxScrollTop = 1425 - e.screenHeight
        }
      })
67
    },
68 69 70 71 72 73 74 75
    methods: {
      doMove() {
        // console.log('doMove',this.y);
        if(this.y > 0){
          this.setINodeStyle("head-img", 'transform', `scale(${(this.y > 0 ? this.y : 0) / 200 + 1})`)
        }else{
          this.setINodeStyle("head-img", 'transform', `translateY(${this.y})`)
        }
76 77 78 79 80 81
        this.setINodeStyle("body", 'transform', `translateY(${this.y})`)
      },
      touchstart(e : TouchEvent) {
        sY = e.touches[0].screenY;
      },
      touchmove(e : TouchEvent) {
82 83 84 85
        this.y += e.touches[0].screenY - sY;
        dY = e.touches[0].screenY - sY
        sY = e.touches[0].screenY;
        this.doMove()
86 87 88
      },
      touchend() {
        // console.log('touchend')
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        function moveTo(y : number, callback : () => void, speed : number = 10) {
          let interval : number = 0
          let acceleration : number = 1
          interval = setInterval(() => {
            // 加速度
            acceleration += 0.2

            const dy = y - this.y
            if (Math.abs(dy) < 1) {
              this.y = y
            } else {
              this.y += dy / speed * acceleration
            }

            // 滚动边界限制
            if(this.y < maxScrollTop*-1){
              this.y = maxScrollTop*-1
            }
            if(this.y > 0){
              this.y = 0
            }

            // console.log('this.y',this.y);

            this.doMove()
            if (this.y == y || this.y == maxScrollTop*-1 || this.y == 0) {
              clearInterval(interval)
              callback()
            }
          }, 16)
        }
        // console.log('dY',dY);
        if(this.y < 0){
          // console.log(dY);
          moveTo(this.y + dY * 20, () => {
            // console.log('done');
          },5)
        }else{
          const bounceY = (this.y * -0.03).toInt()
          // console.log('bounceY', bounceY);
          moveTo(bounceY, () => {
            moveTo(0, () => {
              // console.log('done');
            }, 30)
          })
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        }
      },
      // 工具方法,用于快速设置 INode 的 style
      setINodeStyle(refName : string, propertyName : string, propertyStyle : any) : void {
        let node : INode | null = this.$nodeMap.get(refName)
        if (node == null) {
          node = this.$refs.get(refName) as INode;
          this.$nodeMap.set(refName, node)
        } else {
          // console.log('直接拿');
        }
        node?.style?.setProperty(propertyName, propertyStyle);
      },
      back() {
        uni.navigateBack({
          success(result) {
            console.log('navigateBack success', result.errMsg)
          },
          fail(error) {
            console.log('navigateBack fail', error.errMsg)
          },
          complete(result) {
            console.log('navigateBack complete', result.errMsg)
          }
        })
      }
    }
  }
</script>

<style>
  .head-img {
    height: 350px;
167
    /* width: 1000rpx; */
168 169 170 171 172 173
    margin-left: -125rpx;
    margin-top: -50px;
    background-color: #aab8d9;
  }

  .body {
174 175
    margin-top: -200px;
    width: 750rpx;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  }

  .user-info {
    padding: 15px;
    flex-direction: row;
  }

  .user-avatar {
    width: 150rpx;
    height: 150rpx;
    border-radius: 100px;
    border: 3px solid #FFF;
  }

  .font-box {
    flex-direction: column;
    justify-content: space-around;
    padding: 10px;
  }

  .username {
    font-size: 26px;
    color: #FFF;
  }

  .slogan {
    font-size: 16px;
    color: #FFF;
  }

  .content {
    background-color: #FFF;
    border-radius: 10px;
209
    padding: 15px;
210 211 212
  }

  .content-item {
213 214
    padding: 15px;
    margin-bottom: 16px;
215 216
    background-color: #fff;
    border-radius: 5px;
217
    border: 1px solid rgba(220, 220, 220, 0.5);
218 219 220
  }

  .text {
221
    font-size: 14px;
222 223 224 225 226 227
    color: #666;
    line-height: 20px;
  }

  .nav-back {
    position: absolute;
228 229
    top: 30px;
    left: 10px;
230 231 232 233 234 235 236 237 238 239 240 241 242 243
    background-color: rgba(220, 220, 220, 0.3);
    border-radius: 100px;
    width: 28px;
    height: 28px;
    justify-content: center;
    align-items: center;
    z-index: 10;
  }

  .nav-back .back-img {
    width: 18px;
    height: 18px;
  }
</style>