https://github.com/sourcey/recliner
A lightweight production ready jQuery plugin for lazy loading images and other dynamic content
https://github.com/sourcey/recliner
Last synced: 2 months ago
JSON representation
A lightweight production ready jQuery plugin for lazy loading images and other dynamic content
- Host: GitHub
- URL: https://github.com/sourcey/recliner
- Owner: sourcey
- License: mit
- Created: 2014-09-08T13:39:07.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2024-12-14T09:23:55.000Z (over 1 year ago)
- Last Synced: 2025-03-29T17:08:40.566Z (about 1 year ago)
- Language: JavaScript
- Homepage: http://sourcey.com/recliner
- Size: 34.2 KB
- Stars: 51
- Watchers: 9
- Forks: 21
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Recliner.js
Recliner is a super lightweight (1KB) jQuery plugin for lazy loading images, iframes and other dynamic (AJAX) content. Being lazy never felt so good, just hook it up, and start sippin' those margaritas!
The script was born out of necessity when one of our clients came to us with massive scroll lag on one of their media heavy mobile news sites. It turned out that `lazy-load-xt` was the culprit, so naturally we tested the other lazy load scripts out there but **none** of them met our simple criteria:
* Lightweight
* Sets stateful CSS classes on elements
* Ability to override event callbacks for custom behaviour
* Can load *any* dynamic content (images, iframes, AJAX)
* Mobile friendly
* Printer friendly
Recliner is currently used on some very high traffic sites, so it's well tested and production ready.
## Demo
For more information and a live demo see the project page: [http://sourcey.com/recliner](http://sourcey.com/recliner)
## Installation
If you use `bower` then type:
```bash
bower install recliner
```
Or if you like using `npm` then go ahead and type:
```bash
npm install jquery-recliner
```
Otherwise, just download `recliner.min.js` and stick it in your assets folder :)
## Usage
Add jQuery (2.x or 1.x) and Recliner to your HTML source:
```html
```
Bind Recliner on elements with the `.lazy` class:
```javascript
$(".lazy").recliner({
attrib: "data-src", // selector for attribute containing the media src
throttle: 300, // millisecond interval at which to process events
threshold: 100, // scroll distance from element before its loaded
printable: true, // be printer friendly and show all elements on document print
live: true // auto bind lazy loading to ajax loaded elements
});
```
You can also progrmatically trigger an update to check for new elements to be loaded:
```javascript
$(window).trigger("lazyupdate");
```
Recliner can be used to load a range of different dynamic content.
##### Images
**Note:** It's a good idea to specify image dimensions explicitly so your page height doesn't go berserk as lazy content is loaded into the DOM.
```html
```
##### Iframes
```html
```
##### AJAX
```html
Loading, be patient damnit!
```
## Callback API
Recliner exposes a simple event based API so you can implement your own custom behaviour using callbacks:
##### lazyload
The `lazyload` event will be triggered on elements that are about to be loaded.
```javascript
$(document).on('lazyload', '.lazy', function() {
var $e = $(this);
// do something with the element to be loaded...
console.log('lazyload', $e);
});
```
##### lazyshow
The `lazyshow` event will be triggered on elements after they have been loaded.
```javascript
$(document).on('lazyshow', '.lazy', function() {
var $e = $(this);
// do something with the loaded element...
console.log('lazyshow', $e);
});
```
## Styling and Animations
Recliner will set the following stateful CSS classes on your elements:
* `lazy-loading`: Set while the element is being loaded
* `lazy-loaded`: Set when the element has loaded
Using the stateful classes you can add some fancy transitions to your images:
```css
img {
opacity: 0;
transition: opacity .333s ease-in;
}
img.lazy-loaded {
opacity: 1;
}
```
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
## Issues
If you find any bugs please use the [Github issue tracker](https://github.com/sourcey/recliner/issues).