主頁 >
其他 > Vue-懶加載(判斷元素是否在可視區域內)
Vue-懶加載(判斷元素是否在可視區域內)
2020-09-17 07:05:41 其他
上公式:
元素距離頂部高度(elOffsetTop) >= dom滾動高度(docScrollTop)
并且元素距離頂部高度(elOffsetTop) < (dom滾動高度 + 視窗高度)
上代碼:
一個多圖表 懶加載 例子
<template>
<div :id="boxId" style="height: 450px">
<div v-loading="chartLoading">
<defect-flight-pattern-chart
:chart-data=https://www.cnblogs.com/zheroXH/p/"chartData"
:chart-id="chartId">
<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 = https://www.cnblogs.com/zheroXH/p/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>
覺得有幫助的小伙伴點個贊支持下~
覺得有幫助的小伙伴點個贊~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/63552.html
標籤:其他
上一篇:【隨筆】跳出程式員思維看世界
下一篇:為什么人到中年就危機了呢?