Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

LazyLoad

LazyLoad is a plugin that allows you to defer image loading until the image is scrolled to.

Download Debut Article Example Usagehttp://davidwalsh.name/wp-admin/page.php?action=edit&post=3414#post_name

Plugin Code (Version 1.0)

var LazyLoad = new Class({

	Implements: [Options,Events],

	/* additional options */
	options: {
		range: 200,
		image: 'blank.gif',
		resetDimensions: true,
		elements: 'img',
		container: window,
		fireScroll: true,
		mode: 'vertical'
	},

	/* initialize */
	initialize: function(options) {
	
		/* vars */
		this.setOptions(options);
		this.container = document.id(this.options.container);
		this.elements = $$(this.options.elements);
		var axis = (this.options.mode == 'vertical' ? 'y': 'x');
		this.containerDimension = this.container.getSize()[axis];
		this.start = 0;

		/* find elements remember and hold on to */
		this.elements = this.elements.filter(function(el) {
			/* reset image src IF the image is below the fold and range */
			if(el.getPosition(this.container)[axis] > this.containerDimension + this.options.range) {
				el.store('oSRC',el.get('src')).set('src',this.options.image);
				if(this.options.resetDimensions) {
					el.store('oWidth',el.get('width')).store('oHeight',el.get('height')).set({'width':'','height':''});
				}
				return true;
			}
		},this);
	
		/* create the action function */
		var action = function() {
			var cpos = this.container.getScroll()[axis];
			if(cpos > this.start) {
				this.elements = this.elements.filter(function(el) {
					if((this.container.getScroll()[axis] + this.options.range + this.containerDimension) >= el.getPosition(this.container)[axis]) {
						if(el.retrieve('oSRC')) { el.set('src',el.retrieve('oSRC')); }
						if(this.options.resetDimensions) {
							el.set({
								width: el.retrieve('oWidth'),
								height: el.retrieve('oHeight') 
							});
						}
						this.fireEvent('load',[el]);
						return false;
					}
					return true;
				},this);
				this.start = cpos;
			}
			this.fireEvent('scroll');
			/* remove this event IF no elements */
			if(!this.elements.length) {
				this.container.removeEvent('scroll',action);
				this.fireEvent('complete');
			}
		}.bind(this);
	
		/* listen for scroll */
		this.container.addEvent('scroll',action);
		if(this.options.fireScroll) { this.container.fireEvent('scroll'); }
	}
});

/* usage */
window.addEvent('domready',function() {
	var lazyloader = new LazyLoad();
});

Options and Events

Options for LazyLoad include:

  • range: (defaults to 200) The amount of space from the container’s bottom position that you want to look for images to load.
  • image: (defaults to “blank.gif”) The image to replace the original image.
  • resetDimensions: (defaults to true) Removes the image’s width and height attributes.
  • elements: (defaults to “img”) Images to consider for lazy loading.
  • container: (defaults to window) The container for which to look for images within.
  • mode: (defaults to “vertical”) The mode for which the plugin will adjust to.

Events for LazyLoad include:

  • Complete: Fires when all images have been loaded.
  • Load: Fires on each individual image once it has been loaded.
  • Scroll: Fires when the container is scrolled.

Code Revisions & Bug Fixes

None.

Discussion

  1. boris
    November 7, 2009 @ 5:11 pm

    Hey David, I am using your Side Scroll plugin, and I was wondering what I need to change for this to work with scrolling sideways. Thanks for your help :)

  2. December 14, 2009 @ 4:21 am

    @Boris: I needed the same behaviour as you and implemented it: http://gist.github.com/255921

  3. boris
    December 14, 2009 @ 8:07 am

    @thomasd: I edited the original and switched the ‘Y’ with ‘X’ and it seems to work. Besides the ability to switch between modes did you do the same thing?

  4. December 23, 2009 @ 5:49 am

    Yes making an option for switching from ‘x’ to ‘y’ was all that was needed.

  5. June 30, 2010 @ 7:11 am

    Thanks David,

    once more a neat and highly usable plugin – good work as usual ;)

    cheers
    Frank

  6. gregor
    July 11, 2010 @ 11:41 am

    It looks like in GChrome images were loaded at once all of them -> Example usage

  7. July 11, 2010 @ 12:00 pm

    @Gregor: WebKit doesn’t respect LazyLoading. No way no how.

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!