欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

uniapp自定義下拉刷新組件項目實踐總結(jié)分析

 更新時間:2023年09月18日 09:06:03   作者:MarkGuan  
在日常的開發(fā)過程中,我們經(jīng)常遇到下拉刷新的場景,很方便的刷新游覽的內(nèi)容,在此實現(xiàn)了一個下拉刷新的自定義組件,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步

準備工作

  • components新建一個q-pull文件夾,并新建一個q-pull.vue的組件;
  • 按照前面文章所說的頁面結(jié)構(gòu),編寫好預(yù)定的自定義下拉刷新組件頁面;

原理分析

自定義下拉刷新就是在之前自定義滑動觸摸組件的基礎(chǔ)上,在頂部增加一個刷新的模塊。

在頁面下拉的時候,判斷是否為下滑的方向,如果是就獲取數(shù)據(jù),數(shù)據(jù)請求完成后,隱藏刷新模塊即可。

組件實現(xiàn)

準備工作和原理分析完成后,接下來寫一個簡單的組件。

模板部分

這部分主要是顯示動畫、提示、顏色、背景色以及控制是否展示頁面。

<view class="pull">
  <view
    id="pull-container"
    :style="pullInfo.style"
    ref="pullBox"
    @touchstart="handlerStart"
    @touchmove="handlerMove"
    @touchend="handlerEnd">
    <view class="pull-head">
      <view class="pull-text"> {{ pullInfo.tipText }} </view>
    </view>
    <view class="pull-body">
      <slot></slot>
    </view>
  </view>
</view>

樣式部分

.pull {
  position: relative;
  width: 100%;
  height: 100%;
  #pull-container {
    position: relative;
    width: 100%;
    height: 100%;
    .pull-head {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      text-align: center;
      transform: translateY(-100%);
      .pull-text {
        padding: 30rpx 0;
        color: $mainColor;
        font-size: 26rpx;
        background: $f8;
        font-weight: bold;
      }
    }
    .pull-body {
      box-sizing: border-box;
      padding: 60rpx;
      text-align: left;
      font-size: 28rpx;
    }
  }
}

腳本部分

  • 引入依賴
// 導(dǎo)入依賴
import { reactive } from "vue";
// 頁面屬性
// 下拉刷新
const pullInfo = reactive({
  style: {},
  tipText: "",
  startY: 0,
  deltaY: 0,
  slowY: 0,
  resetTimer: null,
  resetTime: 500,
});
// 發(fā)送事件
const emits = defineEmits(["load"]);
  • 開始下拉
function handlerStart(e) {
  let startY = e.touches[0].pageY;
  pullInfo.style = "transition: transform 0s";
}
  • 下拉移動
function handlerMove(e) {
  e.preventDefault();
  pullInfo.deltaY = e.touches[0].pageY - pullInfo.startY;
  if (pullInfo.deltaY > 0) {
    pullInfo.tipText = "下拉刷新";
    if (pullInfo.deltaY > 60) {
      pullInfo.tipText = "松開刷新";
      pullInfo.slowY = (pullInfo.deltaY - 60) * 0.2 + 60;
    } else {
      pullInfo.slowY = pullInfo.deltaY;
    }
    pullInfo.style = `transform: translateY(${pullInfo.slowY * 2}rpx)`;
  }
}
  • 下拉結(jié)束
function handlerEnd(e) {
  pullInfo.style = "transition: transform .5s";
  if (pullInfo.deltaY > 60) {
    pullInfo.tipText = "正在加載中...";
    pullInfo.style = "transform: translateY(95rpx)";
    emits("load");
  } else {
    pullInfo.style = "transform: translateY(0)";
  }
}
  • 復(fù)位
function reset() {
  pullInfo.resetTimer = setTimeout(() => {
    pullInfo.tipText = "刷新成功!";
    pullInfo.resetTimer = setTimeout(() => {
      pullInfo.style = "transform: translateY(0)";
      clearTimeout(pullInfo.resetTimer);
    }, pullInfo.resetTime);
  }, pullInfo.resetTime);
}

實戰(zhàn)演練

模板使用

<q-pull
  ref="myPull"
  @load="loadSet">
  <!-- 插槽內(nèi)容 -->
  <view>
    <view
      v-for="(item, index) in pull.list"
      :key="index"
      >{{item}}</view
    >
  </view>
</q-pull>

腳本使用

定義數(shù)據(jù)

// 列表
const pull = reactive({
  list: [1, 2, 3, 4, 5, 6],
});

加載內(nèi)容

function loadSet() {
  // 請求數(shù)據(jù)
  setTimeout(() => {
    let num = Math.random() * 100;
    pull.list.push(num);
    if (proxy.$refs.myPull) {
      proxy.$refs.myPull.reset();
    }
  }, 3000);
}

內(nèi)置刷新

uniapp 也為我們開發(fā)者準備了內(nèi)置下拉刷新,一起去看看如何使用吧。

引入配置

pages.json文件中找到需要下拉刷新的頁面中加入以下配置。

{
  "path": "pages/index/pull-refresh",
  "style": {
    "navigationBarTitleText": "下拉刷新",
    "enablePullDownRefresh": true
  }
}

可以在 APP 端自定義樣式,在enablePullDownRefresh下面添加如下配置:

{
  //...
  "app-plus": {
    "pullToRefresh": {
      "support": true,
      "color": "#24afd6",
      "style": "circle"
    }
  }
  // ...
}

頁面引入

import { onPullDownRefresh } from "@dcloudio/uni-app";

監(jiān)聽下拉刷新

// 監(jiān)聽下拉刷新
onPullDownRefresh(() => {
  // 開始下拉刷新
  uni.startPullDownRefresh();
  // 獲取數(shù)據(jù)
  getData();
});

停止下拉刷新

// 獲取數(shù)據(jù)
function getData() {
  console.log("獲取數(shù)據(jù)!");
  setTimeout(() => {
    uni.stopPullDownRefresh();
  }, 3000);
}

案例展示

自定義組件

h5 端效果

小程序端效果

APP 端效果

內(nèi)置下拉刷新

h5 端效果

小程序端效果

APP 端效果

以上就是uniapp自定義下拉刷新組件項目實踐總結(jié)分析的詳細內(nèi)容,更多關(guān)于uniapp自定義下拉刷新組件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論