Making a Slick MobileApp Website with jQuery & CSS
Created by Martin Angelov on Jul 9th, 2010
As a web developer, you have to bring creativity and attention to detail to your work. It is often the little things that leave a lasting impression to a visitor. Be it a lovable character illustration, or unique slideshow, it is usually not the information you present that is memorable, but the way you present it.
Today we are making a complete jQuery & CSS website for a fictional mobile application. It is going to feature semantic markup and a progressively enhanced slideshow effect. It will allow the user to see four of the most popular smartphones running the mobile app.
Step 1 – XHTML
When building websites it is important that you lay your code in a semantic way. This would include using tags for what they were supposed to be used for. You should use headings for titles, paragraphs for text (instead of generic divs) and lists where applicable.
mobileapp.html
03 | < h1 id = "logoh1" >< a href = "/" id = "logo" >MobileApp - the most useful mobile app!a >h1 > |
05 | < div id = "phoneCarousel" > |
06 | < div class = "previous arrow" >div > |
07 | < div class = "next arrow" >div > |
10 | < img id = "iphone" class = "default" src = "img/phones/iphone.png" width = "270" height = "400" alt = "iPhone" /> |
11 | < img id = "nexus" src = "img/phones/nexus_one.png" width = "270" height = "400" alt = "Nexus One" /> |
12 | < img id = "nokia" src = "img/phones/nokia.png" width = "270" height = "400" alt = "Nokia" /> |
13 | < img id = "blackberry" src = "img/phones/blackberry.png" width = "270" height = "400" alt = "BlackBerry" /> |
17 | < img class = "availableAppStore" src = "img/available_on_the_appstore.png" width = "230" height = "80" alt = "Available on the Appstore" /> |
20 | < h3 >< img src = "img/thumb.png" alt = "MobileApp" width = "114" height = "114" class = "thumb" />A wonderful apph3 > |
21 | < p >Lorem ipsum dolor sit amet.. p > |
25 | < h3 >< img src = "img/thumb.png" alt = "MobileApp" width = "114" height = "114" class = "thumb" />More awesome factsh3 > |
26 | < p >Lorem ipsum dolor sit amet.. p > |
This is all the markup that is used to display the website. The h1 heading holds a hyperlink which is styled as the logo (the logo image is set as the background of the hyperlink and a negative text indent is used to hide the text of the link).
After this we have the
#phoneCarousel div and inside it are the arrows and the stage. The phone images inside the stage are rotated with jQuery as you will see in a moment.
Lastly we have the
Available on the Appstore badge, and two blocks of text.
Slick MobileApp Website - Slideshow
Step 2 – CSS
CSS is responsible for converting our semantic markup into a true website. Take a closer look at the
#stage styles in the second part of the code, as these are what make the animation possible.
styles.css – Part 1
04 | background : url ( 'img/bg.png' ) repeat-x #f6f8f9 ; |
05 | font-family : 'Myriad Pro' , Arial , Helvetica , sans-serif ; |
08 | #logoh 1 { margin : 40px 0 0 ;} |
15 | background : url ( 'img/bokeh.jpg' ) no-repeat 0 120px ; |
24 | margin : 90px auto 120px ; |
33 | background : url ( 'img/arrows.png' ) no-repeat ; |
43 | background-position : right top ; |
50 | #phoneCarousel .arrow:hover{ |
51 | background-position : left bottom ; |
54 | #phoneCarousel .next:hover{ |
55 | background-position : right bottom ; |
After defining the body styles we can move on to styling the
#page div, which holds everything together. Its
background image is offset 120px vertically, so it matches the background of the body, filling the full width of the page.
Next is the
#phoneCarousel div. It has a relative positioning applied, so the stage (where all the animations take place) can be
properly centered. The previous/next arrows are styled as well.
styles.css – Part 2
02 | background : url ( 'img/logo.png' ) no-repeat ; |
32 | #stage .animationReady{ |
40 | .text{ margin-top : 70px ; width : 700px ;} |
49 | .text h 3 { font-size : 30px ;} |
50 | .text p{ font-size : 20px ;} |
52 | .thumb{ float : left ; margin-right : 40px ;} |
54 | .availableAppStore{ float : right ;} |
In the second part of the stylesheet, we continue with the
#stage styles. The phone pictures are hidden by default, so if JavaScript is disabled, the user is not left with a bunch of scattered images.
As you will see in the next step, the animation is achieved through changing the top and left CSS properties. For this to work, the images must be absolutely positioned. This is why the
.animatonReady class is assigned on load with jQuery (if JS is disabled, this style would not be applied).
Lastly we style the text blocks, which explain details about our fictional MobileApp.
Text Blocks
Step 3 – jQuery
When you click on one of the arrows, an animation starts, which uses sine and cosine calculations to move and scale down the images, creating the illusion of a circular movement. It is not as complicated as it sounds, as you can see for yourself from the code below.
script.js
01 | $(document).ready( function (){ |
07 | var images = $( '#stage img' ).removeClass( 'default' ).addClass( 'animationReady' ); |
08 | var dim = { width:images.width(),height:images.height()}; |
10 | var cnt = images.length; |
13 | var centerX = $( '#stage' ).width()/2; |
14 | var centerY = $( '#stage' ).height()/2 - dim.height/2; |
16 | function rotate(step,total){ |
23 | var eSin,eCos,newWidth,newHeight,q; |
30 | q = ((360/cnt)*i+deg)*Math.PI/180; |
40 | newWidth = q*dim.width; |
41 | newHeight = q*dim.height; |
53 | top : centerY+15*eSin, |
54 | left : centerX+200*eCos, |
55 | opacity : 0.8+eSin*0.2, |
56 | marginLeft : -newWidth/2, |
57 | zIndex : Math.round(80+eSin*20) |
58 | }).width(newWidth).height(newHeight); |
61 | total-=Math.abs(step); |
62 | if (total<=0) return false ; |
65 | setTimeout( function (){rotate(step,total)},40); |
72 | $( '#phoneCarousel .previous' ).click( function (){ |
77 | $( '#phoneCarousel .next' ).click( function (){ |
To start an animation you just need to call the
rotate function with two arguments – a step, and a total rotation, both of which are numbers. Step can be negative, which would mean that the rotation is run in the opposite way. Each time the function is run, total is decremented with the absolute value of the step, and once it reaches zero the animation is stopped.
In a number of places in this code, you can see that I’ve used a specific calculation –
360/cnt. This is done to distribute the phones evenly (360 being the number of degrees in a circle). This way you can add or remove images and they will be properly animated.
Slick MobileApp Website
With this our Slick MobleApp Website is complete!
Wrapping it up
Today we made a complete jQuery & CSS website for a fictional mobile application. You are free to modify the code and use it any way you see fit. If you liked the tutorial be sure to subscribe to our
RSS feed,
follow us on twitter, or leave a comment in the section below.
Postado por Fernando Schimit - Fox Creative