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

vue組件實(shí)現(xiàn)列表自動(dòng)無(wú)限循環(huán)的方法

 更新時(shí)間:2023年11月14日 09:35:55   作者:The?ever?Boy  
最近剛好有個(gè)功能需要實(shí)現(xiàn)列表的無(wú)限循環(huán)滾動(dòng),這篇文章主要給大家介紹了關(guān)于vue組件實(shí)現(xiàn)列表自動(dòng)無(wú)限循環(huán)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下

前述:

用過(guò)vue-seamless-scroll插件,手動(dòng)滾動(dòng)列表到底部出現(xiàn)了空白現(xiàn)象,某些表現(xiàn)不符合項(xiàng)目場(chǎng)景,故自己寫(xiě)了一個(gè)自己用的組件,如果有人需要可以直接拿去用,如有不足請(qǐng)指教勿噴!

主要功能:

  • 列表自動(dòng)無(wú)限循環(huán)滾動(dòng)
  • 鼠標(biāo)移入停止?jié)L動(dòng),移出繼續(xù)滾動(dòng)
  • 待滾動(dòng)內(nèi)容高度未鋪滿并超過(guò)容器高度時(shí)不滾動(dòng)
  • 支持滾動(dòng)速度、單次滾動(dòng)時(shí)間間隔、單次滾動(dòng)高度,三個(gè)參數(shù)控制
  • 可自己手動(dòng)滾動(dòng)列表

效果圖 :

組件代碼: 

<template>
  <div class="scroll-outer" ref="outer" @mouseover="onMouseover" @mouseleave="onMouseleave">
    <div class="scroll-inner-box" ref="scrollBox">
      <div class="scroll-item-box" ref="scrollItemBox">
        <slot></slot>
      </div>
      <div v-if="showSecond" class="scroll-item-box">
        <slot></slot>
      </div>
    </div>
  </div>
</template>
  <script>
export default {
  name: "my-auto-scroll",
  props: {
    list: {
      type: Array,
      default: () => [
        { name: "張三1" },
        { name: "張三2" },
        { name: "張三3" },
        { name: "張三4" },
        { name: "張三5" },
        { name: "張三6" },
        { name: "張三7" },
        { name: "張三8" },
        { name: "張三9" },
        { name: "張三10" },
      ],
    },
    speed: {
      type: Number,
      default: 0.1,
    },
    //滾動(dòng)作單步運(yùn)動(dòng)時(shí)的單純運(yùn)動(dòng)距離
    singleHeight: {
      type: Number,
      default: 0,
    },
    //單步運(yùn)動(dòng)的時(shí)間間隔
    waitTime: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      rafId: null,
      y: 0,
      showSecond: false,
      controleHeight: 0,
    };
  },
  watch: {
    list: {
      handler(newVal) {
        var that = this;
        this.$nextTick(() => {
          console.log(newVal);
          if (newVal && newVal.length > 0) {
            let scrollBox = that.$refs.scrollBox;
            let outer = that.$refs.outer;

            if (this.myReq) {
              cancelAnimationFrame(this.myReq);
            }
            // 開(kāi)啟動(dòng)畫(huà)
            if (this.canRun()) this.reqAnimationFrame();
            // this.reqAnimationFrame();
            // 手動(dòng)滾動(dòng)到底部時(shí)滾動(dòng)條重置到最上邊,同時(shí)滾動(dòng)盒子重置為top:0
            outer.addEventListener("scroll", function () {
              if (
                outer.scrollTop + outer.clientHeight + 4 >=
                outer.scrollHeight
              ) {
                outer.scrollTop = 0;
                that.y = 0;
                scrollBox.style.top = 0;
              }
            });
          }
        });
      },
      deep: true,
      immediate: true,
    },
  },
  mounted() {
    window.addEventListener("resize", this.listenResizeFn);
  },
  methods: {
    listenResizeFn() {
      cancelAnimationFrame(this.myReq);
      if (this.canRun()) this.reqAnimationFrame();
    },
    onMouseover() {
      clearTimeout(this.timer);
      cancelAnimationFrame(this.myReq);
    },
    onMouseleave() {
      if (this.canRun()) this.reqAnimationFrame();
    },
    canRun() {
      let scrollItemBox = this.$refs.scrollItemBox;
      let scrollBox = this.$refs.scrollBox;
      let outer = this.$refs.outer;
      // 開(kāi)啟動(dòng)畫(huà)條件:滾動(dòng)盒子(scrollBox)高度高于外層容器(outer)高度
      if (outer.offsetHeight >= scrollItemBox.offsetHeight) {
        this.showSecond = false;
        outer.scrollTop = 0;
        this.y = 0;
        scrollBox.style.top = 0;
        return false;
      } else {
        this.showSecond = true;
        return true;
      }
    },
    //獲取dom元素的高度:content+padding+margin+border
    getComputedHeight(dom) {
      let computedStyle = getComputedStyle(dom);

      let computedHeight =
        dom.offsetHeight +
        parseFloat(computedStyle.marginTop) +
        parseFloat(computedStyle.marginBottom);
      return computedHeight;
    },
    reqAnimationFrame() {
      //外層容器
      let outer = this.$refs.outer;
      //滾動(dòng)盒子
      let scrollBox = this.$refs.scrollBox;
      //滾動(dòng)盒子下邊的第一個(gè)scroll-item-box,
      let scrollItemBox = this.$refs.scrollItemBox;

      //滾動(dòng)速度
      this.speed = this.speed > 1 ? 1 : this.speed < 0 ? 0.1 : this.speed;

      //取第一個(gè)scrollItemBox高度
      let definedHeight = this.getComputedHeight(scrollItemBox);
      //持續(xù)滾動(dòng)
      this.y = this.y + this.speed;
      scrollBox.style.top = -this.y + "px";

      //====添加滾動(dòng)間隔控制====開(kāi)始
      if (this.singleHeight >= 20 && this.waitTime > 500) {
        if (this.controleHeight >= this.singleHeight) {
          cancelAnimationFrame(this.myReq);
          this.controleHeight = 0;
          this.timer = setTimeout(() => {
            if (this.canRun) this.reqAnimationFrame();
          }, this.waitTime);
          return;
        } else {
          // 一次移動(dòng)高度未達(dá)到指定距離繼續(xù)執(zhí)行動(dòng)畫(huà)
          this.controleHeight += this.speed;
        }
      }
      //====添加滾動(dòng)間隔控制====結(jié)束

      //當(dāng)滾動(dòng)到第一個(gè)scroll-item-box高度時(shí)scrollBox重置為top:0,視覺(jué)上是無(wú)縫滾動(dòng)
      if (this.y >= definedHeight) {
        this.y = 0;
      }
      this.myReq = window.requestAnimationFrame(this.reqAnimationFrame);
    },
  },
  destroyed() {
    window.removeEventListener("resize", this.listenResizeFn);
    cancelAnimationFrame(this.myReq);
    if (this.timer) clearTimeout(this.timer);
  },
};
</script>
  <style lang="scss">
