Placed in: Home arrow Programming arrow The iPhone unlock screen in xHTML, CSS and jQuery
The iPhone unlock screen in xHTML, CSS and jQuery

The iPhone: Everybody knows what it is, many people "played around" with the gadget and most people love it. I also own one of these amazing smartphones, and the looks of the software is really, really sleek and innovative (Just like we're used from Apple).

I wanted to transfer (some) of these amazing designs to a webpage to re-create the same look and feel for webbrowsers. Today, I'm going to show you how to create The iPhone unlock screen in xHTML, CSS and jQuery.

The iPhone unlock screen in xHTML, CSS and jQuery

OK, maybe not fully the "feeling" (look and feel) from the iPhone, since I can't make any webbrowser react to placing your finger on the screen, but your mouse will do the trick.

Demo The iPhone unlock screen in xHTML, CSS and jQuery   Download The iPhone unlock screen in xHTML, CSS and jQuery

Features:

  • XHTML and CSS valid.
  • "Timer" displays the current time (Just like the iPhone).
  • "Date" displays the current date (Just like the iPhone).
  • Pretty sleek interface, including see-through elements (Just like the iPhone).
  • Changeable background.
  • Tested and working on Firefox 3, Internet Explorer 7 and Safari 3.

Known issues

  • The "slider" doesn't slide back (what the iPhone does).
  • The "unlock animation" isn't exactly the same as the iPhone.
  • Doesn't work on an actual iPhone.

Other than those minor issues, it works as expected. I'm planning to make some more of these "iPhone style" webpages, so subscribe to the feed if you want to keep updated. Want to know how I created it? Check it out.

Resources

Before I started, I needed some reference material to work with.

  • For the beautiful background image, I took a wallpaper from SocWall.
  • For the high resolution iPhone layout, Teehan+lax helpt me out.
  • I wanted to use the Slider component from jQuery UI.
  • I wanted to use jQuery for the functionality. The last version (v. 1.3.0) isn't compatible (yet) with the slider component, and therefor I'm using v. 1.2.6.
  • Using the jQuery slider component was pretty hard, Ryan helped me out a lot here.
  • To display the current date and time with JavaScript, I used WebDevelopersNotes as a resource.

With all resources set, on to the next step.

xHTML

When you look in the source of the page, you'll see that the xHTML used isn't very complex.

 
<div id="iphone-scrollcontainer">
   <div id="iphone-inside">
      <div id="unlock-top">
         <p id="timepicker" class="time">08:23</p>
         <p id="datepicker" class="date">Wednesday, July 6</p>
      </div>
      <div id="unlock-spacer">
         &nbsp;
      </div>
      <div id="unlock-bottom">
         <div id="unlock-slider-wrapper">
            <div id="unlock-slider">
               <div id="unlock-handle"></div>
            </div>
         </div>
       </div>
    </div>
</div>
  • #iphone-scrollcontainer will be the overall container that holds all other elements.
  • #iphone-inside will have the same width & height as the container, but will be used to place the background image that'll fade out.
  • #unlock-top is used for the top of the unlock screen, holding the time and date.
  • #timepicker & &datepicker; will be used to change into the current time and date.
  • #unlock-spacer is needed to push the bottom down.
  • #unlock-bottom is the place for the bottom.
  • #unlock-slider-wrapper is the container for the slider.
  • #unlock-slider is the place where the handle can slide, used by the jQuery slider.
  • #unlock-handle needed by the jQuery slider component: The actual handle for the slider.

CSS

Now for the CSS part. We're going to use absolute positioning for this since the iPhone container will not resize. I'm only going to explain some things (not all) of the CSS file.

 
#iphone-scrollcontainer {
   height:461px;
   width:320px;
   position:absolute;
   top:140px;
   left:40px;
   background-color:#000000;
}
 
#iphone-inside {
   height:100%;
   width:100%;
   background-image:url(../images/background.png);
}
 
#unlock-spacer {
   height:272px;
}
  • #iphone-scrollcontainer has a size of 461 by 320 px. It is asbsolute positioned from the top left corner of the page, moved 140px down and 40 px to the right. The container has a black (#000000) background color.
  • #iphone-inside holds the backround image (background.png). The reason why this isn't placed in the scollcontainer, is because the inside will be faded out by jQuery and the black background from the scrollcontainer will remain.
  • #unlock-spacer has a height of 272px, because the total height is 461px. From this total height, the top (95px) and the bottom (94px) needs to be substracted, leaving 272px for the spacer.

JavaScript

