≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

vue底部加载更多的实例代码

人气:442 时间:2019-04-11

这篇文章主要为大家详细介绍了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底部加载更多的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对四海网网站的支持!

本文来自:http://www.q1010.com/184/5318-0.html

注:关于vue底部加载更多的实例代码的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • Vue下路由History模式打包后页面空白的解决方法
  • 浅谈vue首屏加载优化
  • vue项目打包后打开页面空白解决办法
  • vue+springmvc导出excel数据的实现代码
  • vue ssr 指南详读
  • vue awesome swiper异步加载数据出现的bug问题
  • 分析Vue SPA项目优化小记
  • Vue实现todolist删除功能
  • 分析关于vue-area-linkage走过的坑
  • vue如何引入sass全局变量
上一篇:Vue项目中添加锁屏功能实现思路
下一篇:Vue下路由History模式打包后页面空白的解决方法
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。