jQuery Background Position

A jQuery plugin that allows the position of a background image to be animated. If you find this plugin useful please vote for it on the jQuery site.

The current version is 1.0.1 and is available under the GPL and MIT licences. For more detail see the documentation reference page.

Basics

You can animate the position of a background image as either 'backgroundPosition' or 'background-position', providing both the horizontal and vertical offsets.

Simple animation:

Foreground text
$('#simpleButton').click(function() {
	$('#simpleBG').css({backgroundPosition: '0px 0px'}).
		animate({backgroundPosition: '-200px -150px'});
});

Positions

You have several options when specifying the new background position, besides supplying an absolute offset.

By named position:

Foreground text
$('#namedSelect').change(function() {
	$('#namedBG').animate({backgroundPosition: $(this).val()});
});

By relative position:

Foreground text
$('#relativeButton').click(function() {
	$('#relativeBG').animate({backgroundPosition: $('#relativeSelect').val()});
});

Options

You can still use the standard options of all animations.

Speed:

Foreground text
$('#speedButton').click(function() {
	var speed = parseInt($('#speedSelect').val(), 10);
	speed = (isNaN(speed) ? $('#speedSelect').val() : speed);
	$('#speedBG').animate({backgroundPosition: '+=50% +=50%'}, speed);
});

Easing:

Foreground text
$('#easingButton').click(function() {
	$('#easingBG').animate({backgroundPosition: '+=50% +=50%'},
		2000, $('#easingSelect').val());
});

Callback:

Foreground text
$('#callbackButton').click(function() {
	$('#callbackBG').animate({backgroundPosition: '+=50% +=50%'},
		1000, function() { alert('Completed animation'); });
});

In the Wild

This tab highlights examples of this plugin in use "in the wild".

To add another example, please contact me (kbwood{at}iinet.com.au) and provide the plugin name, the URL of your site, its title, and a short description of its purpose and where/how the plugin is used.

Quick Reference

Below are examples of how the plugin can be used. For more detail see the documentation reference page.

$(selector).animate({backgroundPosition: '-100px -50px'});
$(selector).animate({'background-position': '+=50% +=25%'});
$(selector).animate({backgroundPosition: 'left center'});

Usage

  1. Include the jQuery library in the head section of your page.
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
  2. Download and include the jQuery Background Position JavaScript in the head section of your page.
    <script type="text/javascript" src="jquery.backgroundpos.js"></script>
    Alternately, you can use the packed version jquery.backgroundpos.pack.js (1.7K vs 2.5K), or the minified version jquery.backgroundpos.min.js (1.6K, 0.8K when zipped).
  3. Invoke the animation on your elements.
    $(selector).animate({backgroundPosition: '-100px -50px'});

For more detail see the documentation reference page.

Comments

I just wanted to thank you for your wonderful jQuery plugin. I was having problems with IE not working with my animations and your plugin really helped me out.

Robert Stringer

Contact Keith Wood at kbwood{at}iinet.com.au with comments or suggestions.

Change History

VersionDateChanges
1.0.128 May 2011
  • Updated for jQuery 1.6
  • Allow explicit positive values for positioning
  • Default to center if no current position
1.0.013 Nov 2010
  • Initial release

Valid HTML 4.01 Strict