上公式:

元素距离顶部高度(elOffsetTop) >= dom滚动高度(docScrollTop)

并且元素距离顶部高度(elOffsetTop) < (dom滚动高度 + 视窗高度)

上代码:

一个多图表 懒加载 例子

<template>
<div :id="boxId" style="height: 450px">
<div v-loading="chartLoading">
<defect-flight-pattern-chart
:chart-data="chartData"
:chart-id="chartId">
</defect-flight-pattern-chart>
</div>
</div>
</template> <script>
import DefectFlightPatternChart from '~/components/metrics/defect-flight-pattern-chart'
export default {
components: {
DefectFlightPatternChart
},
props: {
projectUuid: { type: String, default: '' },
chartIndex: { type: Number, default: 0 }
},
data () {
return {
chartData: {},
chartLoading: false,
isLoaded: false,
boxId: 'dashboard-chart-box-',
chartId: 'dashboard-chart-'
}
},
mounted () {
this.chartId = this.chartId + this.chartIndex + Math.floor(Math.random() * 1000)
this.boxId = this.chartId + '-box'
this.$nextTick(() => {
this.scroll()
window.addEventListener('scroll', this.scroll)
})
},
destroyed () {
window.removeEventListener('scroll', this.scroll, false)
},
methods: {
async getChartData () {
try {
this.isLoaded = true
this.chartLoading = true
const { data } = await this.$axios.get(`/api/v1/projects/${this.projectUuid}/issues/trend`)
this.chartData = data
this.chartLoading = false
} catch (e) {
console.log(e)
}
},
async scroll () {
const elOffsetTop = document.getElementById(this.boxId).offsetTop
const docScrollTop = document.documentElement.scrollTop - 230
if (elOffsetTop >= docScrollTop && elOffsetTop < (docScrollTop + window.innerHeight) && !this.isLoaded) {
await this.getChartData()
}
}
}
}
</script>

觉得有帮助的小伙伴点个赞支持下~

觉得有帮助的小伙伴点个赞~

Vue-懒加载(判断元素是否在可视区域内)的更多相关文章

  1. 如何判断元素是否在可视区域内--getBoundingClientRect

    介绍 Element.getBoundingClientRect()方法返回元素的大小及其相对于视口的位置. 根据MDN文档 getBoundingClientRect 方法返回的是一个DOMRect ...

  2. jq、js判断元素是否在可视区域内

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> ...

  3. 关于Vue懒加载问题

    有关Vue懒加载其实并不是想象的那么难和复杂: 首先引入 import  VueLazyLoad from 'vue-lazyload'; 其次是使用 Vue.use(VueLazyLoad,{ er ...

  4. vue懒加载

    vue懒加载(白屏或者加载慢的解决方法) 懒加载:也叫延迟加载,即在需要的时候进行加载,随用随载. 为什么需要懒加载? 像vue这种单页面应用,如果没有应用懒加载,运用webpack打包后的文件将会异 ...

  5. 如何判断元素是否在可视区域ViewPort

    个性签名: 生如夏花,逝如冬雪:人生如此,何悔何怨. 前言: 经常需要计算元素的大小或者所在页面的位置,offsetWidth,clientWidth,scrollWidth,scrollTop这几个 ...

  6. vue懒加载 && 浏览器高度

    当我们进入首页时,可能有很多条目需要显示,但是如果条目太多,我们全部将之显示出来就会造成性能的消耗,比如,我在第一条就找到了需要的或者我就看前面两条我就不想看后面的了,所以,这时候如果使用全部加载的方 ...

  7. vue懒加载 路由 router 的编写(resolve)

    如果用import引入的话,当项目打包时路由里的所有component都会打包在一个js中,造成进入首页时,需要加载的内容过多,时间相对比较长.当你用require这种方式引入的时候,会将你的comp ...

  8. vue-lazyload 的vue 懒加载的使用

    vue-lazyload vue 图片懒加载的使用 下载 vue-lazyload npm i vue-lazyload -S 使用 vue-lazyload 在 src 下面的 main.js 的文 ...

  9. 使用Webpack的代码分离实现Vue懒加载(译文)

    当一个Vue的项目体积变得十分庞大的时候,使用Webpack的代码分离功能将Vue Components,routes或Vuex的代码进行分离并按需加载,会极大的提高App的首屏加载速度. 在Vue的 ...

随机推荐

  1. ELKBR部署检测项目日志

    ELK filebeat:具有日志收集功能,相比logstash,+filebeat更轻量,占用资源更少,适合客户端使用. redis消息队列选型:Redis 服务器通常都是用作 NoSQL 数据库, ...

  2. 教程 —— 如何在自己的应用集成superset

    Superset 是apache的一个孵化项目,定位为一款现代的,准商用BI系统 superset Apache Superset (incubating) is a modern, enterpri ...

  3. android 和 webService交互

    webService 很久不用了,第一次使用还是13年, 早已忘记怎么搞了.今天看了篇博文,写了个demo .记录下吧! 首先要下载skoap2  .... xxx.jar  ,我用的是最新的3.6. ...

  4. Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面

    Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面 在<Spring Boot(一):快速开始>中介绍了如何使用 Spring Boot 构建一个工程,并且提 ...

  5. HBase shell scan 模糊查询

    0.进入hbase shell ./hbase shell help help "get" #查看单独的某个命令的帮助 1. 一般命令 status 查看状态 version 查看 ...

  6. eclipse下mybatis-generator-config插件

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

  7. 关于spring boot多张表建立外健的讨论

    现在有四张表:student(学生表).blogs(博客表).comment(评论表).reply(回复表) 现在说一下这四张表: student(学生表):学生的信息记录表 blogs(博客表):学 ...

  8. AD 域服务简介(二)- Java 获取 AD 域用户

    博客地址:http://www.moonxy.com 关于AD 域服务器搭建及其使用,请参阅:AD 域服务简介(一) - 基于 LDAP 的 AD 域服务器搭建及其使用 一.前言 先简单简单回顾上一篇 ...

  9. 04:videoToolbox:硬编码

    一:前言: 1:apple develop 来源 2:VideoToolbox(视频工具箱)工作流程 2.1:创建 一个压缩会话. 2.2:添加会话属性. 2.3:编码视频帧.接受视频编码回调. 2. ...

  10. Hadoop 之 HDFS基本概念

    1.HDFS的基本概念 答:块(Block).NameNode.DataNode.HDFS的文件被分成块进行存储,默认块的大小为64M,所以说块是文件存储和处理的逻辑单元.NameNode是管理节点, ...