.scroll-outer {
  height: 100%;
  overflow-x: hidden;
  position: relative;
  &::-webkit-scrollbar {
    width: 0.3vw;
  }
  &:hover::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 0.1vw rgba(0, 0, 0, 0.3);
    border-radius: 0.1vw;
    background-color: #295099;
    opacity: 1;
    // display: none;
  }
  &:hover::-webkit-scrollbar-thumb {
    opacity: 1;
    border-radius: 0.1vw;
    -webkit-box-shadow: inset 0 0 0.1vw rgba(0, 0, 0, 0.3);
    background-color: #0ba9ea;
  }
}
.scroll-inner-box {
  height: auto;
  position: absolute;
  width: 100%;
  top: 0;
  left: 0;
}
</style>
  
  

使用示例: 

import autoScroll from "@/components/autoScroll";
<autoScroll :list="list" :speed="0.5" :waitTime="2000" :singleHeight="100">
            <div class="t-item" v-for="(item,index) in list" :key="index">
              <div class="tvalue" style="flex: 0 0 30%;">{{ item.jgjc }}</div>
              <span class="tvalue">{{ item.total||'--' }}</span>
              <span class="tvalue" style="color:#0FCBDE">{{ item.ypc||'--' }}</span>
              <span class="tvalue" style="color:#F15730">{{ item.zlz||'--' }}</span>
              <span class="tvalue" style="color:#17DB68">{{ item.zlwc||'--' }}</span>
            </div>
          </autoScroll>

使用注意:

  • autoScroll容器默認(rèn)是占外層容器寬高百分百,要自己在autoScroll外層加個(gè)容器
  • 參數(shù)waitTIme和singleHeight同時(shí)存在,才能出現(xiàn)滾動(dòng)動(dòng)畫(huà)間隔執(zhí)行的效果 
  • 樣式用了sass,如果有問(wèn)題可以去掉或者導(dǎo)入sass

總結(jié) 

