Vue3 Quasar簡易Lazy Loading

文章展示了如何在Vue应用中使用混入(mixin)和IntersectionObserverAPI创建一个懒加载功能。它定义了一个名为`lazyLoad`的mixin,该mixin包含一个`inView`数组和`onIntersection`方法来处理视口内的元素。在模板中,使用v-intersection指令结合quasar指令系统来触发懒加载效果。
摘要由CSDN通过智能技术生成

直接上代碼

src/mixins/lazyLoad.js

import { reactive } from 'vue'

export default function () {
  const inView = reactive([])
  const onIntersection = (entry) => {
    if (entry.isIntersecting === true) {
      add(`${entry.target.dataset.id}`)
    }
  }

  function add(i) {
    inView.push(i)
    inView.sort(sortAtoi)
  }

  function sortAtoi(a, b) {
    return Number(a) - Number(b)
  }
	return {
		inView,
		onIntersection
	}
}

調用方法:

<template>
    <card
      v-for="(hotel, index) in hotelList"
      :key="index"
      v-intersection="onIntersection"
      :in-view="inView.includes(`hotelCard-${index - 1}`)"
     >
         <!-- 代碼省略 -->
    </card>
</template>

<script setup>
import LazyLoad from 'src/mixins/lazyLoad'


const { onIntersection, inView } = LazyLoad()

</script>

<style lang="scss" scoped>
/*--lazy load --*/
.in-view-item {
  transition: all 0.3s;
  display: block;
}

.in-view-enter,
.in-view-leave-to {
  opacity: 0;
  transform: translateX(-30px);
}

.in-view-leave-active {
  position: absolute;
}
</style>

這裡的v-intersection是利用qusasr 套件的Directive​​​​​​​ 

以上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值