a vuepress plugin to better supporting image lazy loading
The plugin will preferentially use native image lazy-loading, if the browser does not support it, it will be implemented through IntersectionObserver
base on markdown-it-img-lazy and markdown-it-imsize
yarn add vuepress-plugin-img-lazy
# or
npm i vuepress-plugin-img-lazy
module.exports = {
plugins: [
'img-lazy'
]
}
![img](/img.jpg)
# or
![img](/img.jpg =500x300) <!-- better -->
- registered as global components
// enhanceAppFile.js
import ImgLazy from 'vuepress-plugin-img-lazy/ImgLazy'
export default ({ Vue }) => {
Vue.component(ImgLazy.name, ImgLazy)
}
- or registered as components
import ImgLazy from 'vuepress-plugin-img-lazy/ImgLazy'
export default {
components: { ImgLazy }
}
- use
<template>
<img-lazy src="/img.jpg" />
</template>
If you registered as global components, you can use it directly in the markdown
file
<img-lazy src="/img.jpg" />
- Type:
Boolean
- Default:
true
- Required:
false
Use the native image lazy-loading for the web
- Type:
string
- Default:
lazy
- Required:
false
Default class name for image
- Type:
String
- Default:
200px
- Required:
false
rootMargin for IntersectionObserver
- Type:
string
Function
- Default:
src => src && src.charAt(0) === '/' && !src.startsWith(ctx.base) ? ctx.base + src.slice(1) : src
- Required:
false
Config prefix for src in images
- Base URL already included by default, But it does not include the
<img/>
label in the markdown file
If you need to use both Base URL
and <img/>
labels, refer to
<img :data-src="$withBase('/img.png')" loading="lazy" class="lazy">
- In order to better supporting image lazy loading, it is better to specify the size of the image (in some themes, you need to set
display
asinline block
orblock
separately), so as to ensure that the image can occupy the position it should occupy