vue底部加载更多的实例代码
程序员文章站
2022-03-21 20:34:06
要实现的效果如下:
要实现的效果如下:
<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实现的事件绑定功能基本示例