pull-zoom-image.uvue 6.8 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
  let sY : number = 0,
      dY : number = 0,
52 53
      maxScrollTop:number = 0,
      moveIng:boolean = false;
54 55 56 57
  export default {
    data() {
      return {
        $nodeMap: new Map<string, INode>(),
58 59
        y: 0 as number,
        scrollTop:0 as number
60
      }
61 62 63 64 65 66 67
    },
    onReady() {
      uni.getSystemInfo({
        success: (e) => {
          maxScrollTop = 1425 - e.screenHeight
        }
      })
68
    },
69 70 71 72 73 74 75 76
    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})`)
        }
77 78 79 80 81 82
        this.setINodeStyle("body", 'transform', `translateY(${this.y})`)
      },
      touchstart(e : TouchEvent) {
        sY = e.touches[0].screenY;
      },
      touchmove(e : TouchEvent) {
83 84 85 86
        this.y += e.touches[0].screenY - sY;
        dY = e.touches[0].screenY - sY
        sY = e.touches[0].screenY;
        this.doMove()
87 88 89
      },
      touchend() {
        // console.log('touchend')
90 91 92 93
        if(moveIng){
          return
        }
        moveIng = true
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        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
            }

            this.doMove()
            if (this.y == y || this.y == maxScrollTop*-1 || this.y == 0) {
              clearInterval(interval)
              callback()
            }
          }, 16)
        }
        // console.log('dY',dY);
116
        console.log('this.y',this.y);
117 118
        if(this.y < 0){
          // console.log(dY);
119 120 121 122 123 124 125 126 127 128
          let endY = this.y + dY * 30
          // 滚动边界限制
          if(endY < maxScrollTop*-1){
            endY = maxScrollTop*-1
          }
          if(endY > 0){
            endY = 0
          }
          // console.log('endY',endY);
          moveTo(endY, () => {
129
            // console.log('done');
130
            moveIng = false
131 132
          },5)
        }else{
133 134 135
          moveTo(0, () => {
            // console.log('done');
            moveIng = false
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 167 168 169
        }
      },
      // 工具方法,用于快速设置 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;
170
    /* width: 1000rpx; */
171 172 173 174 175 176
    margin-left: -125rpx;
    margin-top: -50px;
    background-color: #aab8d9;
  }

  .body {
177 178
    margin-top: -200px;
    width: 750rpx;
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 209 210 211
  }

  .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;
212
    padding: 15px;
213 214 215
  }

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

  .text {
224
    font-size: 14px;
225 226 227 228 229 230
    color: #666;
    line-height: 20px;
  }

  .nav-back {
    position: absolute;
231 232
    top: 30px;
    left: 10px;
233 234 235 236 237 238 239 240 241 242 243 244 245 246
    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>