zoukankan      html  css  js  c++  java
  • Vue-懒加载(判断元素是否在可视区域内)

    上公式:

    元素距离顶部高度(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>
    
    

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

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

  • 相关阅读:
    Excel公式进阶教程
    Excel公式初级教程
    一阶常系数线性差分方程通解求法
    博客园为代码块添加复制功能
    MFC分割窗口(CSplitterWnd)与选项卡视图(CTabView)的混合使用
    自制软件改变窗口大小
    conda的基本使用
    安装GPU驱动
    更换conda源、安装pytorch
    服务器安装anaconda
  • 原文地址:https://www.cnblogs.com/zheroXH/p/11678031.html
Copyright © 2011-2022 走看看