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:
[sociallocker]
[/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
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 > |
09 | < div id = "main_object" ></ div > |
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 } |
4 | position : relative ; width : 950px ; height : 700px ; overflow : hidden ; cursor : pointer ; |
5 | background-image : url (../images/layer 1 .png), url (../images/layer 2 .png), url (../images/layer 3 .png), url (../images/bg.jpg); |
6 | background-position : center bottom , right top , right bottom , left top ; |
7 | background-repeat : no-repeat ; |
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 | background : url (../images/layer 1 .png) no-repeat center bottom , url (../images/layer 2 .png) no-repeat right top , url (../images/layer 3 .png) no-repeat right bottom , url (../images/bg.jpg) no-repeat left top ; |
Step 3. JS
Here are a little of JS for our animation:
js/main.js
17 | this .obj = document.getElementById( 'main_object' ); |
18 | this .x2 = this .obj.clientWidth; |
23 | mousemove : function (e) { |
27 | this .x1 = this .xm - this .obj.offsetLeft - ex76.l1w/2; |
28 | this .y1 = this .ym - this .obj.offsetTop - ex76.l1h/2; |
34 | if (ex76.x2 < -ex76.l2w) ex76.x2 = ex76.obj.clientWidth; |
37 | if (ex76.x3 > ex76.obj.clientWidth) ex76.x3 = -ex76.l3w; |
39 | ex76.obj.style.backgroundPosition = ex76.x1 + 'px ' + ex76.y1 + ', ' + ex76.x2 + 'px top, ' + ex76.x3 + 'px bottom, left top' ; |
41 | setTimeout(ex76.run, 20); |
44 | window.onload = function () { |
47 | document.onmousemove = function (e) { |
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):
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!