At this point, we already have a pretty nice interface. For some interaction, we'll need to add some JavaScript. Like I said, I used the 1.2.6 version of jQuery to stay compatible with the Slider component. After including jquery-1.2.6.min.js, ui.core-1.5.3.js and ui.slider.js I created a new file called iphone-unlock.js with the following (Not the complete code):

 
$(document).ready(function()
{
   // Set the slider to be sliding
   $("#unlock-slider").slider({
   handle: "#unlock-handle",
   animate:true,
   stop: function(e,ui)
   {
      if($("#unlock-handle").position().left == 205)
      {
         unlock();
      }
      else
      {
         // Should slide back, but can't create it
         // Reason: When calling "$("#unlock-slider").slider("moveTo", 0);"
         // a loop is created, causing to crash the page
      }
   }});
   
   var unlock = function()
   {
      $("#unlock-bottom").fadeOut("normal", function(){
         $("#unlock-top").slideUp("fast", function(){
            $("#iphone-inside").fadeOut("slow", function(){
               window.location="index.html";  
        });  
      });                  
    });
  }
});
  • handle: tells the Slider component that the node is used as the handle.
  • animate: is set to true so when users click in the slider, it'll smootly animate to that position.
  • stop: is the function that's called when the slider stops. At that point, the slider checks if it's on his end (205px from the left). If so, it unlocks. If not, ht should slide back, but I can't create this effect (See the comments). If anyone knows a work-around for this, please let me know.
  • unlock function first fadeOuts the bottom, afster that it slides up the top and last it fades out the background image. Than it re-directs to another page, but in this case it's the same page.

All other code should be self-explanatory.

Conclusion and Download

Looking back, I think this is one pretty cool way of using the jQuery framework. With the sleek interface of the iPhone, what's not to like? Although not exactly everything is the same as the gadget, it still manages to get the iPhone feeling in your webbrowser.

Demo The iPhone unlock screen in xHTML, CSS and jQuery   Download The iPhone unlock screen in xHTML, CSS and jQuery

That's it! Hoped you learned something new today and start loving the jQuery framework (like I do). Be prepared to expect more jQuery / CSS / xHTML iPhone UI articles in the future.

Update

Martin provided a fix for the "not sliding back". He also made some adjustments to the code to make it look more like an actual iPhone, like the glowing slider and the same effect.

Thanks! I've updated the demo page and download package.


Tags:  iphone mac screen jquery xhtml css

Interested in this topic? You might enjoy another article I've written called

Did you like this article? Subscribe to my feed or email to keep updated on new articles.

Spread the word and submit to:
Digg!Reddit!Del.icio.us!Facebook!Technorati!StumbleUpon!Newsvine!Furl!Ma.gnolia!
Comments
Add NewSearchRSS
Garrett W.   2009-01-17 11:17:05
Gravatar image found another issue or two. if you click at the right-most spot on the bar, the slider moves there but does not activate the unlock like it should.
but more importantly, i suppose: the real iphone does not slide to whatever position you click. but i'm sure you knew that.
i don't know any jQuery, but i'm wondering if it provides all that you'd need to recreate the slider perfectly. for example, some simple mouseup and mousedown triggers would be useful, in the case that either (1) you lift your finger without completing the entire slide, or (2) you complete the slide without lifting your finger, in which case it should still unlock.
Marco   2009-01-17 11:29:03
Gravatar image Hi Garret,

Thank you for your comment.

The reason why the slider doesn't "unlock" when you click the right-most spot on the bar, is because the "animate" is set to "true". The "stop" variable (for a strange reason) get's triggered when clicked which means jQuery thinks that the left position is still zero, eventhough it's going to move. When "animate" is removed (changing it back to the default "false" ), clicking the right-most point will make it work.

In this example, I wanted to use (and explain) the work of the UI Slider. To create a better unlock screen, you should actually work with the "jQuery UI Draggables", which provide you with more flexibility to actually drag items around (which is what you want). In this example, I wanted to show how to actually slide the slider.

Maybe I'll refactor the code some day and make it more "sliding" like the actual phone ;) . Comments like yours support me to do so!
Sarah   2009-01-17 12:37:04
Gravatar image That is really really nice, :)

Source download isn't working for me though, can't wait to get my hands on it!
Marco - Download page   2009-01-17 12:39:16
Gravatar image Hi Sarah, thank you for your compliment!

Strange that you can't get the download working :( . Try visiting the "download details" page for this download and try the download button there.

If it still doesn't work, please contact me (use contact page) and I'll send you the source files myself :) .
Ole   2009-01-17 18:33:57
Gravatar image Just wanted to day that is also works perfectly in Opera 9.6 ^^
Marco   2009-01-17 20:27:23
Gravatar image Hi Ole, thanks for letting me know! Hope you enjoyed it ;) .

