vue 實現滾動到頁面底部開始加載更多


直接上代碼:

<template>
  <div class="newsList">
    <div v-for="(items, index) in newsList">
      <div class="date">{{showDay(index)}}</div>
      <div class="list" >
        <ul>
          <li class="list-item" v-for="item in items">
            <span class="text">{{item.title}}</span>
            <img :src="attachImageUrl(item.images[0])" class="image"/>
          </li>
        </ul>
      </div>
    </div>
    <div class="infinite-scroll" v-show="loading">
      <svg class="loader-circular" viewBox="25 25 50 50">
        <circle class="loader-path" cx="50" cy="50" r="20" fill="none" stroke="rgb(53, 157, 218)" stroke-width="5"></circle>
      </svg>
      <span class="infinite-scroll-text">{{tips}}</span>
    </div>
  </div>
</template>

<script>
  import axios from 'axios';

  export default {
    data () {
      return {
        newsList: [],
        date: [],
        todayDate: '',
        REQUIRE: true,
        loading: false,
        tips: '努力加載中...'
      }
    },
    created () {
      // 獲取今日新聞
      axios.get('http://zhihuapi.herokuapp.com/api/4/news/latest')
        .then( (res) => {
        this.newsList.push(res.data['stories'])
        this.date.push(res.data['date']);
        this.todayDate = res.data['date']
      })
    },
    mounted () {
      // 添加滾動事件,檢測滾動到頁面底部
      window.addEventListener('scroll', this.scrollBottom)
    },
    methods: {
      scrollBottom() {
        // 滾動到頁面底部時,請求前一天的文章內容
        if (((window.screen.height + document.body.scrollTop) > (document.body.clientHeight)) && this.REQUIRE) {
          // 請求的數據未加載完成時,滾動到底部不再請求前一天的數據
          this.REQUIRE = false;
          this.loading = true;
          this.tips = '努力加載中...';
          axios.get('http://zhihuapi.herokuapp.com/api/4/news/before/' + this.todayDate).then((res) => {
            this.newsList.push(res.data['stories']);
          this.date.push(res.data['date']);
          this.todayDate = res.data['date'];
          // 請求的數據加載完成后,再次滾動到底部時,允許請求前一天數據
          this.$nextTick(() => {
            this.REQUIRE = true;
            this.loading = false;
          });
        }).catch(() => {
            this.tips = '連接失敗,請稍后重試';
          // 請求失敗時,將 REQUIRE 置為 true,滾動到底部時,再次請求
          this.REQUIRE = true;
        });
        }
      },
      showDay (index) {
        if (index === 0) {
          return '今日新聞'
        } else {
          return this.getToday(index)
        }
      },
      getToday (index) {
        let year = this.date[index].slice(0, 4);
        let month = this.date[index].slice(4, 6);
        let day = this.date[index].slice(6);
        let today = new Date(year + '/' + month + '/' + day);
        let week = ['日', '一', '二', '三', '四', '五', '六'];
        return month + '月' + day + '日' + ' 星期' + week[today.getDay()];
      },
      attachImageUrl (srcUrl) {
        if (srcUrl !== undefined) {
          return 'http://read.html5.qq.com/image?src=forum&q=5&r=0&imgflag=7&imageUrl=' + srcUrl.slice(0, 4) + srcUrl.slice(5);
        }
      }
    }
  }
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM