Lazy Load - 图1

Lazy Load

Lazy Load delays loading of images on page while they are outside of viewport until user scrolls to them.

It will make the page load faster, improve scrolling performance and also save traffic.

Note, that lazy images and elements should be inside of scrollable <div class="page-content"> to work correctly

Usage

With Images

To use lazy load on images:

  1. specify image source into data-src attribute instead of src attribute
  2. add lazy class to the image
  1. <div class="page-content">
  2. ...
  3. <img data-src="path/to/image.jpg" class="lazy">
  4. ...
  5. </div>

After lazy image successfully loaded, or in case of error loading the image file, the lazy class will be replaced with lazy-loaded class.

With Background Images

It is also possible to use lazy load for background images, in this case:

  1. specify element’s background image source into data-background
  2. add lazy class to element
  1. <div class="page-content">
  2. ...
  3. <div data-background="path/to/image.jpg" class="lazy">
  4. ...
  5. </div>
  6. ...
  7. </div>

After lazy image successfully loaded, or in case of error loading the image file, the lazy class will be replaced with lazy-loaded class.

With Fade-in Effect

If you want to add fade-in effect when image is loaded, you need to add additional lazy-fade-in class to image/element:

  1. <div class="page-content">
  2. ...
  3. <img data-src="path/to/image.jpg" class="lazy lazy-fade-in">
  4. <div data-background="path/to/image.jpg" class="lazy lazy-fade-in">
  5. ...
  6. </div>
  7. ...
  8. </div>

Lazy Load App Parameters

It is possible to control some default lazy loading behavior using global app parameters by passing lazy loading related parameters under lazy parameter:

ParameterTypeDefaultDescription
placeholderstringLazy load image placeholder source to show while image is not yet loaded. By default it is 1x1 px image
thresholdnumber0By default images are loaded when they appear on the screen. Use this parameter if you want to load images earlier. Setting it to 50 will load image when it 50 pixels before it appears on viewport
sequentialbooleantrueIf enabled, then lazy images will be loaded one by one when they appear in viewport
observerbooleantrueIf enabled, it will use Intersection Observer if it is supported by browser.

For example:

  1. var app = new Framework7({
  2. lazy: {
  3. threshold: 50,
  4. sequential: false,
  5. },
  6. });

Lazy Load App Methods

If you add lazy load images manually after page initialization (e.g. after Ajax request) then Lazy Load won’t be initialized automatically and won’t work as expected. In this case you need to init it manually using the following method:

app.lazy.create(pageEl) - initialize lazy loading on page

  • pageEl - HTMLElement or string (with CSS Selector) of page which contains lazy load images. Required.

And if you want to destroy/disable it later:

app.lazy.destroy(pageEl) - destroy/disable lazy loading on page

  • pageEl - HTMLElement or string (with CSS Selector) of page which contains lazy load images. Required.

If you want to force any lazy image/element to load you may use the following method:

app.lazy.loadImage(imageEl, callback) - force to load lazy image

  • imageEl - HTMLElement or string (with CSS Selector) of lazy image or element (element with lazy class). Required.
  • callback - function - callback function that will be executed when image file loaded or in case of error loading this file.

Lazy Load Events

Lazy load will fire the following DOM events on lazy element and events on app instance:

DOM Events

EventTargetDescription
lazy:loadLazy Element<… class=”lazy”>Event will be triggered in the beginning of image file loading
lazy:loadedLazy Element<… class=”lazy”>Event will be triggered after image file successfully loaded
lazy:errorLazy Element<… class=”lazy”>Event will be triggered in case of error loading image file

App Instance Events

Lazy load emits events on app instance as well.

EventTargetArgumentsDescription
lazyLoadapplazyElEvent will be triggered in the beginning of image file loading. As an argument it receives lazy loading HTML element
lazyLoadedapplazyElEvent will be triggered after image file successfully loaded. As an argument it receives lazy loading HTML element
lazyErrorapplazyElEvent will be triggered in case of error loading image file. As an argument it receives lazy loading HTML element

Trigger Lazy Load Manually

It is possible to force lazy handler to check lazy images by triggering lazy event on lazy image/element. It is useful in case you have added lazy elements dynamically and want to check should them be loaded or not without scrolling the page. For example:

  1. $$('img.lazy').trigger('lazy');
  2. $$('div.lazy').trigger('lazy');

Examples

  1. img.demo-lazy {
  2. width: 100%;
  3. height: auto;
  4. display: block;
  5. }
  6. div.demo-lazy {
  7. background: #aaa;
  8. background-size: cover;
  9. height: 60vw;
  10. }
  1. <div class="page-content">
  2. <div class="block block-strong">
  3. <p> <img data-src="https://cdn.framework7.io/placeholder/nature-500x500-1.jpg" width="1500" height="1500" class="lazy lazy-fade-in demo-lazy"/></p>
  4. <p>Lorem ipsum ...</p>
  5. <p> <img data-src="https://cdn.framework7.io/placeholder/nature-500x500-2.jpg" width="1500" height="1500" class="lazy lazy-fade-in demo-lazy"/></p>
  6. <p>Aenean id congue orci...</p>
  7. <p> <img data-src="https://cdn.framework7.io/placeholder/nature-500x500-3.jpg" width="1500" height="1500" class="lazy lazy-fade-in demo-lazy"/></p>
  8. <p>Pellentesque aliquam maximus ...</p>
  9. <p> <img data-src="https://cdn.framework7.io/placeholder/nature-500x500-4.jpg" width="1500" height="1500" class="lazy lazy-fade-in demo-lazy"/></p>
  10. <p>Duis ullamcorper velit id enim rutrum...</p>
  11. <p> <img data-src="https://cdn.framework7.io/placeholder/nature-500x500-5.jpg" width="1500" height="1500" class="lazy lazy-fade-in demo-lazy"/></p>
  12. <p>Suspendisse potenti...</p>
  13. <p> <img data-src="https://cdn.framework7.io/placeholder/nature-500x500-6.jpg" width="1500" height="1500" class="lazy lazy-fade-in demo-lazy"/></p>
  14. <p>Lorem ipsum ...</p>
  15. <p> <img data-src="https://cdn.framework7.io/placeholder/nature-500x500-7.jpg" width="1500" height="1500" class="lazy lazy-fade-in demo-lazy"/></p>
  16. <p>Aenean id congue orci...</p>
  17. <p> <img data-src="https://cdn.framework7.io/placeholder/nature-500x500-8.jpg" width="1500" height="1500" class="lazy lazy-fade-in demo-lazy"/></p>
  18. <p>Lorem ipsum ...</p>
  19. <p> <img data-src="https://cdn.framework7.io/placeholder/people-500x500-1.jpg" width="1500" height="1500" class="lazy lazy-fade-in demo-lazy"/></p>
  20. <p>Pellentesque aliquam maximus ...</p>
  21. <p> <img data-src="https://cdn.framework7.io/placeholder/nature-500x500-10.jpg" width="1500" height="1500" class="lazy lazy-fade-in demo-lazy"/></p>
  22. <p>Duis ullamcorper velit...</p>
  23. <p><b>Using as background image:</b></p>
  24. <div data-background="https://cdn.framework7.io/placeholder/people-500x500-10.jpg" class="lazy lazy-fade-in demo-lazy"></div>
  25. <p>Suspendisse potenti...</p>
  26. </div>
  27. </div>

← Inputs / Form Inputs

Link →