Greetings,,,
Liam Best - Slide back?   2009-01-19 09:35:46
Gravatar image Hey mate, regarding your "Can't slide back due to loop" error, try an if like this;
Quote:

i f ( $( " # u n l o c k - h a n d l e " ) . p o s i t i o n ( ) . l e f t ! = 0 ) {
// Slide back function
}
Martin   2009-01-19 15:08:13
Gravatar image This works like a charm :cheer:
Quote:

if($("#unlock-handle";).position().left == 205)
{
unlock();
}
else
{
$("#unlock-handle";).animate({left: 0}, 200 );
}
Marco - Awesome! Thanks!   2009-01-19 15:25:31
Gravatar image Damn Martin, thanks for that! I was so blinded by the slider component and it's functionality that I didn't thought the simple jQuery "animate" function would do the trick too.

I've changed the demo / download package with the modified code and I've added your credit :).

Once again, thank you for helping me (and others) out here! Still learning every day ;) .
Xander   2009-01-19 14:45:37
Gravatar image Nice work, it does look pretty neat. It's be good to see it automatically swing back, shame that as you said, it seems to be looping something.
Might also be worth adding a handle that when the slider is moved for the first time the text fades out. It's probably also possible to create a variable that fades it in and out depending on the distance moved, but I can imagine that being pretty memory intensive.
Martin   2009-01-19 14:57:13
Gravatar image @Xander:
I've got the opacity and swing bang working now. ill post an demo on my site soon.
if its OK with the original author?

//Martin
Gaya - Good job   2009-01-19 16:39:17
Gravatar image Nice one Marco.
Good to see that everyone is helping you improve the source.
Josh   2009-01-19 21:29:15
Gravatar image It's also working in Opera 9.63 - Windows with one exception:
after firstly sliding back (not going to the end - right) you need to click somewhere else to slide it again.
travis - linking   2009-01-20 22:41:07
Gravatar image anyone know how to link to another page after you "unlock the phone"? can you set the code so the slide acts like clicking a link?
Marco   2009-01-21 09:42:56
Gravatar image When you open up the "/iphone-unlock.js", you'll find the following code:
Quote:
{window.location="index.html";});

Simply change the "index.html" to something else and you're ready to go :).

Good luck!
Travis   2009-01-21 15:20:52
Gravatar image Thanks, works perfectly. I must have skipped right over that
Dainis Graveris - Great idea   2009-01-20 22:47:32
Gravatar image Huh, thanks for idea and work, I didn't have any idea how to create something like this before, looks hot :)
Ryan - Is it possible to make an i-frame within the iphon   2009-01-21 16:41:44
Gravatar image Hello guys ....

Is it possible to make an i-frame within the iphone? if so , how?

I wana link it to other sites etc.

instead of after unlocking going to the page you want ...

thanks
Marco - iframe?   2009-01-21 17:53:47
Gravatar image You could remove all elements inside the "iphone-scrollcontainer" nodes which than leaves you with only the iPhone background. Place your iframe inside that container, give it a src & add the correct width & height and that should do the trick.

Good luck!
Ryan - How to centre align the iphone?   2009-01-21 16:46:05
Gravatar image How to centre align the iphone?
Marco   2009-01-21 17:54:54
Gravatar image Currently, the iPhone has been set on a fixed position. If you want to have it center-aligned, you should change the CSS file and make the "margin-left" and "margin-right" from the container set to "auto". I'm not sure if everything will comply since some are set to a absolute position.

Good luck!
dlv - dlv   2009-01-22 18:35:35
Gravatar image really nice, good implemented and ispiring
thanks for share this work in this tutorial
pearible - pearible actual   2009-02-03 20:49:29
Gravatar image Slider doesn't appear to work in IE7 today
Cosmin - Not working in IE7   2009-02-12 10:35:22
Gravatar image Unfortunately, it's not working under IE7 :(
Marko - load   2009-02-22 22:14:08
Gravatar image Is possible to load swf in side of iphone (screen) after unlock?
vaQ - IE7   2009-03-03 02:37:43
Gravatar image Hello everybody,

