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>

原文地址:https://www.cnblogs.com/guoliping/p/11112577.html

时间: 2024-08-13 16:17:06

vue 实现滚动到页面底部开始加载更多的相关文章

Jquery鼠标滚动到页面底部自动加载更多内容,使用分页

https://www.cnblogs.com/qhorse/p/4717726.html index.php代码 [html] view plaincopy<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://

滑轮滚动到页面底部ajax加载数据

滚动下拉到页面底部加载数据是很多瀑布流网站的做法,那来看看配合jsonp是如何实现的吧 当然本例子采用的是jquery库,后期会做成原生js.本例的数据调用的是锋利的jquery一书提供的一段json. 首先要先判断页面怎么样才是滚动到底部,也就是scrollTop+window的height是否大于document的height,jquery如下代码: $(window).scrollTop()+$(window).height()>=$(document).height(): 再给windo

当滚动条滚动到页面底部自动加载增加内容的js代码

这篇文章主要介绍了如何使用javscript实现滚动条滚动到页面底部自动加载增加页面内容,需要的朋友可以参考下..1,注册页面滚动事件,window.onscroll = function(){ }; 2,相关获取页面高度.滚动条位置.文档高度的函数: 复制代码 代码如下://获取滚动条当前的位置 function getScrollTop() { var scrollTop = 0; if (document.documentElement && document.documentEle

Android Demo之旅 ListView底部添加加载更多按钮实现数据分页

在我们的实际项目中,数据应该说是很多的,我们的ListView不可能一下子把数据全部加载进来,我们可以当滚动条滚动到ListView的底部的时候,给一个更多的提示,当我们点击它即加载下一页的数据,相当与我们的分页效果,参考网上的东西,写了一个小小的demo,并总结了一些知识点,功能图如下:    源代码下载地址:http://download.csdn.net/detail/harderxin/7762625 掌握知识点: 1)自定义Adapter,将数据和ListView绑定起来 2)理解La

探索SwipeRefreshLayout配合自定义ListView完成下拉刷新、滑到底部自动加载更多

在Android开发过程中经常需要实现上下拉刷新功能,Google推出的下拉刷新控件SwipeRefreshLayout(彩虹条),由于官方版本只有下拉刷新而没有上拉加载更多的功能,很多人也尝试在这个基础上进行改写.今天尝试一下使用SwipeRefreshLayout配合自定义ListView实现下拉刷新.滑到底部自动加载更多的功能. 效果图如下所示,在进入页面的时候加载自动刷新,滑到底部自动加载更多,当数据已经加载完成则显示已经加载完成,,否则上拉任可继续加载 先贴一下项目结构图吧,这样可能对

嗯嗯,一句代码就搞定 RecycleView 侧滑菜单、添加头部底部、加载更多

很早就萌生了将这种方案封装为一个开源库的想法,旨在实现调用方式最简单,且又不失可定制性.本库最大的特点的是采用了 Glide 简洁明了的链式调用方式,一句代码即可添加侧滑菜单.头部底部等. 特性: 1.自定义侧滑菜单布局 2.添加头部.底部 3.轻松实现加载更多 4.设置 item 间距 5.多种 item 类型 6.支持 LinearLayout 及 GridLayout 7.一句代码实现所有功能 效果: 左侧滑菜单.右侧滑菜单.自定义菜单布局:      头部.多头部:      底部.多底

【iOS开发-62】自定义cell制作团购页面、顶部图片轮播、底部模拟加载更多功能,核心是练习代理模式

(1)效果 (2)案例源代码免费下载 团购页面+iOS源代码+头部广告轮播+底部加载更多 (3)补充 在源代码中,有一处瑕疵:就是因为是单线程,所以在上下拖动页面的时候,上面的图片轮播会停止.所以我们需要兼顾,解决方案,把定时器加到当前的runLoop中. 即在WPTgHeaderView.m的playOn方法中添加一行代码: -(void)playOn{ timer=[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector

H5页面下拉加载更多(实用版)

近期在做一个H5网站,需要下拉加载更多产品列表的功能.百度搜索了好久,什么说法都有,什么插件都有.   醉了.基本上每一个能直接拿来用的. 最后发现: 1.dropload.js 插件  还可以,但是有个问题,只能单页使用比较方便.带有tab标签的不推荐使用. 2.自己百度了半天总结出来的一套: <script> document.addEventListener('scroll', watchScroll); var itemIndex = 0; var classid = 10; var

关于H5判定区域里面滑动到底部,加载更多的总结

1.如何判定H5中滑动到底部,然后加载更多的功能实现. 思路:我们需要设定一个固定高度的盒子,然后我们利用scroll来监听滚动,当scrollTop(滚动的距离) + clientHeight(页面的设定的高度) >= scrollHeight(页面内容总高度) 这样我们就可以判定页面内容滑动到底部了,然后加载更多数据. $("#Box").scroll(function () { let scrollTop = document.getElementById("Bo