分享

How I made an iPhone 4 in CSS3

 king9413 2014-09-23

In 19th December 2011 I published my creation, iPhone4 in CSS3, to my website. Since that time more then 400 000 unique visitors saw it, many of them like and share it. And now i want to tell how I made it from the stage of beginning to the first tweet about it's finished.

Challenge

The main idea of this iPhone was a challenge. One of my coworkers give mi a link with iOS Icons made in Pure CSS and after I saw what was there I decided to create something cool like that or even cooler. My brain decide - "Challenge excepted!". And it was the beginning.

Main task

That day was born an idea to create not only icons, but whole iPhone in CSS3 (with no images, SVG, base64, canvas at all) and give a life to it with some interaction:

  • turning on and off like in a real iPhone;
  • working "Slide to unlock" slider;
  • close to real "slide to unlock" animation;

Technologies

CSS3

Main interesting CSS3 properties that helps me to paint (to design) the iPhone:

  • border-radius for rounding corners of almost all elements;
  • box-shadow for creating shadows and inner glows;
  • background-size for creating ribbed background in 'Message' and 'Phone' icons;
  • transform (rotate, skew, scale) - transforming some parts of icons, it helps to create icon details close to real pictures;
  • ::before and ::after elements helps minimize HTML code elements;
  • :nth-child(n) helps to chose needed elements from the array;

jQuery + jQuery UI

From this technology I was using not to much interesting things. animate() function to create "slide to unlock" animation, for flying in and out icons when unblocking or blocking the device. draddable() + animate() function to enable finger dragging the slider.

Script style inherited from Html5BoilerPlate.

HTML

Html structure is very simple. I was not using many different tags. The main of them are 'div', 'hr', 'b' and lists. Everything important has been assigned to CSS3.

Parts of code

HTML structure of the iPhone

  1. <div class="iphone">
  2.  
  3. <div class="iphone_light_gradient"></div> <!-- glare on the iPhone -->
  4. <div class="iphone_power_button" id="iphone_power_button"></div> <!-- on/off button -->
  5. <div class="iphone_voice_toogle"></div> <!-- voice toogle -->
  6. <div class="iphone_voice_plus"></div> <!-- button "+" -->
  7. <div class="iphone_voice_minus"></div> <!-- button "-" -->
  8.  
  9. <div class="iphone_camera"></div> <!-- camera -->
  10.  
  11. <div class="iphone_dynamic"><span></span><!-- lots of 'span' for dynamic --><span></span></div> <!-- dynamic -->
  12. <div class="iphone_black_bg"></div> <!-- black background of front part of iPhone -->
  13.  
  14. <div class="iphone_display" > <!-- display -->
  15.  
  16. <div class="iphone_headline" id="iphone_headline"> <!-- top dark line -->
  17. <div class="iphone_net"></div> <!-- operator network -->
  18. <div class="iphone_net_title">tjrus</div> <!-- name of the phone network -->
  19. <div class="iphone_wi-fi"><div class="hack"></div></div> <!-- wi-fi indocator -->
  20. <div class="iphone_clock" id="iphone_headline_clock">00:00</div> <!-- clock -->
  21. <div class="iphone_lock"></div> <!-- lock -->
  22. <div class="iphone_battery"></div> <!-- battery -->
  23. </div>
  24. <div class="iphone_header" id="iphone_lock_header"> <!-- top panel of lock screen -->
  25. <div class="iphone_time" id="iphone_lock_time">0<span>:</span>00</div> <!-- clock -->
  26. <div class="iphone_date" id="iphone_lock_date"></div> <!-- date -->
  27. </div>
  28. <div class="iphone_footer" id="iphone_lock_footer"> <!-- bottom part of lock panel -->
  29. <div class="iphone_unlock" id="iphone_unlock"> <!-- "slide to unlock" -->
  30. <div class="iphone_slider" id="iphone_slider"></div> <!-- slider -->
  31. <div class="iphone_slide2unlock" id="iphone_slide2unlock">slide to unlock</div>
  32. </div>
  33. </div>
  34. <div class="iphone_icons_containter" id="iphone_icons_containter"> <!-- icons container -->
  35. <div class="icon"></div> <!-- example of icon container (there are 16 icons here) -->
  36. </div>
  37. <div class="iphone_dock" id="iphone_dock"> <!-- dock -->
  38. <div class="icon"></div> <!-- example of icon container (there are 4 icons here) -->
  39. </div>
  40. </div>
  41.  
  42. <div class="iphone_home" id="iphone_home_button"></div> <!-- home button -->
  43.  
  44. </div>

CSS3 interesting parts

There is no sense to give you full CSS3 code in 3395 lines. So here is interesting parts.

Icons

