移动端安卓和IOS开发框架Framework7教程-Images 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

For Images <img>

To use lazy load on images:

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

For 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>
复制

That is all, after image appears in viewport, it will be loaded. After that lazy class will be replaced with lazy-loaded.

With Fade-in Effect

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

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

Trigger Lazy Load Manually

It is possible to trigger image loading by triggering "lazy" event on lazy image/element, for example:

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

Examples

  1. ...
  2. <div class="page-content">
  3.   <div class="content-block">
  4.     <div class="content-block-inner">
  5.       <p>
  6.         <img data-src="http://lorempixel.com/500/500/nature/1" width="100%" class="lazy lazy-fadeIn">
  7.       </p>
  8.       <p>Lorem ipsum dolor sit amet...</p>
  9.  
  10.       <p>
  11.         <img data-src="http://lorempixel.com/500/500/nature/2" width="100%" class="lazy lazy-fadeIn">
  12.       </p>
  13.       <p>Aenean id congue orci...</p>
  14.  
  15.       <p>
  16.         <img data-src="http://lorempixel.com/500/500/nature/3" width="100%" class="lazy lazy-fadeIn">
  17.       </p>
  18.       <p>Pellentesque aliquam ...</p>
  19.  
  20.       ...
  21.  
  22.       <p><b>Using as background image:</b></p>
  23.       <div data-background="http://lorempixel.com/500/500/people/10" style="background: #aaa; height:60vw; background-size-cover" class="lazy lazy-fadeIn"></div>
  24.       <p>Suspendisse potenti. Curabitur ...</p>
  25.     </div>
  26.   </div>
  27. </div>
  28. ...
复制

实例预览

 

你可能感兴趣的:(Framework7)