直接上代码:

<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>

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

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

    index.php代码   [html] view plaincopy <!DOCTYPE html PUBLIC ;}                .single_item{padding: ...

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

    1.index.php <style type="text/css"> #container{margin:10px auto;width: 660px; border ...

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

    这篇文章主要介绍了如何使用javscript实现滚动条滚动到页面底部自动加载增加页面内容,需要的朋友可以参考下..1,注册页面滚动事件,window.onscroll = function(){ }; ...

  4. vue使用H5实现滚动到页面底部时加载数据

    使用原生vue实现瀑布流,发现无法实现小程序那种滚动到地步触发加载效果,只能自己研究了 实现效果: 实现代码: 首先添加监听滚动事件 mounted() { window.addEventListen ...

  5. jQuery+ajax实现滚动到页面底部自动加载图文列表效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. JS 判断滚动底部并加载更多效果。。。。。。。。。

    JS 判断滚动底部并加载更多效果......... <html lang="zh-cn"> <head> <meta http-equiv=" ...

  7. UWP-ListView到底部自动加载更多数据

    原文:UWP-ListView到底部自动加载更多数据 ListView绑定的数据当需要“更多”时自动加载 ListView划到底部后,绑定的ObservableCollection列表数据需要加载的更 ...

  8. RecycleView 滑动到底部,加载更多

    android.support.v7 包提供了一个新的组件:RecycleView,用以提供一个灵活的列表试图.显示大型数据集,它支持局部刷新.显示动画等功能,可以用来取代ListView与GridV ...

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

    近期在做一个H5网站,需要下拉加载更多产品列表的功能.百度搜索了好久,什么说法都有,什么插件都有.   醉了.基本上每一个能直接拿来用的. 最后发现: 1.dropload.js 插件  还可以,但是 ...

随机推荐

  1. jieba模块

    jieba模块 下载 pip install jieba 使用 import jieba 精确模式 jieba.cut() 直接打印出的是单个文字 转换成列表-->jieba.lcut() im ...

  2. mybatis框架-choose when otherwise 的使用

    需求:模拟实际业务情况,传入多条件进行查询 /** * 需求:模拟实际业务,用户传入多个条件,进行用户列表信息的查询 * @param roleids * @return */ public List ...

  3. VScode插件:Todo Tree

    Todo Tree 用于记录很多需要做但是暂时没办法立即做的事情,如修改样式,日期格式处理等 用法: // TODO: 流程图用canvas重构 然后,你在代码中写的TODO,会被识别出来,点击即可查 ...

  4. update Select 从查询的结果中更新表

    UPDATE tbl_a a INNER JOIN tbl_b b ON a.arg=b.argSET a.arg2=b.arg2 这个语法即可实现

  5. Polling 、Long Polling 和 WebSocket

    最近在学习研究WebSocket,了解到Polling 和Long Polling,翻阅了一些博文,根据自己的理解,做个学习笔记 Polling (轮询): 这种方式就是客户端定时向服务器发送http ...

  6. python client.py

    vi /Library/Frameworks/Python.framework/Versions//http/client.py vi /Library/Frameworks/Python.frame ...

  7. 基于web公交查询系统自我安排进度

    这周完成站点信息管理

  8. ECMAScript6之Class

    1.Class的基本语法 1.1简介 基本上,ES6 的class可以看作只是一个语法糖,它的绝大部分功能,ES5 都可以做到,新的class写法只是让对象原型的写法更加清晰.更像面向对象编程的语法而 ...

  9. tcp & 长连接 短连接

    参考文档: tcp协议 http://blog.chinaunix.net/uid-26833883-id-3627644.html 长连接和短连接 http://blog.csdn.net/free ...

  10. Elasticsearch详解-续

    Elasticsearch详解-续 Chandler_珏瑜  关注  7.6 2019.05.22 10:46* 字数 8366 阅读 675评论 4喜欢 25 5.3 性能调优  Elasticse ...