项目作者: RadKod

项目描述 :
Vue component render when visible. Uses Intersection Observer API.
高级语言: JavaScript
项目地址: git://github.com/RadKod/v-lazy-component.git
创建时间: 2020-05-06T19:44:31Z
项目社区:https://github.com/RadKod/v-lazy-component

开源协议:MIT License

下载


npm version
npm downloads
changelog
License


v-lazy-component


Vue component render when visible 👁️⚡️






GitHub stars


Website


Sponsorship 💖

GitHub

Buy me a coffee

Features

  • ⚡️ Lightweight
  • 🎨 Interactive
  • 👶🏻 Easy implementation
  • 📦 Vue2 & Vue3 support

Getting Started

Try it Online ⚡️

DEMO

Installation

  1. yarn add v-lazy-component # or npm i v-lazy-component

Vue3

Global Register

  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import LazyComponent from 'v-lazy-component'
  4. const app = createApp(App)
  5. app.use(LazyComponent)
  6. app.mount('#app')

Local Register

  1. <script setup>
  2. import LazyComponent from 'v-lazy-component'
  3. </script>

Via CDN

  1. <script src="https://unpkg.com/vue@3"></script>
  2. <script src="https://unpkg.com/v-lazy-component"></script>
  3. <script>
  4. const app = Vue.createApp({})
  5. app.use(LazyComponent)
  6. app.mount('#app')
  7. </script>

Vue2

Global Register

  1. import Vue from "vue";
  2. import LazyComponent from "v-lazy-component/vue2";
  3. Vue.use(LazyComponent);

Local Register

  1. import LazyComponent from "v-lazy-component/vue2";
  2. export default {
  3. components: {
  4. LazyComponent
  5. }
  6. }

Via CDN

  1. <script src="https://unpkg.com/vue@2"></script>
  2. <script src="https://unpkg.com/v-lazy-component/vue2"></script>
  3. <script>
  4. new Vue({
  5. el: "#app"
  6. });
  7. Vue.use(LazyComponent);
  8. </script>

Usage

  1. <lazy-component wrapper-tag="section" @intersected="optionalDispatch">
  2. <your-component></your-component>
  3. <!-- Optional -->
  4. <template #placeholder>
  5. <span>Loading...</span>
  6. </template>
  7. </lazy-component>

idle variant

  1. ....
  2. <button @click="isIntersected = true">Click for Render</button>
  3. <lazy-component :is-intersected="isIntersected" idle>
  4. <your-component></your-component>
  5. <!-- Optional -->
  6. <template #placeholder>
  7. <span>Loading...</span>
  8. </template>
  9. </lazy-component>

Props

Name Description Type Default
wrapper-tag Html tag of lazy component String div
is-intersected Do not wait observe, Force render Boolean false
idle Do not use observer, wait is-intersected prop changes for render Boolean false
root-margin Intersection Observer API doc String 0px 0px 0px 0px
threshold Intersection Observer API doc Number, Array 0

See Intersection Observer API doc

Slots

placeholder Content that is loaded as a placeholder until it comes into view

Events

intersected dispatch event when visible

CSS Selectors

  1. .v-lazy-component.v-lazy-component--loading {
  2. filter: blur(20px);
  3. }
  4. .v-lazy-component.v-lazy-component--loaded {
  5. filter: blur(0);
  6. transition: filter 1s;
  7. }

Development

Vue3

  1. yarn build:vue3 # build for vue3
  1. # Serve
  2. cd dev/vue3
  3. yarn install
  4. yarn serve

Vue2

  1. yarn build:vue2 # build for vue2
  1. # Serve
  2. cd dev/vue2
  3. yarn install
  4. yarn serve

Vue 2&3

  1. yarn build # build for vue2 and vue3

Sponsorship

You can sponsor me for the continuity of my projects:









License

MIT License

Copyright (c) selimdoyranli selimdoyranli@gmail.com