Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

VueComponent to lazy load images and components using a HOC to track window scroll position.

License

NotificationsYou must be signed in to change notification settings

CCherry07/vue-lazy-load-image-component

Repository files navigation

# Yarn$ yarn add vue-lazy-load-image-component# Pnpm$ pnpm i vue-lazy-load-image-component

LazyLoadImage usage

<template>  <div>    <MyImage:image="image" />    <span>{{ image.caption }}</span>  </div></template><script lang="ts" setup>import {LazyLoadImage }from"vue-lazy-load-image-component";const image=ref({  alt:"My image",  height:100,  src:"https://example.com/image.jpg",  width:100,  caption:"My image caption",});</script>

Props

PropTypeDefaultDescription
afterLoadFunctionFunction called after the image has been completely loaded.
beforeLoadFunctionFunction called right before the placeholder is replaced with the image element.
delayMethodStringthrottleMethod from lodash to use to delay the scroll/resize events. It can bethrottle ordebounce.
delayTimeNumber300Time in ms sent to the delayMethod.
effectStringName of the effect to use. Please, read next section with an explanation on how to use them.
placeholderReactClass<span>React element to use as a placeholder.
placeholderSrcStringImage src to display while the image is not visible or loaded.
thresholdNumber100Threshold in pixels. So the image starts loading before it appears in the viewport.
useIntersectionObserverBooleantrueWhether to use browser's IntersectionObserver when available.
visibleByDefaultBooleanfalseWhether the image must be visible from the beginning.
wrapperClassNameStringIn some occasions (for example, when using a placeholderSrc) a wrapper span tag is rendered. This prop allows setting a class to that element.
wrapperPropsObjectnullProps that should be passed to the wrapper span when it is rendered (for example, when using placeholderSrc or effect)
...Any other image attribute

Using effects

LazyLoadImage includes several effects ready to be used, they are useful to add visual candy to your application, but are completely optional in case you don't need them or want to implement you own effect.

They rely on CSS and the corresponding CSS file must be imported:

<template>  <div>    <MyImage:image="image" />  </div></template><script lang="ts" setup>import {LazyLoadImage }from"vue-lazy-load-image-component";import"vue-lazy-load-image-component/lib/style.css";const image=ref({  alt:"My image",  height:100,  src:"https://example.com/image.jpg",  width:100,});</script>

The current available effects are:

  • blur: renders a blurred image based onplaceholderSrc and transitions to a non-blurred one when the image specified in the src is loaded.

Screenshot of the blur effect

  • black-and-white: renders a black and white image based onplaceholderSrc and transitions to a colorful image when the image specified in the src is loaded.

Screenshot of the black-and-white effect

  • opacity: renders a blank space and transitions to full opacity when the image is loaded.

Screenshot of the opacity effect

LazyLoadComponent usage

<template>  <div>    <LazyLoadComponent>      <MyComponent />    </LazyLoadComponent>  </div></template><script lang="ts" setup>import {LazyLoadComponent }from"vue-lazy-load-image-component";importMyComponentfrom"./MyComponent.vue";</script>

Props

PropTypeDefaultDescription
afterLoadFunctionFunction called after the component has been rendered.
beforeLoadFunctionFunction called right before the component is rendered.
delayMethodStringthrottleMethod from lodash to use to delay the scroll/resize events. It can bethrottle ordebounce.
delayTimeNumber300Time in ms sent to the delayMethod from lodash.
placeholderReactClass<span>React element to use as a placeholder.
thresholdNumber100Threshold in pixels. So the component starts loading before it appears in the viewport.
useIntersectionObserverBooleantrueWhether to use browser's IntersectionObserver when available.
visibleByDefaultBooleanfalseWhether the component must be visible from the beginning.

UsingtrackWindowScroll HOC to improve performance

When you have many elements to lazy load in the same page, you might get poor performance because each one is listening to the scroll/resize events. In that case, it's better to wrap the deepest common parent of those components with a HOC to track those events (trackWindowScroll).

For example, if we have anApp which renders aGallery, we would wrap theGallery component with the HOC.

<template>  <div>    <GalleryWithScrollTracking:images="images" />  </div></template><script lang="ts" setup>import {trackWindowScroll }from"vue-lazy-load-image-component";importGalleryfrom"./Gallery.vue";// Wrap Gallery with trackWindowScroll HOC so it receives// a scrollPosition prop to pass down to the imagesconst images=ref([  {    alt:"My image",    height:100,    src:"https://example.com/image.jpg",    width:100,    scrollPosition: { x:0, y:0 },  },  {    alt:"My image 2",    height:100,    src:"https://example.com/image2.jpg",    width:100,    scrollPosition: { x:0, y:0 },  },]);const GalleryWithScrollTracking=trackWindowScroll(Gallery);</script>

You must set the propscrollPosition to the lazy load components. This way, they will know the scroll/resize events are tracked by a parent component and will not subscribe to them.

Props

LazyLoadImage

PropTypeDefaultDescription
scrollPositionObjectObject containingx andy with the curent window scroll position. Required.
afterLoadFunctionFunction called after the image has been rendered.
beforeLoadFunctionFunction called right before the image is rendered.
placeholderReactClass<span>React element to use as a placeholder.
thresholdNumber100Threshold in pixels. So the image starts loading before it appears in the viewport.
visibleByDefaultBooleanfalseWhether the image must be visible from the beginning.
wrapperPropsObjectnullProps that should be passed to the wrapper span when it is rendered (for example, when using placeholderSrc or effect)
...Any other image attribute

Component wrapped withtrackWindowScroll (in the example,Gallery)

PropTypeDefaultDescription
delayMethodStringthrottleMethod from lodash to use to delay the scroll/resize events. It can bethrottle ordebounce.
delayTimeNumber300Time in ms sent to the delayMethod from lodash.
useIntersectionObserverBooleantrueWhether to use browser's IntersectionObserver when available.

Notice you can do the same replacingLazyLoadImage withLazyLoadComponent.

When to usevisibleByDefault?

The propvisibleByDefault makes the LazyLoadImage to behave like a normal<img>. Why is it useful, then?

Imagine you are going to lazy-load an image you have already loaded in the same page. In that case, there is no need to lazy-load it because it's already stored in the cache of the user's browser. You can directly display it.

Maybe the following code snippet will make it more clear:

<template>  <div>    <imgsrc="/landscape.jpg"alt="Beautiful landscape" />    <GalleryWithScrollTracking:images="images" />  </div></template><script lang="ts" setup>import {trackWindowScroll }from"vue-lazy-load-image-component";importGalleryfrom"./Gallery.vue";const images=ref([  {    alt:"My image",    height:100,    src:"https://example.com/image.jpg",    width:100,  },  {    alt:"My image 2",    height:100,    src:"https://example.com/image2.jpg",    width:100,// If the image we are creating here has the same src than before,// we can directly display it with no need to lazy-load.    visibleByDefault:image.src==="/landscape.jpg",  },]);const GalleryWithScrollTracking=trackWindowScroll(Gallery);</script>

reference

react-lazy-load-image-component

About

VueComponent to lazy load images and components using a HOC to track window scroll position.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp