直接上代码:

<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. 新版Notepad++加十六进制查看的插件HexEditor(转载+总结)

    用到hex文件查看,老版本的notepad++是可以安装的,新版本的是安装不了,这个差评啊, 但是网上有方法:https://github.com/chcg/NPP_HexEdit/releases下 ...

  2. serialize()和new FormData()的区别

    serialize()和FormData对象都可将表单数据序列化,后通过ajax异步提交 ,序列化表单就是将form表单中所有name属性序列化成KEY-VALUE的形式,提交到后台,后台以对象相应的 ...

  3. 排序算法-归并排序(Java)

    package com.rao.sort; import java.util.Arrays; /** * @author Srao * @className MergeSort * @date 201 ...

  4. BZOJ 4826: [Hnoi2017]影魔 单调栈+可持久化线段树

    Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样 的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个 ...

  5. Python中字符串匹配函数startswith()函数

    1.函数用途含义 Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在指定范围内 ...

  6. redis哨兵模式实现主从故障切换

    环境设定base2 172.25.78.12 masterbase3 172.25.78.13 slavebase4 172.25.78.14 slave1.配置一主二从环境在base2上[root@ ...

  7. vue本地静态图片的路径问题解决方案

    不少人在vue的开发中遇到这样一个问题: img的src属性绑定url变量,然而图片加载失败. 大部分的情况中,是开发者使用了错误的写法,例如: <img src="{{ imgUrl ...

  8. 学Redis这篇就够了!

    学Redis这篇就够了!   作者:王爷科技 https://www.toutiao.com/i6713520017595433485 Redis 简介 & 优势 Redis 数据类型 发布订 ...

  9. 避免因为Arcgis Server服务设置不当导致Oracle Process溢出的方法

    我之前写过一篇文章<arcsoc进程无限增长导致oracle processes溢出>(见链接:https://www.cnblogs.com/6yuhang/p/9379086.html ...

  10. samba服务器安装测试

    samba服务器安装 1.简介 作用:samba文件服务器允许通过网络跨步同的操作系统进行文件共享. 2.安装samba服务器 $ sudo apt-get update $ sudo apt-get ...