How to use Multiple backgrounds and Animation with CSS3

Tutorials

Multiple backgrounds with CSS3 and a little of animation. In CSS3 appear new possibility to use multiple backgrounds for the objects, in our new article I will demonstrate how to do it. And, I going to add a bit of JS code for simple animation (to make it not so boring). Commonly, it is very easy to apply multiple backgrounds – we just need to list them (separated by commas) in the object properties.

Here are samples and downloadable package:

Live Demo

[sociallocker]

download in package

[/sociallocker]


Ok, download the example files and lets start coding !


Step 1. HTML

As usual, we start with the HTML. This is source code of our sample:

index.html

01 <html>
02 <head>
03     <title>Multiple backgrounds with CSS3 and a little of animation</title>
04     <link rel="stylesheet" type="text/css" href="css/main.css" />
05     <script type="text/javascript" src="js/main.js"></script>
06 </head>
07  <body>
08     <div class="example">
09         <div id="main_object"></div>
10     </div>
11  </body>
12 </html>

Nothing special, just <div id=”main_object”></div> inside

Step 2. CSS

Here are single CSS file with all necessary styles:

css/main.css

1 body{background:#eee;margin:0;padding:0}
2 .example{background:#FFF;width:950px;border:1px #000 solid;margin:20px auto;padding:15px;-moz-border-radius: 3px;-webkit-border-radius: 3px}
3 #main_object{
4     position:relative;width:950px;height:700px;overflow:hidden;cursor:pointer;
5     background-imageurl(../images/layer1.png), url(../images/layer2.png), url(../images/layer3.png), url(../images/bg.jpg);
6     background-positioncenter bottomright topright bottomleft top;
7     background-repeatno-repeat;
8 }

Here, you can see way how I pointing these several backgrounds to our main element – all comma separated. Same and for ‘background-position’ too. Of course, if you want – you always can use use ‘background’ property to set all necessary styles, in this case – you can enum your properties in same way, separating values with commas, example:

1 backgroundurl(../images/layer1.png) no-repeat center bottomurl(../images/layer2.png) no-repeat right topurl(../images/layer3.png) no-repeat right bottomurl(../images/bg.jpg) no-repeat left top;
2 }

Step 3. JS

Here are a little of JS for our animation:

js/main.js

01 var ex76 = {
02     // variables
03     l1w  : 358, // layer 1 width
04     l1h  : 235, // layer 1 height
05     l2w  : 441, // layer 2 width
06     l3w  : 307, // layer 3 width
07     // inner variables
08     obj  : 0,
09     xm   : 0,
10     ym   : 0,
11     x1 : 0,
12     y1 : 0,
13     x2 : 0,
14     x3 : 0,
15     // initialization
16     init : function() {
17         this.obj = document.getElementById('main_object');
18         this.x2 = this.obj.clientWidth;
19         this.x3 = -this.l3w;
20         this.run();
21     },
22     // refreshing mouse positions
23     mousemove : function(e) {
24         this.xm = e.clientX;
25         this.ym = e.clientY;
26         // recalculation new positions
27         this.x1 = this.xm - this.obj.offsetLeft - ex76.l1w/2;
28         this.y1 = this.ym - this.obj.offsetTop - ex76.l1h/2;
29     },
30     // loop function
31     run : function() {
32         // shifting second layer object to left
33         ex76.x2--;
34         if (ex76.x2 < -ex76.l2w) ex76.x2 = ex76.obj.clientWidth;
35         // shifting second layer object to right
36         ex76.x3++;
37         if (ex76.x3 > ex76.obj.clientWidth) ex76.x3 = -ex76.l3w;
38         // updating background-position value with new positions
39         ex76.obj.style.backgroundPosition = ex76.x1 + 'px ' + ex76.y1 + ', ' + ex76.x2 + 'px top, ' + ex76.x3 + 'px bottom, left top';
40         // loop
41         setTimeout(ex76.run, 20);
42     }
43 };
44 window.onload = function() {
45     ex76.init(); // initialization
46     // binding mouse move event
47     document.onmousemove = function(e) {
48         if (window.event)
49             e = window.event;
50         ex76.mousemove(e);
51     }
52 }

This is pretty easy – for second and third layer – I changing its X positions (by cycle), for first layer – I changing its position in place where located our mouse.

Step 4. Images

Also we need several images for our demo (for different layers):

    bg.jpg
    layer1.png
    layer2.png
    layer3.png

Live Demo

Conclusion

Today I told you how you can assign several images as background for single element. Hope that our demo gave you some ideas. Here are another application of this, you can create custom controls (buttons as example) with custom background, as example rounded buttons. You will need just set 2-3 layers (for beginning, center and end of button). Good luck!

Rate article