上公式:

元素距离顶部高度(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. 数论---GCD(最大公约数)+LCM(最小公倍数)

    #include<bits/stdc++.h> #define ll long long using namespace std; /* ll gcd(ll a, ll b) {//非递归 ...

  2. Disruptor中shutdown方法失效,及产生的不确定性源码分析

    版权声明:原创作品,谢绝转载!否则将追究法律责任. Disruptor框架是一个优秀的并发框架,利用RingBuffer中的预分配内存实现内存的可重复利用,降低了GC的频率. 具体关于Disrupto ...

  3. 使用secureCRT进行linux和windows之间的nginx文件夹传输

    1.首先进入secureCRT软件,新建一个链接,我现在已经创建好了进入这个页面: 注意:新建链接时里面的hostname是你linux的ip地址,使用ifconfig就可以看到 2.在secureC ...

  4. asp.netcore2.1 IIS部署发布

    下载SDK .net core2.1 SDK下载地址:https://www.microsoft.com/net/download 创建Demo项目 打开 powershell 新建项目 dotnet ...

  5. FreeSql (十九)多表查询

    多表查询,常用的有联表 LeftJoin/InnerJoin/RightJoin ,这三个方法在上篇文章已经介绍过. 除了联表,还有子查询 Where Exists,和 Select 子表: IFre ...

  6. CNCF 宣布成立应用交付领域小组,正式开启云原生应用时代

    作者|赵钰莹 作为云原生领域的顶级开源社区, Cloud Native Computing Foundation (云原生基金会,以下简称 CNCF)近日宣布成立 Application Delive ...

  7. .Net基础篇_学习笔记_第五天_流程控制while循环003

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. NetCore+AutoMapper多个对象映射到一个Dto对象

    目录 一. 定义源映射类和被映射类DTO二.注入AutoMapper三.配置映射四.写测试 一.定义源映射对象 为了体现AutoMapper映射特性,在SocialAttribute中的Name属性没 ...

  9. 微信支付中分账功能 填坑指南V1

    公司是做电商的,近期开发了一款小程序,准备线上线下同步销售玩具.这里就涉及到微信支付的功能,网上有很多教程,官方也有文档和Demo,因此微信支付还是比较容易实现的. 由于我们公司是和其他公司合作运营的 ...

  10. charles 启用/禁用断点

    本文参考:charles 启用/禁用断点 1.3. enable/disable breakpoints 和 2.3 breakpoints settings 断点设置是常用的了,没啥好说的了,可以设 ...