pull-zoom-image.uvue 7.2 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
<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>
41 42
      <view class="content-item" v-for="(item,index) in 15" :key="index">
        <text class="text" style="padding-left: 4px;">{{item + 5}}. 占位</text>
43 44 45 46
      </view>
    </view>
  </view>
</view>
47 48
</template>

49
<script>
50 51
  let sY : number = 0,
      dY : number = 0,
52
      maxScrollTop:number = 0,
53 54
      moveIng:boolean = false,
      startMoveTime:number = 0;
55 56 57 58
  export default {
    data() {
      return {
        $nodeMap: new Map<string, INode>(),
59 60
        y: 0 as number,
        scrollTop:0 as number
61
      }
62 63 64 65
    },
    onReady() {
      uni.getSystemInfo({
        success: (e) => {
66
          maxScrollTop = 1750 - e.screenHeight
67 68
        }
      })
69
    },
70 71 72 73 74 75 76 77
    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})`)
        }
78 79 80
        this.setINodeStyle("body", 'transform', `translateY(${this.y})`)
      },
      touchstart(e : TouchEvent) {
81 82 83
        sY = e.touches[0].screenY;
        //计时
        startMoveTime = 0
84 85
      },
      touchmove(e : TouchEvent) {
86 87 88
        this.y += e.touches[0].screenY - sY;
        dY = e.touches[0].screenY - sY
        sY = e.touches[0].screenY;
89 90 91 92
        // 限制滚动到底后上拉
        if(this.y < maxScrollTop*-1){
          this.y = maxScrollTop*-1
        }
93
        this.doMove()
94 95 96 97
        //计时
        if(startMoveTime === 0){
          startMoveTime = Date.now()
        }
98 99 100
      },
      touchend() {
        // console.log('touchend')
101 102 103 104
        if(moveIng){
          return
        }
        moveIng = true
105
        function moveTo(y : number, callback : () => void, speed : number = 10,acceleration : number = 0.2) {
106
          let interval : number = 0
107
          let _acceleration : number = 1
108 109
          interval = setInterval(() => {
            // 加速度
110
            _acceleration += acceleration
111 112

            const dy = y - this.y
113
            if (Math.abs(dy) < 3) {
114 115
              this.y = y
            } else {
116
              this.y += dy / speed * _acceleration
117 118 119 120 121 122 123 124 125 126
            }

            this.doMove()
            if (this.y == y || this.y == maxScrollTop*-1 || this.y == 0) {
              clearInterval(interval)
              callback()
            }
          }, 16)
        }
        // console.log('dY',dY);
127
        // console.log('this.y',this.y);
128
        if(this.y < 0){
129 130 131 132 133 134
          let speed:number = dY/(Date.now() - startMoveTime) * 1000
          let inertia = speed * 3
          if( Math.abs(inertia) < 100 ){
            inertia = inertia > 0 ? 100 : -100;
          }
          let endY = this.y + inertia
135 136 137 138 139 140 141 142 143 144
          // 滚动边界限制
          if(endY < maxScrollTop*-1){
            endY = maxScrollTop*-1
          }
          if(endY > 0){
            endY = 0
          }
          // console.log('endY',endY);
          moveTo(endY, () => {
            moveIng = false
145
          },10,0.1)
146
        }else{
147 148
          moveTo(0, () => {
            moveIng = false
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
        }
      },
      // 工具方法,用于快速设置 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;
183
    /* width: 1000rpx; */
184 185 186 187 188 189
    margin-left: -125rpx;
    margin-top: -50px;
    background-color: #aab8d9;
  }

  .body {
190 191
    margin-top: -200px;
    width: 750rpx;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  }

  .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;
225
    padding: 15px;
226 227 228
  }

  .content-item {
229 230
    padding: 15px;
    margin-bottom: 16px;
231 232
    background-color: #fff;
    border-radius: 5px;
233
    border: 1px solid rgba(220, 220, 220, 0.5);
234 235 236
  }

  .text {
237
    font-size: 14px;
238 239 240 241 242 243
    color: #666;
    line-height: 20px;
  }

  .nav-back {
    position: absolute;
244 245
    top: 30px;
    left: 10px;
246 247 248 249 250 251 252 253 254 255 256 257 258 259
    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>