All icons are in container with .icon class. So I can easily change their order or simply delete some of them. And also can easily give them general style.

  1. .icon {
  2. width: 56px;
  3. height: 56px;
  4. border-radius: 10px;
  5. box-shadow: rgba(0,0,0,0.5) 0 1px 2px;
  6. margin-bottom: 30px;
  7. position: absolute;
  8. }
  9.  
  10. .icon span { /* contains icon title */
  11. display: block;
  12. text-align: center;
  13. font: bold 11px/15px "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
  14. color: #fff;
  15. text-shadow: rgba(0,0,0,0.3) 1px 2px 1px;
  16. text-transform: capitalize;
  17. position: absolute;
  18. top: 58px;
  19. left: -10px;
  20. width: 76px;
  21. }

We have the array or icons, so the insist way i find to put them on their places is:

  1. .icon:nth-child(4n + 1) { left:17px; }
  2. .icon:nth-child(4n + 2) { left:92px; }
  3. .icon:nth-child(4n + 3) { left:168px; }
  4. .icon:nth-child(4n + 4) { left:243px; }
  5.  
  6. .icon:nth-child(-n + 16) { top: 258px; }
  7. .icon:nth-child(-n + 12) { top: 172px; }
  8. .icon:nth-child(-n + 8) { top: 86px; }
  9. .icon:nth-child(-n + 4) { top: 0; }

in case of dock icons we have a little different situation:

  1. .iphone_dock .icon:nth-child(1) { margin-left:7px; margin-right: 19px; }
  2. .iphone_dock .icon:nth-child(2) { margin-right: 20px; }
  3. .iphone_dock .icon:nth-child(3) { margin-right: 19px; }

If you are interested in full icons code you can find it in main styles of that project

jQuery interesting parts

As I already told I inherit script style from Html5BoilerPlate. All options, possibilities and sattes of the iPhone a structured in one object:

  1. iphone = {
  2. slide_started : false, // is slide started
  3. letter_animate_time : 50, // time of letter animation
  4. panels_animate_time : 400, // panels animation time
  5. status : 'lock', // Current iPhone status. available statuses: "lock", "unlock", "off"
  6. iconsDefaultPosition : {}, // default icon positions
  7. iconsOutPosition : {}, // icons out positions
  8.  
  9. init : function(){}, // initialize all functionality
  10.  
  11. endSlide : function(){}, // function that processes the end of slide action
  12.  
  13. turnOn : function(){}, // turning iPhone on
  14. turnOff : function(){}, // turning iPhone off
  15. lock : function(){}, // locking iPhone
  16. unlock : function(){}, // unlocking iPhone
  17.  
  18. showIcons : function(){}, // icons incoming animation
  19.  
  20. hideIcons : function(){}, // icons hiding
  21.  
  22. animateHideIcons : function(){}, // animated icons hiding
  23.  
  24. prepareIcons : function(){}, // gethering default icons position
  25.  
  26. timeUpdate : function(){}, // updating time on clocks
  27. stopTextAnimate : function(){}, // stops the text animation
  28. startTextAnimate : function(){}, // starting text animation
  29. prepareTextAnimate : function () {}, // prepating "slide to unlock" for animation
  30. animateLetters : function() {}, // start of animation cycle in "slide to unlock"
  31.  
  32. ua : function() {}, // user agent identification
  33. }

Treatment "slide to unlock" process
  1. $("#iphone_slider").draggable({
  2. containment: 'parent',
  3. axis: 'x',
  4. start: function(event, ui) {
  5. $(document).mousemove(function(){
  6. if(iphone.slide_started){
  7. var left = $("#iphone_slider").css('left').substring(0, $("#iphone_slider").css('left').length - 2);
  8. var width = $('#iphone_unlock').width() - $('#iphone_slider').width();
  9. var opacity_k = (width - left*3) / (width);
  10. $('#iphone_slide2unlock').css({'opacity': opacity_k}, TIME/2); // controls the text opacity while sliding
  11. }
  12. });
  13. },
  14. stop: function(event, ui){
  15. $(document).unbind('mousemove');
  16. }
  17. });
  18. $("#iphone_slider").mousedown(function(){
  19. iphone.slide_started = true;
  20. });
  21. $(document).mouseup(function(){
  22. if (iphone.slide_started){
  23. iphone.endSlide(); // here we find out how far users slide the slider to unlock iPhone or return to starting position
  24. iphone.slide_started = false;
  25. }
  26. });

The result

The result page shows the many excellent, but not all CSS3 possibilities. It a pity but all this code can't be used on websites because of the huge complex of CSS3 properties slows browser too much.

Cross-browser compatibility

This is the most sad point:). The iPhone 4 in CSS3 working great in latest Webkit browsers (Safari and Chrome) under MacOS X (in Windows family there is a trouble with 'Phone' icon), also in latest Firefox and Opera browsers.

For iOS devices the animation of 'slide to unlock' letters was disabled due to the fact that this strongly slow down the browser.

Demo

The main page - iPhone 4 in CSS3

styles — iphone.css

script — iphone.js

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约