WeChat applet, imitating WeChat, pull down to display the effect of the applet, very silky

insert image description here

1. View layer

The movable-view (movable view container) of the WeChat applet and the movable area of ​​the movable-view are used.

WeChat Mini Program Documentation

<!--wxml-->
<view style="position: relative;" class="page-container">
  <view>
    二楼内容
  </view>
  <movable-area class="area-style">
    <movable-view disabled="{
    
    {disabled}}" bindchange="changeMove" bindtouchend="touchend" bindtouchstart="touchstart" y="{
    
    {y}}" class="view-style br" direction="vertical">
      一楼内容
    </movable-view>
  </movable-area>
</view>

3. css

Note: The mobile area must be larger than the movable view container, otherwise it will not be able to move.
Here I set the .area-style to 200vh. The view-style is set to 100vh.
Here is a detail:
When the mobile area reaches the bottom, continue to slide down to move the area It will bounce back to the top,
solution:
1. Setting: height of moving area = height of movable area + height of movable return,
2. Control via js: sliding down at the lowest point is invalid

/* wxss */
.page-container{
    
    
  height: 100vh;
  overflow: hidden;
  background-color: #aaa;
  text-align: center;
  font-size: 24px;
  font-weight: 500;
  padding-top: 20px;
}
.area-style {
    
    
  height: 200vh;
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
}

.view-style {
    
    
  width: 100%;
  height: 100vh;
  background-color: #fafafa;
  text-align: center;
  font-size: 24px;
  font-weight: 500;
  padding-top: 20px;
}

5. js

Control the y value through js, and then realize the opening/closing effect of the sliding and sliding animation.

Note: If the scroll-view is used on the first or second floor, it is necessary to dynamically control whether the scroll-view can slide when y=minY or y=maxY.

// ts
Page({
    
    

  /**
   * 页面的初始数据
   */
  data: {
    
    
    y: 0, // 一楼(可滑动块)距离顶部的距离
    minY: 0, // 一楼(可滑动块)距离顶部的最小距离
    maxY: 0,// 一楼(可滑动块)距离顶部的最大距离
    endY: 0,// 滑动结束是y的值
    startY: 0, // 滑动开始时y的值(要么等于minY,要么等于maxY)
    threshold: 100, // 滑动阀值(指 滑动超过此值一段距离,才会打开或者关闭,否则弹回原来的状态)
    disabled: false, // 是否禁止滑动
  },
  // 滑动过程中,把每次滑动的当前值,赋值给endY
  changeMove(e: any) {
    
    
    this.setData({
    
    
      endY: e.detail.y
    })
  },
  // 滑动结束
  touchend() {
    
    
    const {
    
     startY, endY, maxY, minY, threshold } = this.data
    let value;
    // 判断向下滑动
    if (endY > startY && endY - startY > threshold) {
    
    
      // 触发下滑,页面打开二楼
      value = maxY
      // 触发下滑成功,设置震动反馈
      wx.vibrateShort({
    
     type: 'heavy' })
    } else {
    
    
      // 未触发下滑,页面回弹到一楼
      value = minY
    }
    // 判断上划
    if (startY > endY && startY - endY < threshold) {
    
    
      // 触发上滑,页面回到一楼
      value = maxY
    }
    this.setData({
    
    
      y: value
    })
  },
  // 滑动开始
  touchstart() {
    
    
    this.setData({
    
    
      startY: this.data.y
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad() {
    
    
    // 获取屏幕高度
    const res = await wx.getSystemInfo()
    // 设置最大顶部距离(-200,目的是让一楼漏出头,方便上划,或者点击)
    this.setData({
    
    
      maxY: res.screenHeight - 200
    })
  },
})

Continuous optimization, welcome to discuss together.

Guess you like

Origin blog.csdn.net/qq_45142260/article/details/131954258