Vue.js中怎么實(shí)現(xiàn)一個(gè)上下滾動(dòng)加載組件

今天就跟大家聊聊有關(guān)Vue.js中怎么實(shí)現(xiàn)一個(gè)上下滾動(dòng)加載組件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)公司于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元曹妃甸做網(wǎng)站,已為上家服務(wù),為曹妃甸各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792

組件代碼

// scrollLoader.vue
// 滾動(dòng)加載組件

<style scoped>
  .container-main {margin: 0 auto; overflow: auto; overflow-x: hidden; padding: 0;}
  .loading{ width: 100%; height: 40px; position: relative; overflow: hidden; text-align: center; margin: 5px 0 ; color: #999; font-size: 13px;}
  .loading-icon{color: #707070;};
  .loader {
    font-size: 10px;
    margin: 8px auto;
    text-indent: -9999em;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #999;
    background: -moz-linear-gradient(left, #999 10%, rgba(255, 255, 255, 0) 42%);
    background: -webkit-linear-gradient(left, #999 10%, rgba(255, 255, 255, 0) 42%);
    background: -o-linear-gradient(left, #999 10%, rgba(255, 255, 255, 0) 42%);
    background: -ms-linear-gradient(left, #999 10%, rgba(255, 255, 255, 0) 42%);
    background: linear-gradient(to right, #999 10%, rgba(255, 255, 255, 0) 42%);
    position: relative;
    -webkit-animation: load3 1s infinite linear;
    animation: load3 1s infinite linear;
  }
  .loader:before {
    width: 50%;
    height: 50%;
    background: #999;
    border-radius: 100% 0 0 0;
    position: absolute;
    top: 0;
    left: 0;
    content: '';
  }
  .loader:after {
    background: #f5f5f5;
    width: 72%;
    height: 75%;
    border-radius: 68%;
    content: '';
    margin: auto;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
  }
  @-webkit-keyframes load3 {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
  }
  @keyframes load3 {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
  }

</style>

<template>
  <div id="scrollLoader-container" class="container-main">
    <div class="loading" v-if="topLoading">
      <div class="loader">加載中...</div>
    </div>

    <div :>
      <slot></slot>
    </div>

    <div class="loading" v-if="bottonLoading">
      <div class="loader">加載中...</div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "scroll-loader",

    props: {
      //給slot傳一個(gè)最小值,保證一開始能出現(xiàn)滾動(dòng)條
      'minHeight': {
        type: Number,
        default: 800
      }, 

    },

    created(){
    },
    computed: {
      realMinHeight(){
        return this.minHeight + 30
      }
    },
    data() {
      return {
        topLoading: false,
        bottonLoading: false,

        stopTopLoading: false, //是否停止傳播滾動(dòng)到頂部事件
        stopBottonLoading: false, //是否停止傳播滾動(dòng)到底部事件
      }
    },
    mounted(){
      this.listenScroll();
    },

    methods: {
      listenScroll(){
        var me = this;
        var topDone = (stopTopLoading) => {
          me.topLoading = false;
          if(stopTopLoading) me.stopTopLoading = true;
        };

        var bottonDone = (stopBottonLoading) => {
          me.bottonLoading = false;
          if(stopBottonLoading) me.stopBottonLoading = true;
        };
        setTimeout(function(){
          var scrollContainer = document.getElementById('scrollLoader-container');

          scrollContainer.onscroll = function(){

            if(scrollContainer.scrollTop<=0 && !me.stopTopLoading){
              if(me.topLoading) return;

              me.topLoading = true;
              me.$emit('scroll-to-top', topDone);
            }
            if((scrollContainer.offsetHeight + scrollContainer.scrollTop+1 >= scrollContainer.scrollHeight) && !me.stopBottonLoading){
              if(me.bottonLoading) return;

              me.bottonLoading = true;
              scrollContainer.scrollTop += 40;
              me.$emit('scroll-to-botton', bottonDone);
            }
          }
        }, 50)
      },

    }
  }
</script>

看完上述內(nèi)容,你們對(duì)Vue.js中怎么實(shí)現(xiàn)一個(gè)上下滾動(dòng)加載組件有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

名稱欄目:Vue.js中怎么實(shí)現(xiàn)一個(gè)上下滾動(dòng)加載組件
當(dāng)前路徑:http://bm7419.com/article18/pcgsdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航做網(wǎng)站、靜態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、定制網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名