最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于vue封裝下拉刷新上拉加載組件

 更新時(shí)間:2021年09月26日 15:00:13   作者:yang_web  
這篇文章主要為大家詳細(xì)介紹了基于vue封裝下拉刷新上拉加載組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

基于vue和原生javascript封裝的下拉刷新上拉加載組件,供大家參考,具體內(nèi)容如下

  • upTilte插槽是下拉刷新的自定義內(nèi)容放的地方
  • downTilte插槽是上拉加載的自定義內(nèi)容放的地方
  • 默認(rèn)插槽為列表內(nèi)容區(qū)域

組件代碼如下

<template>
  <div class="refresh" id="refresh">
    <slot name="upTilte"></slot>
    <slot></slot>
    <slot name="downTilte"></slot>
  </div>
</template>

<script>
export default {
  name: 'PullupOrPulldownRefresh',
  props: {
    // 最大移動距離
    maxMove: {
      type: Number,
      default: 300
    },
    // 阻尼系數(shù)
    friction: {
      type: Number,
      default: 0.3
    }
  },
  data() {
    return {
      startY: 0,
      ul: null,
      draw: null,
      up: null,
      down: null,
      y: 0 // 慣性回彈的距離
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.draw = document.getElementById('refresh')
      this.ul = this.draw.children[1]
      this.up = this.draw.children[0]
      this.down = this.draw.children[2]
      this.draw.addEventListener('touchstart', this.touchstart)
      this.draw.addEventListener('touchmove', this.touchmoveEvent)
      this.draw.addEventListener('touchend', this.touchendEvent)
    })
  },
  methods: {
    // 觸摸開始事件
    touchstart(event) {
      this.startY = event.changedTouches[0].clientY
    },
    // 觸摸移動事件
    touchmoveEvent(event) {
      const height = this.ul.clientHeight - this.draw.clientHeight
      if (height === this.draw.scrollTop || this.draw.scrollTop === 0) {
        var a = event.changedTouches[0].clientY - this.startY
        this.y = a <= this.maxMove ? a : this.maxMove
        // 為了清除卡頓問題,需要清除過渡效果
        this.ul.style.transition = 'none'
        this.ul.style.transform = 'translateY(' + this.friction * this.y + 'px)'
        // 修改狀態(tài)
        const upHeight = -this.up.clientHeight + this.friction * this.y
        // 下拉開始
        if (this.friction * this.y > 0) (this.setStatus(this.friction * this.y), this.up.style.transition = 'none', this.up.style.transform = 'translateY(' + upHeight + 'px) translateX(-50%)')
        // 上拉開始
        if (this.friction * this.y < 0) (this.setStatus(this.friction * this.y), this.down.style.transition = 'none', this.down.style.marginTop = this.friction * this.y + 'px')
      }
    },
    // 觸摸結(jié)束事件
    touchendEvent(event) {
      if (this.friction * this.y >= 50) this.$emit('RefreshUp', this.friction * this.y)
      else if (this.friction * this.y < -50) this.$emit('RefreshDown', this.friction * this.y)
      else this.resetStyle()
    },
    // 重置并且添加過渡效果
    resetStyle() {
      this.ul.style.transition = 'transform .6s'
      this.ul.style.transform = 'translateY(' + 0 + 'px)'
      this.up.style.transition = 'all .6s'
      this.up.style.transform = 'translateY(-' + this.up.clientHeight + 'px) translateX(-50%)'
      this.down.style.transition = 'all .6s'
      this.down.style.marginTop = -this.down.clientHeight + 'px'
    },
    // 設(shè)置刷新狀態(tài)
    setStatus(y) {
      this.$emit('setStatus', y)
    }
  }
}
</script>

<style lang="scss">
.refresh {
  width: 100%;
  height: 100vh;
  border: 2px solid #ccc;
  position: relative;
  overflow: hidden;
  overflow: auto;
  position: fixed;
  ul {
    zoom: 1;
    padding: 0 10%;
  }

  ul::after {
    content: '';
    display: block;
    visibility: hidden;
    height: 0;
    clear: both;
  }

  li {
    list-style: none;
    width: 100%;
    height: 50px;
    line-height: 50px;
    text-align: center;
  }
  .UpRefresh {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    z-index: -9;
  }
  .DownRefresh {
    position: relative;
    left: 50%;
    transform: translateX(-50%);
    margin-top: -10px;
    z-index: -9;
  }
}
</style>
  • 組件的使用方法
  • friction為摩擦系數(shù)
  • @RefreshUp為下拉到一定距離觸發(fā)事件
  • @RefreshDown為上拉到一定距離觸發(fā)事件
  • @setStatus為更改刷新狀態(tài)的方法
<template>
  <div>
    <PullupOrPulldownRefresh
      ref="PullupOrPulldownRefresh"
      :maxMove="maxMove"
      :friction="friction"
      @RefreshUp="RefreshUp"
      @RefreshDown="RefreshDown"
      @setStatus="setStatus"
    >
      <template v-slot:upTilte>
        <!-- <div class="UpRefresh" v-show="isUpRefresh">{{ Uptitle }}</div> -->
        <div class="UpRefresh" v-show="isUpRefresh">
          <img :src="require('@/assets/logo.png')" alt="" />
          <p>{{ Uptitle }}</p>
        </div>
      </template>
      <ul>
        <li
          v-for="(item, index) in data"
          :key="index"
          style="background: orange"
        >
          {{ item }}
        </li>
      </ul>
      <template v-slot:downTilte>
        <div class="DownRefresh" v-show="isDownRefresh">{{ Downtitle }}</div>
      </template>
    </PullupOrPulldownRefresh>
  </div>
</template>

