{"id":1660,"date":"2008-12-03T05:45:29","date_gmt":"2008-12-03T12:45:29","guid":{"rendered":"http:\/\/css-tricks.com\/?p=1660"},"modified":"2015-05-05T18:46:02","modified_gmt":"2015-05-06T01:46:02","slug":"jquery-robot","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/jquery-robot\/","title":{"rendered":"Building an Animated Cartoon Robot with jQuery"},"content":{"rendered":"
Aside from being a fun exercise, what purpose does something like this have? None that’s plainly obvious. Its about as useful as a miniature ship in a bottle. Yet it does have an underlying purpose. It could inspire someone to look beyond the perceived constraints of web designers and developers. <\/p>\n
View Demo<\/a><\/p>\n <\/p>\n This project was created by layering several empty divs over each other with transparent PNGs as background images. <\/p>\n The backgrounds were animated at different speeds using a jQuery plug-in by Alexander Farkas. This effect simulates a faux 3-D animated background dubbed the \u201cparallax effect\u201d originating from old-school side scrolling video games. <\/p>\n The robot is comprised similarly to the background animation scene by layering several DIVs together to create the different robot pieces. The final step, was animating the robot with some jQuery. <\/p>\n <\/p>\n <\/p>\n The structure of the divs closely resembles our diagram. None of the DIVs has the width attribute specified so they will expand to fill the size of any browser window they are displayed on. NOTE:<\/strong> All the images that make the background scenery parallax effect are 9999px wide. Well beyond the width of any computer display or television in common use. We\u2019ll use CSS to place the background images exactly where we want within each particular div.<\/p>\n The CSS for this project is just as simple as the markup.<\/p>\n Absolute positioning is used to pull any header or paragraph text 9999px to the left of the screen. Then we declare every DIV in the page position: relative. By making all the DIVs position: relative;, we now have the ability to the use the z-index property to reverse the natural stacking order of the robot DIVs.<\/p>\nOverview<\/h3>\n
The Markup<\/h3>\n
<div id=\"wrapper\">\r\n\t\r\n <div id=\"cloud-01\">\r\n <div id=\"cloud-02\">\r\n <div id=\"mountains-03\">\t\r\n <div id=\"ground\">\r\n\t\r\n <div id=\"full-robot\">\r\n <div id=\"branding\"><h1>Robot Head.<\/h1><\/div> \r\n <div id=\"content\"><p> Robot Chest.<\/p><\/div> \r\n <div id=\"sec-content\"><p> Robot Pelvis.<\/p><\/div> \r\n <div id=\"footer\"><p> Robot Legs.<\/p><\/div>\r\n <\/div>\r\n\r\n <\/div>\r\n <\/div>\r\n <\/div>\r\n <\/div>\r\n\r\n<\/div><\/code><\/pre>\n
The Style<\/h3>\n
h1, p { position: absolute; left: -9999px; }\r\n\r\ndiv {position: relative;}\r\n\r\n#wrapper { background: #bedfe4 url(..\/images\/sun.png) no-repeat left -30px; border: 5px solid #402309;}\r\n\r\n#cloud-01 { background: url(..\/images\/clouds-01.png) no-repeat left -100px; } \r\n \r\n#cloud-02 {\tbackground: url(..\/images\/clouds-02.png) no-repeat left top; }\r\n\r\n#mountains-03 { background: url(..\/images\/mountain-03.png) no-repeat left bottom; }\r\n\r\n#ground { background: url(..\/images\/ground-05.png) no-repeat left bottom; }\r\n\r\n#full-robot { width: 271px; }\r\n\r\n#branding { \r\n\tbackground: url(..\/images\/robot-head.png) no-repeat center top;\r\n\twidth: 271px;\r\n\theight: 253px;\r\n\tz-index: 4; \r\n\t}\r\n\r\n#content {\r\n\tbackground: url(..\/images\/robot-torso.png) no-repeat center top;\r\n\twidth: 271px;\r\n\theight: 164px;\r\n\tz-index: 3;\r\n\tmargin-top: -65px;\r\n\t}\r\n\r\n#sec-content {\r\n\tbackground: url(..\/images\/robot-hips.png) no-repeat center top;\r\n\twidth: 271px;\r\n\theight: 124px;\r\n\tz-index: 2;\r\n\tmargin-top: -90px;\r\n\t}\r\n\r\n#footer {\r\n\tbackground: url('..\/images\/robot-legs.png') no-repeat center top;\r\n\twidth: 271px;\r\n\theight: 244px;\r\n\tz-index: 1;\r\n\tmargin-top: -90px;\r\n\t}<\/code><\/pre>\n
The jQuery JavaScript<\/h3>\n