is there any idea how to fix it in ie7 ... still not working :(
cihat - sohbet   2009-03-04 20:39:40
Gravatar image Hi good work thanks sohbet
kev - iphone punker   2009-03-09 10:34:42
Gravatar image can you link the slider to a html website as a index page and how do you do it
kind lost :s
Muhal   2009-07-09 15:20:15
Gravatar image Got the same problem :( When i put the window.location part under the line where it was originally (-> line #40 i think), it redirects, but without waiting for fadeout effect done...
Almost - But not quite   2009-05-18 20:19:25
Gravatar image Great looking script. Doesn't work with latest versions of jQuery and UI though :(
Don Cheng - Not workin on the old IE6 too   2009-07-09 21:36:05
Gravatar image Cool implementation! But it is not working on the old IE6 too........oh and one thing..........on FireFox once I slided the unlock button to the right, the rainbow screen disappeared for about half a second, then the rainbow screen returned after I saw the black background under it for that amount of time.........how to make the rainbow screen disappear forever once I unlocked the iphone please?
Don Cheng - A fix for IE!   2009-07-15 12:58:44
Gravatar image :!: A fix for IE! In style.css, change z-index: 1; to z-index: 0; for the DIV #slide-to-unlock...........yet I don't know why the DIV #unlock-handle with z-index: 10; will be overlapped by the DIV #slide-to-unlock...................... :!:
sohbet - sohbet   2009-08-13 16:48:21
Gravatar image Subject to very good, but thank you very much for help
Sohpet - sohpet   2009-08-18 03:10:35
Gravatar image Cool implementation! But it is not working on the old IE6 too........oh and one thing..........on FireFox once I slided the unlock button to the right, the rainbow screen disappeared for about half a second, then the rainbow screen returned after I saw the black background under it for that amount of time.........how to make the rainbow screen disappear forever once I unlocked the iphone please?
?? this
Sohbet - sohbet   2009-08-18 03:11:31
Gravatar image thank you my admin
Fabian - Text Effect   2009-08-25 19:27:39
Gravatar image Hello Marco,

Well done. I just have one question.
I can't find in the source, where you created the effect over the text ("slide to unlock";), that glows.

I was thinking, making a transparent png of gif that you can place on top (because you can't change the opacity in a certain part of a div, right?), but how did you do it (or did I miss something)?

Greetings, Fabian
dotBrilliance web development
Marco - Thanks!   2009-08-25 19:30:53
Gravatar image First off: Thanks for the comment Fabian. I translated it into English so all the other readers can understand what you're saying.

Anyway, the text is just an animated GIF created with Photoshop - I don't think there is any other way on how you can create such an effect.

If you download the source, you'll be able to find the image somewhere there.

Good luck!
virdfel - WAW!   2009-08-25 16:19:15
Gravatar image nice idea!!, very cool
Kyle - Changing slider arrow   2009-09-04 07:18:30
Gravatar image Does anyone know anything about customizing the slider arrow? I had a friend that did it but I can't seem to get in touch with them or figure it out on my own. Thanks in advance for any support/replies!
gay chat - gay sohbet   2009-09-19 04:05:00
Gravatar image rainbow screen returned after I saw the black background under it for that amount of time.........how to make the rainbow screen disappear forever once I unlocked the iphone please
Cene Estetigi - Cene Estetik   2009-10-08 17:05:11
Gravatar image Nice Doc.... thanks... admin
lez sohbet - lez sohbet   2009-10-09 21:02:49
Gravatar image Thnks
PA - Does't work on the real iphone itsel   2009-10-23 11:49:31
Gravatar image This demo doesn't work on a real iphone. You can't really slide : you will only get a click, and the carret will not follow your finger...
Stranger   2009-11-03 12:29:04
Gravatar image Brilliant!!!
I have been looking for this for ages! :cheer:
Kartik - TheKGuy   2009-12-14 08:39:49
Gravatar image Thanks alot for this i've been searching a lot this thing rox!!
Antoine Guédès - Freak!   2009-12-21 15:59:11
Gravatar image Man, it's a crazy tutorial!
Thank you for it, I have a big idea to do. Thank you!
Anonymous - web   2009-12-23 07:20:10
Gravatar image Thank you for it, I have a big idea to do. Thank you!
seutje - ew   2009-12-23 15:48:35
Gravatar image this made me throw up my lunch :(

not only is it utterly useless in a practical sense, but the source is downright nasty, if ur gonna apply dodgy positioning on load, at least try to get it right and don't just slap some position:relative; here and some position:absolute; there...

seriously, this post made give up advocating best practice

Keep up spitting out crap, all hope for a clean future of the web is lost anyway
mani2umca - Thanks!   2010-01-07 07:51:40
Gravatar image Great and thanks a lot for the share!!! B)
webb - Re: The iPhone unlock screen   2010-01-16 05:08:36
Gravatar image This has given me an idea on how people will enter my site.
Read more...
Name:
Email:
  Gravatar enabled.
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Unsubscribe from e-mail notifications.
 
< Prev   Next >
Subscribe

If 4309 people are reading this site every day, why don't you?