到此這篇關(guān)于vue組件實(shí)現(xiàn)列表自動(dòng)無(wú)限循環(huán)的文章就介紹到這了,更多相關(guān)vue列表自動(dòng)無(wú)限循環(huán)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Vuex中g(shù)etters的使用教程

    詳解Vuex中g(shù)etters的使用教程

    在Store倉(cāng)庫(kù)里,state就是用來(lái)存放數(shù)據(jù)。如果很多組件都使用這個(gè)過(guò)濾后的數(shù)據(jù),我們是否可以把這個(gè)數(shù)據(jù)抽提出來(lái)共享?這就是getters存在的意義。我們可以認(rèn)為,getters是store的計(jì)算屬性。本文將具體介紹一下getters的使用教程,需要的可以參考一下
    2022-01-01
  • vue?proxytable代理根路徑的同時(shí)增加其他代理方式

    vue?proxytable代理根路徑的同時(shí)增加其他代理方式

    這篇文章主要介紹了vue?proxytable代理根路徑的同時(shí)增加其他代理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue+webpack實(shí)現(xiàn)異步加載三種用法示例詳解

    vue+webpack實(shí)現(xiàn)異步加載三種用法示例詳解

    這篇文章主要介紹了vue+webpack實(shí)現(xiàn)異步加載的三種用法,文中給大家提到了vue+webpack實(shí)現(xiàn)異步組件加載的代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2018-04-04
  • Vue.js綁定HTML class數(shù)組語(yǔ)法錯(cuò)誤的原因分析

    Vue.js綁定HTML class數(shù)組語(yǔ)法錯(cuò)誤的原因分析

    Vue.js綁定HTML class數(shù)組語(yǔ)法錯(cuò)誤有哪些原因?qū)е碌哪?,該如何解決呢?下面小編給大家分享Vue.js綁定HTML class數(shù)組語(yǔ)法錯(cuò)誤的原因分析,感興趣的朋友一起看看吧
    2016-10-10
  • vue中可以綁定多個(gè)事件嗎

    vue中可以綁定多個(gè)事件嗎

    這篇文章主要介紹了vue中可以綁定多個(gè)事件嗎,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue項(xiàng)目中使用v-show和v-if指令以及原理、區(qū)別和適用場(chǎng)景說(shuō)明

    Vue項(xiàng)目中使用v-show和v-if指令以及原理、區(qū)別和適用場(chǎng)景說(shuō)明

    Vue中v-show通過(guò)display屬性控制顯示隱藏,元素始終存在DOM,適合頻繁切換;v-if條件為false時(shí)移除元素,適合初始條件不成立的情況,性能上首次渲染可能更優(yōu),但切換頻繁時(shí)v-show更高效,其他方式包括綁定style/class、計(jì)算屬性及第三方動(dòng)畫(huà)庫(kù)
    2025-07-07
  • Vue.js常用指令的使用小結(jié)

    Vue.js常用指令的使用小結(jié)

    這篇文章主要介紹了Vue.js常用指令的使用,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-06-06
  • vite如何實(shí)現(xiàn)構(gòu)建vue3新項(xiàng)目

    vite如何實(shí)現(xiàn)構(gòu)建vue3新項(xiàng)目

    這篇文章主要介紹了vite如何實(shí)現(xiàn)構(gòu)建vue3新項(xiàng)目方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-05-05
  • Vue.js中的extend綁定節(jié)點(diǎn)并顯示的方法

    Vue.js中的extend綁定節(jié)點(diǎn)并顯示的方法

    在本篇內(nèi)容里小編給大家整理了關(guān)于Vue.js中的extend綁定節(jié)點(diǎn)并顯示的方法以及相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-06-06
  • vue3使用jsx語(yǔ)法詳解

    vue3使用jsx語(yǔ)法詳解

    在絕大多數(shù)情況下,Vue 推薦使用模板語(yǔ)法來(lái)創(chuàng)建應(yīng)用,然而在某些使用場(chǎng)景下,我們真的需要用到 JavaScript 完全的編程能力,這時(shí)JSX或渲染函數(shù)就派上用場(chǎng)了,下面就來(lái)介紹一下如何使用
    2026-02-02

最新評(píng)論

南康市| 龙川县| 策勒县| 白城市| 贺州市| 藁城市| 江油市| 磴口县| 天峻县| 万载县| 安龙县| 吕梁市| 娄底市| 建阳市| 久治县| 苏尼特左旗| 张掖市| 浦北县| 瓦房店市| 什邡市| 蓝田县| 岳阳县| 德格县| 航空| 集贤县| 衢州市| 安溪县| 鱼台县| 上思县| 黑水县| 修文县| 雷波县| 应城市| 渭源县| 黄龙县| 定州市| 彰武县| 峨边| 渭源县| 威远县| 卢氏县|