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>
vue 实现滚动到页面底部开始加载更多的更多相关文章
- Jquery鼠标滚动到页面底部自动加载更多内容,使用分页
index.php代码 [html] view plaincopy <!DOCTYPE html PUBLIC ;} .single_item{padding: ...
- Jquery+php鼠标滚动到页面底部自动加载更多内容,使用分页
1.index.php <style type="text/css"> #container{margin:10px auto;width: 660px; border ...
- 当滚动条滚动到页面底部自动加载增加内容的js代码
这篇文章主要介绍了如何使用javscript实现滚动条滚动到页面底部自动加载增加页面内容,需要的朋友可以参考下..1,注册页面滚动事件,window.onscroll = function(){ }; ...
- vue使用H5实现滚动到页面底部时加载数据
使用原生vue实现瀑布流,发现无法实现小程序那种滚动到地步触发加载效果,只能自己研究了 实现效果: 实现代码: 首先添加监听滚动事件 mounted() { window.addEventListen ...
- jQuery+ajax实现滚动到页面底部自动加载图文列表效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS 判断滚动底部并加载更多效果。。。。。。。。。
JS 判断滚动底部并加载更多效果......... <html lang="zh-cn"> <head> <meta http-equiv=" ...
- UWP-ListView到底部自动加载更多数据
原文:UWP-ListView到底部自动加载更多数据 ListView绑定的数据当需要“更多”时自动加载 ListView划到底部后,绑定的ObservableCollection列表数据需要加载的更 ...
- RecycleView 滑动到底部,加载更多
android.support.v7 包提供了一个新的组件:RecycleView,用以提供一个灵活的列表试图.显示大型数据集,它支持局部刷新.显示动画等功能,可以用来取代ListView与GridV ...
- H5页面下拉加载更多(实用版)
近期在做一个H5网站,需要下拉加载更多产品列表的功能.百度搜索了好久,什么说法都有,什么插件都有. 醉了.基本上每一个能直接拿来用的. 最后发现: 1.dropload.js 插件 还可以,但是 ...
随机推荐
- JDOJ 1606 数字三角形
JDOJ 1606: 数字三角形 JDOJ传送门 Description 输入n,输出n的数字三角形 见样例 Input n Output n的数字三角形 Sample Input 4 Sample ...
- 28 让树莓派开机“说”出自己的IP地址
http://shumeipai.nxez.com/2019/02/02/analogue-audio-redux.html 树莓派音频口底噪消除的方法
- Super Fish
Super fish is a common fun and leisure game. It's a game that tests your intelligence and memory ...
- Jupyter-notebook安装问题及解决
两种方式: 1.pip install jupyter notebook 2.安装Anaconda 1.pip安装 通过命令行pip,要注意是在哪个虚拟环境,安装好后jupyter notebook所 ...
- Debian9下安装Python3 pip
Debian9下安装Python3 pip 使用apt-get安装Python3-pip包 apt-get install python3-pip
- priority_queue(优先队列)使用方法
priority_queue默认是一个大根堆: 并且出队方式与普通队列queue的front不一样,是top . 如果想用小根堆,可以修改定义时的参数: priority_queue<int,v ...
- 洛谷P3084 [USACO13OPEN]照片
题目 \(DP\) 设状态\(dp[i]\)为\(i\)位置放了斑点牛,前\(i\)个位置能得到的最多的牛. 有方程\(dp[i]=max(dp[j]+1,dp[i])\),而我们并不知道什么\(j\ ...
- C语言实现linux之who功能
/* who_test.c */ #include<stdio.h> #include<string.h> #include<getopt.h> #include& ...
- nginx 配置参数优化
nginx作为高性能web服务器,即使不特意调整配置参数也可以处理大量的并发请求.以下的配置参数是借鉴网上的一些调优参数,仅作为参考,不见得适于你的线上业务. worker进程 worker_proc ...
- centos7 df 命令卡死
登录服务器想查看磁盘使用情况,使用了df,但卡住半天没有响应. 运行strace df -h,发现最后卡在了 stat("/proc/sys/fs/binfmt_misc", 无法 ...