Modern lazy loading of images in under 500 bytes
Slothy uses the Intersection Observer API to ensure the efficient loading of images as they come into view. Since the API is fairly new so you may want to use a polyfill if your targeting older browsers. If Intersection Observer API is not available slothy will fall back to the normal loading of images.
With package manager:
$ npm install slothy
# OR
$ yarn add slothy
Or with CDN:
<script src="https://unpkg.com/slothy@1.2.0/dist/slothy.umd.js"></script>
Add the data-slothy
tag to images you wish to lazy load:
<img data-slothy="http://loremflickr.com/1600/1200" alt="">
<img data-slothy="http://loremflickr.com/1200/1000" alt="">
<img data-slothy="http://loremflickr.com/2000/1500" alt="">
Then initialize:
slothy()
Also see the example.
Lazy load images
Parameters
options
Object? Configuration options for slothy (optional, default{}
)options.offset
(String | Number) Offset from image in px to start loading, defaults to 100px (optional, default100
)options.threshold
Number Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. (optional, default0.01
)options.onLoad
Function Function triggered when image is done loadingoptions.dataSelector
String Data selector on element. Default is "slothy" (optional, default'slothy'
)
Contributions are welcome!
- Fork it.
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Or open up a issue.
Licensed under the MIT License.