Register now or login here to start promoting your blog and your favourite articles.
Create Background Scrolling Effect with jQuery
23 Mar 2010 - 11 Comments
In this tutorial, we're not only go through how to make it, we will have it in plugin as well which allow user to customize the speed of the movement and direction (vertically, horizontally and diagonally).
Author: kevin | Source: queness
Demonstration Download

Introduction

Greeting, today we are going to make a scrolling background effect. This script will move the background of any html tag, either vertically or horizontally. I used this script in one of my project which has a blue sky with clouds and it makes the whole website came alive. I think that's pretty impressive.

In this tutorial, we're not only go through how to make it, we will have it in plugin as well which allow user to customize the speed of the movement and direction (vertically, horizontally and diagonally). Alright, the concept behind this animation is really simple. We will use the background-position attribute to move the background, very easy right?

1. HTML

There is nothing about HTML. Basically we will put the background image in the cloud div and animate the background image.



	
		Scrolling Backgorund
	

	
		

2. CSS

I think, from all of my tutorial, this would be the shortest and simplest CSS code.

body {
	margin:0;
	padding:0;
	background:#fff; 	
}

.clouds {
	width:300px; 
	height:300px; 
	margin:10px; 
	border:2px solid #ccc;
	background:#3e83c8 url(images/bg_clouds.png) repeat-x 0 bottom; 
}

3. Javascript

Alright, this is the part that makes everything alive! We will make it more configurable. We can adjust the speed and the direction. Of course, to make sure you understand what it does, I have put comments in each line of code.


$(document).ready(function () {
    // speed in milliseconds
	var scrollSpeed = 70;

	// set the direction
	var direction = 'h';

	// set the default position
	var current = 0;

	function bgscroll(){

    	// 1 pixel row at a time
	    current -= 1;
   
	    // move the background with backgrond-position css properties
	    $('div.clouds').css("backgroundPosition", (direction == 'h') ? current+"px 0" : "0 " + current+"px");
   
	}

	//Calls the scrolling function repeatedly
	var init = setInterval("bgscroll()", scrollSpeed);	
});

4. The Plugin

I'm not an expert in creating plugins but thanks to my friend from web developer juice, with his amazing skill, he managed to convert the whole script in a very short time and here is the code. He has got some interesting tutorials and articles as well. Make sure you check out his website. :)

(function() {
	$.fn.bgscroll = $.fn.bgScroll = function( options ) {
		
		if( !this.length ) return this;
		if( !options ) options = {};
		if( !window.scrollElements ) window.scrollElements = {};
		
		for( var i = 0; i < this.length; i++ ) {
			
			var allowedChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
			var randomId = '';
			for( var l = 0; l < 5; l++ ) randomId += allowedChars.charAt( Math.floor( Math.random() * allowedChars.length ) );
			
				this[ i ].current = 0;
				this[ i ].scrollSpeed = options.scrollSpeed ? options.scrollSpeed : 70;
				this[ i ].direction = options.direction ? options.direction : 'h';
				window.scrollElements[ randomId ] = this[ i ];
				
				eval( 'window[randomId]=function(){var axis=0;var e=window.scrollElements.' + randomId + ';e.current -= 1;if (e.direction == "h") axis = e.current + "px 0";else if (e.direction == "v") axis = "0 " + e.current + "px";else if (e.direction == "d") axis = e.current + "px " + e.current + "px";$( e ).css("background-position", axis);}' );
														 
				setInterval( 'window.' + randomId + '()', options.scrollSpeed ? options.scrollSpeed : 70 );
			}
			
			return this;
		}
})(jQuery);



Usage

So, this is how you use it. Make sure you've included jQuery framework and the bgscroll plugin. It accepts two properties:

  • scrollSpeed : speed of the movement
  • direction : direction can be either "v", "h" or "d" (vertical, horizontal or diagonal)
$( function() {
	$('.clouds1').bgscroll({scrollSpeed:10 , direction:'h' });
	$('.clouds2').bgscroll({direction:'d'});
});
Check out the Plugin in action

Conclusion

I hope the tutorial and plugin will be useful for you. If you really like this article and want to support me, please share it with your friends :) Thanks!

Demonstration Download

Copyright & Usage

The effects and techniques demonstrated in tutorials on Queness can be used in whatever manner you wish without attribution. You cannot copy whole tutorials (unless permission is given), either in English or translated to another language.

Share This Post to Support Me! :)


Comments

Jess on 6 Aug 2010 says:
Thank you thank you thank you! That was driving me nuts how to figure out and your method is so simple and easy to use!
PrOscarHome on 10 Apr 2010 says:
Great tips!
Ryan Cowles on 26 Mar 2010 says:
I've seen the effect plenty of times, but have yet to try it myself. I think I might have to!
Beben on 25 Mar 2010 says:
its a great...and so smooth too if we change direction to "v"
thanks ^^
kevin on 24 Mar 2010 says:
Hi, I actually have a sample website that using it... it's called http://www.eggracer.com.au

have a look, it might lag a bit sometimes... but you see, it has a jquery image slider in it... otherwise, it will look quite smooth! :)
rory on 24 Mar 2010 says:
That looks really cool perfect for a website I'm doing at the minute. Think I'll have the whole site background moving, if thats possible. Would the site slow down much if I made it massive?
Ricardo Zea on 23 Mar 2010 says:
This is a very interesting plugin.

However, you need to check this other one out, it's called 'Spritely': www.spritely.net
ben smithson on 23 Mar 2010 says:
Cool effect. I filled up my screen and my browser got all flicker-y. I can think of fun uses for this effect - thanks for sharing!!
fiona on 23 Mar 2010 says:
i apologize...dowmloded files work excellent!

great, thanks!
fiona on 23 Mar 2010 says:
it doesn't work even i have copied all parts :( and pasted in html empty file.
image i replaced with one from my root
where i made mistake ?
  • Page :
  • 1
  • 2


Leave a comment

Subscribe RSS Subscribe RSS, Keep yourself updated with queness latest posts!
Pixel Crayons

Buy wholesale computers directly from China at DHgate.com

Discover the tools to build your e-Commerce Site with Netfirms

Buy China Products from Made-in-China.com

Cocktail Dresses

SmartPhone Cell Phone

Wholesale electronics

Web Hosting Rating

Buy WOW Gold

Get your cdl today

Debt collector review

  •  
  •  
  •  
  •  
  •  

Community News

Recent Comments

Random Posts


View all posts and news Back to top

About the Author

A web designer and developer who is passionate and keen on contributing to the web development industry. Feel free to say hi to me, or follow me on twitter.

Kevin Liew