<script>
export default {
  data() {
    return {
      maxMove: 300,
      friction: 0.3,
      data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      isUpRefresh: false,
      isDownRefresh: false,
      Downtitle: '上拉加載更多',
      Uptitle: '下拉刷新'
    }
  },
  methods: {
    setStatus(y) {
      if (y && y > 0) {
        this.isUpRefresh = true
        this.Uptitle = '下拉刷新'
        if (y >= 50) this.Uptitle = '松手刷新'
        return
      }
      this.isDownRefresh = true
      this.Downtitle = '上拉加載更多'
      if (y <= -50) this.Downtitle = '松手加載更多'
    },
    RefreshUp(y) {
      if (!y) return
      if (y >= 50) {
        this.Uptitle = '正在刷新'
        setTimeout(() => {
          for (var i = 1; i <= 10; i++) {
            this.data.push(this.data[this.data.length - 1] + 1)
          }
          this.$refs.PullupOrPulldownRefresh.resetStyle() // 回彈重置
        }, 1000)
      }
    },
    RefreshDown(y) {
      if (!y) return
      if (y <= -50) {
        this.Downtitle = '正在加載'
        setTimeout(() => {
          for (var i = 1; i <= 10; i++) {
            this.data.push(this.data[this.data.length - 1] + 1)
          }
          this.$refs.PullupOrPulldownRefresh.resetStyle() // 回彈重置
        }, 1000)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.UpRefresh img{
  width: 30px;
}
</style>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 前端虛擬滾動列表實(shí)現(xiàn)代碼(vue虛擬列表)

    前端虛擬滾動列表實(shí)現(xiàn)代碼(vue虛擬列表)

    前端的性能瓶頸那就是頁面的卡頓,當(dāng)然這種頁面的卡頓包含了多種原因,下面這篇文章主要給大家介紹了關(guān)于前端虛擬滾動列表實(shí)現(xiàn)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-06-06
  • 使用mint-ui實(shí)現(xiàn)省市區(qū)三級聯(lián)動效果的示例代碼

    使用mint-ui實(shí)現(xiàn)省市區(qū)三級聯(lián)動效果的示例代碼

    下面小編就為大家分享一篇使用mint-ui實(shí)現(xiàn)省市區(qū)三級聯(lián)動效果的示例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • vue時(shí)間戳和時(shí)間的相互轉(zhuǎn)換方式

    vue時(shí)間戳和時(shí)間的相互轉(zhuǎn)換方式

    本文通過示例代碼介紹了vue時(shí)間戳和時(shí)間的相互轉(zhuǎn)換方式,通過場景分析介紹了vue3使用組合式api將時(shí)間戳格式轉(zhuǎn)換成時(shí)間格式(2023年09月28日 10:00),感興趣的朋友一起看看吧
    2023-12-12
  • 如何在vue中使用pdfjs預(yù)覽pdf文件

    如何在vue中使用pdfjs預(yù)覽pdf文件

    本文主要講解了如何在vue中使用pdfjs預(yù)覽pdf文件,這樣的優(yōu)勢是無須讓用戶安裝專門的軟件即可實(shí)現(xiàn)預(yù)覽,下面就看看如何實(shí)現(xiàn)這個(gè)需求
    2021-06-06
  • Vue渲染失敗的幾種原因及解決方案

    Vue渲染失敗的幾種原因及解決方案

    這篇文章主要介紹了Vue渲染失敗的幾種原因及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Vue文件如何轉(zhuǎn)換成base64并去除多余的文件類型前綴

    Vue文件如何轉(zhuǎn)換成base64并去除多余的文件類型前綴

    這篇文章主要介紹了Vue文件如何轉(zhuǎn)換成base64并去除多余的文件類型前綴問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue實(shí)現(xiàn)翻牌動畫

    vue實(shí)現(xiàn)翻牌動畫

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)翻牌動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Vue安裝與環(huán)境配置步驟詳解

    Vue安裝與環(huán)境配置步驟詳解

    在開始學(xué)習(xí)vue的時(shí)候,對于新手安裝這個(gè)環(huán)境是真的搞人心態(tài),不友好,下面這篇文章主要給大家介紹了關(guān)于Vue安裝與環(huán)境配置的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • 一文搞懂Vue3中的異步組件defineAsyncComponentAPI的用法

    一文搞懂Vue3中的異步組件defineAsyncComponentAPI的用法

    這篇文章主要介紹了一文搞懂Vue3中的異步組件,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • Vue3+Vue Router實(shí)現(xiàn)動態(tài)路由導(dǎo)航的示例代碼

    Vue3+Vue Router實(shí)現(xiàn)動態(tài)路由導(dǎo)航的示例代碼

    隨著單頁面應(yīng)用程序(SPA)的日益流行,前端開發(fā)逐漸向復(fù)雜且交互性強(qiáng)的方向發(fā)展,在這個(gè)過程中,Vue.js及其生態(tài)圈的工具(如Vue Router)為我們提供了強(qiáng)大的支持,本文將介紹如何在Vue 3中使用Vue Router實(shí)現(xiàn)動態(tài)路由導(dǎo)航,需要的朋友可以參考下
    2024-08-08

最新評論

科尔| 嘉义市| 福州市| 兴业县| 洞口县| 沈丘县| 青河县| 老河口市| 彰化市| 永兴县| 门头沟区| 德昌县| 临颍县| 格尔木市| 徐闻县| 芦山县| 蒲江县| 西宁市| 盐山县| 高雄县| 藁城市| 卓尼县| 镇巴县| 北碚区| 察雅县| 潜山县| 新沂市| 中方县| 米泉市| 富平县| 新建县| 平和县| 通海县| 玉山县| 祁连县| 香格里拉县| 渝北区| 陇川县| 确山县| 江北区| 金溪县|