{"id":13424217,"url":"https://github.com/hilongjw/vue-lazyload","last_synced_at":"2025-05-14T12:06:22.396Z","repository":{"id":39135842,"uuid":"49623638","full_name":"hilongjw/vue-lazyload","owner":"hilongjw","description":"A Vue.js plugin for lazyload your Image or Component in your application.","archived":false,"fork":false,"pushed_at":"2023-06-30T06:57:44.000Z","size":4856,"stargazers_count":8038,"open_issues_count":197,"forks_count":868,"subscribers_count":92,"default_branch":"master","last_synced_at":"2025-05-07T11:41:51.582Z","etag":null,"topics":["lazyload","vue","vue-lazyload"],"latest_commit_sha":null,"homepage":"http://hilongjw.github.io/vue-lazyload/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hilongjw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-01-14T04:54:04.000Z","updated_at":"2025-04-30T10:44:47.000Z","dependencies_parsed_at":"2024-01-18T03:48:10.892Z","dependency_job_id":null,"html_url":"https://github.com/hilongjw/vue-lazyload","commit_stats":{"total_commits":252,"total_committers":60,"mean_commits":4.2,"dds":0.6150793650793651,"last_synced_commit":"6c4c3e5108bf89b5c71842ab8fb191fd0f06aa4e"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hilongjw%2Fvue-lazyload","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hilongjw%2Fvue-lazyload/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hilongjw%2Fvue-lazyload/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hilongjw%2Fvue-lazyload/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hilongjw","download_url":"https://codeload.github.com/hilongjw/vue-lazyload/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253656410,"owners_count":21943081,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lazyload","vue","vue-lazyload"],"created_at":"2024-07-31T00:00:50.296Z","updated_at":"2025-05-14T12:06:22.353Z","avatar_url":"https://github.com/hilongjw.png","language":"JavaScript","funding_links":[],"categories":["Awesome Vue.js [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome)","JavaScript","实用库","UI实用程序","Components \u0026 Libraries","UI Utilities","Awesome Vue.js","UI Utilities [🔝](#readme)"],"sub_categories":["Libraries \u0026 Plugins","延迟加载","UI Utilities","Lazy Load"],"readme":"# Vue-Lazyload\n\n[![Build Status](https://img.shields.io/circleci/project/hilongjw/vue-lazyload/master.svg?style=flat-square)](https://circleci.com/gh/hilongjw/vue-lazyload)\n[![npm version](https://img.shields.io/npm/v/vue-lazyload.svg?style=flat-square)](http://badge.fury.io/js/vue-lazyload)\n[![npm downloads](https://img.shields.io/npm/dm/vue-lazyload.svg?style=flat-square)](http://badge.fury.io/js/vue-lazyload)\n[![npm license](https://img.shields.io/npm/l/vue-lazyload.svg?style=flat-square)](http://badge.fury.io/js/vue-lazyload)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)\n[![CDNJS version](https://img.shields.io/cdnjs/v/vue-lazyload.svg)](https://cdnjs.com/libraries/vue-lazyload)\n\nVue module for lazyloading images in your applications. Some of goals of this project worth noting include:\n\n* Be lightweight, powerful and easy to use\n* Work on any image type\n* Add loading class while image is loading\n* Supports both of Vue 1.0 and Vue 2.0\n\n# For Vue 3\nPlease use vue-lazyload@3.x, see [here](https://github.com/hilongjw/vue-lazyload/tree/next)\n\n# Table of Contents\n\n* [___Demo___](#demo)\n* [___Requirements___](#requirements)\n* [___Installation___](#installation)\n* [___Usage___](#usage)\n * [___Constructor Options___](#constructor-options)\n * [___Implementation___](#implementation)\n    * [___Basic___](#basic)\n    * [___Css state___](#css-state)\n* [___Methods___](#methods)\n  * [__Event hook__](#event-hook)\n  * [__LazyLoadHandler__](#lazyloadhandler)\n  * [__Performance__](#performance)\n* [___Authors \u0026\u0026 Contributors___](#authors-\u0026\u0026-Contributors)\n* [___License___](#license)\n\n\n# Demo\n\n[___Demo___](http://hilongjw.github.io/vue-lazyload/)\n\n# Requirements\n\n- [Vue.js](https://github.com/vuejs/vue) `1.x` or `2.x`\n\n\n# Installation\n\n## npm\n\n```bash\n\n$ npm i vue-lazyload -S\n\n```\n\n## yarn\n\n```bash\n\n$ yarn add vue-lazyload\n\n```\n\n## CDN\n\nCDN: [https://unpkg.com/vue-lazyload/vue-lazyload.js](https://unpkg.com/vue-lazyload/vue-lazyload.js)\n\n```html\n\u003cscript src=\"https://unpkg.com/vue-lazyload/vue-lazyload.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  Vue.use(VueLazyload)\n  ...\n\u003c/script\u003e\n\n```\n\n# Usage\n\nmain.js:\n\n```javascript\n\nimport Vue from 'vue'\nimport App from './App.vue'\nimport VueLazyload from 'vue-lazyload'\n\nVue.use(VueLazyload)\n\n// or with options\nconst loadimage = require('./assets/loading.gif')\nconst errorimage = require('./assets/error.gif')\n\nVue.use(VueLazyload, {\n  preLoad: 1.3,\n  error: errorimage,\n  loading: loadimage,\n  attempt: 1\n})\n\nnew Vue({\n  el: 'body',\n  components: {\n    App\n  }\n})\n```\n\ntemplate:\n\n```html\n\u003cul\u003e\n  \u003cli v-for=\"img in list\"\u003e\n    \u003cimg v-lazy=\"img.src\" \u003e\n  \u003c/li\u003e\n\u003c/ul\u003e\n```\n\nuse `v-lazy-container` work with raw HTML\n\n```html\n\u003cdiv v-lazy-container=\"{ selector: 'img' }\"\u003e\n  \u003cimg data-src=\"//domain.com/img1.jpg\"\u003e\n  \u003cimg data-src=\"//domain.com/img2.jpg\"\u003e\n  \u003cimg data-src=\"//domain.com/img3.jpg\"\u003e  \n\u003c/div\u003e\n```\n\ncustom `error` and `loading` placeholder image\n\n```html\n\u003cdiv v-lazy-container=\"{ selector: 'img', error: 'xxx.jpg', loading: 'xxx.jpg' }\"\u003e\n  \u003cimg data-src=\"//domain.com/img1.jpg\"\u003e\n  \u003cimg data-src=\"//domain.com/img2.jpg\"\u003e\n  \u003cimg data-src=\"//domain.com/img3.jpg\"\u003e  \n\u003c/div\u003e\n```\n\n```html\n\u003cdiv v-lazy-container=\"{ selector: 'img' }\"\u003e\n  \u003cimg data-src=\"//domain.com/img1.jpg\" data-error=\"xxx.jpg\"\u003e\n  \u003cimg data-src=\"//domain.com/img2.jpg\" data-loading=\"xxx.jpg\"\u003e\n  \u003cimg data-src=\"//domain.com/img3.jpg\"\u003e  \n\u003c/div\u003e\n```\n\n## Constructor Options\n\n|key|description|default|options|\n|:---|---|---|---|\n| `preLoad`|proportion of pre-loading height|`1.3`|`Number`|\n|`error`|src of the image upon load fail|`'data-src'`|`String`\n|`loading`|src of the image while loading|`'data-src'`|`String`|\n|`attempt`|attempts count|`3`|`Number`|\n|`listenEvents`|events that you want vue listen for|`['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend', 'touchmove']`| [Desired Listen Events](#desired-listen-events) |\n|`adapter`| dynamically modify the attribute of element |`{ }`| [Element Adapter](#element-adapter) |\n|`filter`| the image's listener filter |`{ }`| [Image listener filter](#image-listener-filter) |\n|`lazyComponent`| lazyload component | `false` | [Lazy Component](#lazy-component)\n| `dispatchEvent`|trigger the dom event|`false`|`Boolean`|\n| `throttleWait`|throttle wait|`200`|`Number`|\n| `observer`|use IntersectionObserver|`false`|`Boolean`|\n| `observerOptions`|IntersectionObserver options|{ rootMargin: '0px', threshold: 0.1 }|[IntersectionObserver](#intersectionobserver)|\n| `silent`|do not print debug info|`true`|`Boolean`|\n\n### Desired Listen Events\n\nYou can configure which events you want vue-lazyload by passing in an array\nof listener names.\n\n```javascript\nVue.use(VueLazyload, {\n  preLoad: 1.3,\n  error: 'dist/error.png',\n  loading: 'dist/loading.gif',\n  attempt: 1,\n  // the default is ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend']\n  listenEvents: [ 'scroll' ]\n})\n```\n\nThis is useful if you are having trouble with this plugin resetting itself to loading\nwhen you have certain animations and transitions taking place\n\n\n### Image listener filter\n\ndynamically modify the src of image\n\n```javascript\nVue.use(vueLazy, {\n    filter: {\n      progressive (listener, options) {\n          const isCDN = /qiniudn.com/\n          if (isCDN.test(listener.src)) {\n              listener.el.setAttribute('lazy-progressive', 'true')\n              listener.loading = listener.src + '?imageView2/1/w/10/h/10'\n          }\n      },\n      webp (listener, options) {\n          if (!options.supportWebp) return\n          const isCDN = /qiniudn.com/\n          if (isCDN.test(listener.src)) {\n              listener.src += '?imageView2/2/format/webp'\n          }\n      }\n    }\n})\n```\n\n\n### Element Adapter\n\n```javascript\nVue.use(vueLazy, {\n    adapter: {\n        loaded ({ bindType, el, naturalHeight, naturalWidth, $parent, src, loading, error, Init }) {\n            // do something here\n            // example for call LoadedHandler\n            LoadedHandler(el)\n        },\n        loading (listender, Init) {\n            console.log('loading')\n        },\n        error (listender, Init) {\n            console.log('error')\n        }\n    }\n})\n```\n\n### IntersectionObserver\n\nuse [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) to to improve performance of a large number of nodes.\n\n```javascript\nVue.use(vueLazy, {\n  // set observer to true\n  observer: true,\n\n  // optional\n  observerOptions: {\n    rootMargin: '0px',\n    threshold: 0.1\n  }\n})\n```\n\n\n### Lazy Component\n```javascript\nVue.use(VueLazyload, {\n  lazyComponent: true\n});\n```\n\n```html\n\u003clazy-component @show=\"handler\"\u003e\n  \u003cimg class=\"mini-cover\" :src=\"img.src\" width=\"100%\" height=\"400\"\u003e\n\u003c/lazy-component\u003e\n\n\u003cscript\u003e\n  {\n    ...\n    methods: {\n      handler (component) {\n        console.log('this component is showing')\n      }\n    }\n\n  }\n\u003c/script\u003e\n```\nUse in list\n```html\n\u003clazy-component v-for=\"(item, index) in list\" :key=\"item.src\" \u003e\n  \u003cimg class=\"mini-cover\" :src=\"item.src\" width=\"100%\" height=\"400\"\u003e\n\u003c/lazy-component\u003e\n```\n\n\n## Implementation\n\n### Basic\n\nvue-lazyload will set this img element's `src` with `imgUrl` string\n\n```html\n\u003cscript\u003e\nexport default {\n  data () {\n    return {\n      imgObj: {\n        src: 'http://xx.com/logo.png',\n        error: 'http://xx.com/error.png',\n        loading: 'http://xx.com/loading-spin.svg'\n      },\n      imgUrl: 'http://xx.com/logo.png' // String\n    }\n  }\n}\n\u003c/script\u003e\n\n\u003ctemplate\u003e\n  \u003cdiv ref=\"container\"\u003e\n     \u003cimg v-lazy=\"imgUrl\"/\u003e\n     \u003cdiv v-lazy:background-image=\"imgUrl\"\u003e\u003c/div\u003e\n\n     \u003c!-- with customer error and loading --\u003e\n     \u003cimg v-lazy=\"imgObj\"/\u003e\n     \u003cdiv v-lazy:background-image=\"imgObj\"\u003e\u003c/div\u003e\n\n     \u003c!-- Customer scrollable element --\u003e\n     \u003cimg v-lazy.container =\"imgUrl\"/\u003e\n     \u003cdiv v-lazy:background-image.container=\"img\"\u003e\u003c/div\u003e\n\n    \u003c!-- srcset --\u003e\n    \u003cimg v-lazy=\"'img.400px.jpg'\" data-srcset=\"img.400px.jpg 400w, img.800px.jpg 800w, img.1200px.jpg 1200w\"\u003e\n    \u003cimg v-lazy=\"imgUrl\" :data-srcset=\"imgUrl' + '?size=400 400w, ' + imgUrl + ' ?size=800 800w, ' + imgUrl +'/1200.jpg 1200w'\" /\u003e\n  \u003c/div\u003e\n\u003c/template\u003e\n```\n\n### CSS state\n\nThere are three states while img loading\n\n`loading`  `loaded`  `error`\n\n```html\n\u003cimg src=\"imgUrl\" lazy=\"loading\"\u003e\n\u003cimg src=\"imgUrl\" lazy=\"loaded\"\u003e\n\u003cimg src=\"imgUrl\" lazy=\"error\"\u003e\n```\n\n```html\n\u003cstyle\u003e\n  img[lazy=loading] {\n    /*your style here*/\n  }\n  img[lazy=error] {\n    /*your style here*/\n  }\n  img[lazy=loaded] {\n    /*your style here*/\n  }\n  /*\n  or background-image\n  */\n  .yourclass[lazy=loading] {\n    /*your style here*/\n  }\n  .yourclass[lazy=error] {\n    /*your style here*/\n  }\n  .yourclass[lazy=loaded] {\n    /*your style here*/\n  }\n\u003c/style\u003e\n```\n\n## Methods\n\n### Event Hook\n\n`vm.$Lazyload.$on(event, callback)`\n`vm.$Lazyload.$off(event, callback)`\n`vm.$Lazyload.$once(event, callback)`\n\n- `$on` Listen for a custom events `loading`, `loaded`, `error`\n- `$once` Listen for a custom event, but only once. The listener will be removed once it triggers for the first time.\n- `$off` Remove event listener(s).\n\n#### `vm.$Lazyload.$on`\n\n#### Arguments:\n\n * `{string} event`\n * `{Function} callback`\n\n#### Example\n\n```javascript\nvm.$Lazyload.$on('loaded', function ({ bindType, el, naturalHeight, naturalWidth, $parent, src, loading, error }, formCache) {\n  console.log(el, src)\n})\n```\n\n#### `vm.$Lazyload.$once`\n\n#### Arguments:\n\n * `{string} event`\n * `{Function} callback`\n\n#### Example\n\n```javascript\nvm.$Lazyload.$once('loaded', function ({ el, src }) {\n  console.log(el, src)\n})\n```\n\n#### `vm.$Lazyload.$off`\n\nIf only the event is provided, remove all listeners for that event\n\n#### Arguments:\n\n * `{string} event`\n * `{Function} callback`\n\n#### Example\n\n```javascript\nfunction handler ({ el, src }, formCache) {\n  console.log(el, src)\n}\nvm.$Lazyload.$on('loaded', handler)\nvm.$Lazyload.$off('loaded', handler)\nvm.$Lazyload.$off('loaded')\n```\n\n### LazyLoadHandler\n\n`vm.$Lazyload.lazyLoadHandler`\n\nManually trigger lazy loading position calculation\n\n#### Example\n\n```javascript\n\nthis.$Lazyload.lazyLoadHandler()\n\n```\n\n### Performance\n\n```javascript\nthis.$Lazyload.$on('loaded', function (listener) {\n  console.table(this.$Lazyload.performance())\n})\n```\n\n![performance-demo](http://ww1.sinaimg.cn/large/69402bf8gw1fbo62ocvlaj213k09w78w.jpg)\n\n### Dynamic switching pictures\n\n```vue\n \u003cimg v-lazy=\"lazyImg\" :key=\"lazyImg.src\"\u003e\n```\n\n\n# Authors \u0026\u0026 Contributors\n\n- [hilongjw](https://github.com/hilongjw)\n- [imcvampire](https://github.com/imcvampire)\n- [darrynten](https://github.com/darrynten)\n- [biluochun](https://github.com/biluochun)\n- [whwnow](https://github.com/whwnow)\n- [Leopoldthecoder](https://github.com/Leopoldthecoder)\n- [michalbcz](https://github.com/michalbcz)\n- [blue0728](https://github.com/blue0728)\n- [JounQin](https://github.com/JounQin)\n- [llissery](https://github.com/llissery)\n- [mega667](https://github.com/mega667)\n- [RobinCK](https://github.com/RobinCK)\n- [GallenHu](https://github.com/GallenHu)\n\n# License\n\n[The MIT License](http://opensource.org/licenses/MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhilongjw%2Fvue-lazyload","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhilongjw%2Fvue-lazyload","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhilongjw%2Fvue-lazyload/lists"}