ניסוי: הדמיית iOS7 עם HTML ו-CSS בלבד
ניסוי בפיתוח: לדמות את מערכת ההפעלה החדשה של אפל באמצעות HTML ו-CSS בלבד.
מטרת הניסוי
לדמות את מערכת ההפעלה החדשה iOS7 באמצעות HTML ו-CSS בלבד.
הציוד הדרוש
רקע
מה זה Sass?
טכנולוגיה המאפשרת ליצור ולנהל CSS בנפחים גדולים, על-ידי שיטות הלקוחות מעולם התיכנות כמו:
- הפרדת עניינים (Soc – Separation of Concerns) –על-ידי פיזור הקוד בקבצים שונים.
- אל תחזרו על עצמכם (DRY – Don't Repeat Yourself) –על-ידי שמירת בלוקים של CSS לשימוש חוזר בחלקים שונים.
מהו התחביר לכתיבת Sass?
התחביר דומה ל-CSS, בתוספת מרכיבים כמו: פונקציות, משתנים וקינון אלמנטים.
כיצד משתמשים ב-Sass?
לאחר כתיבת התחביר, מבצעים הידור (Compilation) אשר בין השאר:
- בודק שגיאות תחביר.
- ממיר את הקוד ל-CSS .
- מאחד בין קבצים ומייצר מהם קובץ/י CSS.
אם כן, התוצאה הסופית היא קובץ CSS רגיל, אלא שמאחורי הקלעים הקוד מיוצר דינמית.
כיצדמתקיניםSass?
הוראות התקנהכאן.
כיצדמבצעים הידור (Compile)של קוד Sass?
- נתקיןNode.js.
- ניצור 2 קבצי הגדרות בתיקיית השורש:
package.json
[javascript]
{
"name": "ios7-html",
"version": "0.0.1",
"devDependencies": {
"grunt": "0.4.1",
"grunt-contrib-watch": "0.4.3",
"grunt-contrib-sass": "0.4.0"
}
}
[/javascript]Gruntfile.js
[javascript]
module.exports = function (grunt) {
grunt.initConfig({
watch: {
sass: {
tasks: ['sass:dist'],
files: ['sass/**/*.{scss,sass}']
}
},
sass: {
dist: {
options: {
style: 'expanded'
},
files: {
'css/style.css': 'sass/style.scss'
}
}
}
}); grunt.registerTask('default', ['watch']);
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
};
[/javascript] - במסוף (Terminal) נריץ 3 פקודות:
[javascript]
$ npm install -g grunt-cli
[/javascript][javascript]
$ npm install
[/javascript][javascript]
$ grunt
[/javascript]נשאיר את המסוף פועל.
בהתאם לקובץ ההגדרות, Grunt מפעיל פלאגין בשם watch,אשר מהדר את קבצי ה-Sass מחדשבכל-פעם שמתבצע בהם שינוי.
כיצדמגדיריםמשתנים ב-Sass?
scss (לפני הידור)
[css]
$body_width: 800px;
body {
width: $body_width;
}
[/css]
css (לאחר הידור)
[css]
body {
width: 800px;
}
[/css]
כיצד כותביםפונקציות?
scss
[css]
@function getTenPixels() {
@return 10px;
}
img {
width: getTenPixels();
}
[/css]
css
[css]
img {
width: 10px
}
[/css]
כיצד עושיםשימוש חוזרבסט תכונות?
scss
[css]
%header {
font-size: 48px;
text-decoration: underline;
}
h1{
@extend %header;
color: blue;
}
[/css]
css
[css]
h1 {
font-size: 48px;
text-decoration: underline;
color: blue;
}
[/css]
כיצד מבצעיםחישובים מתמטייםב-Sass?
scss
[css]
body {
margin: $body_width / 10;
}
[/css]
css
[css]
body {
margin: 80px;
}
[/css]
חישובים אפשריים:
+ חיבור
– חיסור
* כפל
/ חילוק
% שארית
כיצד כותבים תכונות מקוננות (אחת בתוך השנייה) ב-Sass?
אם ב-CSS היינו כותבים את מסלול האלמנט בכל הגדרה, למשל:
[css]
.top .network li {…}
.top .network:after {…}
[/css]
הרי שב-Sass ניתן להגדיר תכונות של אלמנטים, בתוך אלמנטים אחרים. לדוגמה:
[css]
.top {
.network {
li {
…
}
}
}
[/css]
הוספת & לפני שם האלמנט, תחליף אותו עם שם האלמנט. לדוגמה:
[css]
.network {
&:after {
…
}
}
[/css]
יהפוך לאחר ההידור ל-:
[css]
.network:after {
…
}
[/css]
מהלך הניסוי:
מבנה התיקיות:
. ├── css ├── style.css ├── sass ├── device.scss ├── global.scss ├── screen.scss └── style.scss ├── Gruntfile.js ├── package.json └── index.html
index.html
נקשר את הדף ל-CSS שמתקבל כתוצאה מהידור קבצי ה-scss.
[html]
…
[/html]
ניצור איזורים שיכללו את מרכיבי הטלפון השונים:
- מכשיר
- מסך
- חלק עליון
- חלק תחתון
[html]
…
[/html]
נוסיף לחלק העליון את השעה, אחוז הסוללה ומצב הקליטה:
[html]
[/html]
ניצור איזורים שיכללו 24 אפליקציות.
על-מנת לעצב כל אפליקציה בצורה שונה, האפליקציות תהיינה ממוספרות ב-Class שלהן:
[html]
// Continue with this pattern until 'application24'
…
[/html]
style.scss
זהו קובץ המאחד את שאר קבצי ה-scss.
תזכורת: ב-Gruntfile.js הגדרנו שהקובץ הזה יהודר ל-style.css (שה-HTML מקושר אליו).
- global.scss – הגדרות CSS כלליות
- device.scss – עיצוב המכשיר.
- screen.scss – עיצוב המסך.
[css]
@import "global";
@import "device";
@import "screen";
[/css]
global.scss
בקובץ זה אין תחביר Sass.
הסיבה ליצירתו היא הרצון שהוא יהודר ביחד עם האחרים לקובץ css.
[css]
html{
-webkit-font-smoothing: antialiased;
}
body {
text-align: center;
font: bold 12px arial;
color: #fff;
background: #27657F;
}
[/css]
device.scss
נגדירכמשתנה, את רוחב המכשיר:
[css]
$iphone_width: 284px;
[/css]
נעצב בעזרתתכונות מקוננותאת החלק העליון של המכשיר ומרכיביו:
[css]
.top {
div {
position: absolute;
top: 0.5%;
}
.network {
left: 7%;
margin-top: 0.2%;
font-size: 0.8em;
li{
float: left;
margin-right: 0.5em;
font-size: 1.7em;
line-height: 65%;
}
&:after {
position: absolute;
left: 65%;
content: "OPERATOR";
font-weight: normal;
}
}
.clock {
left: 45%;
}
.battery {
right: 5%;
}
}
[/css]
נעצב את גוף המכשיר:
[css]
.iphone {
$shadow_color: #e6e6e6;
margin: 0 auto;
width: $iphone_width;
height: 100%;
overflow: hidden;
border-radius: $iphone_width / 10;
background: #ccc;
border: $iphone_width / 100 solid $shadow_color;
&:after {
content: "\2610";
text-align: center;
font-size: 2em;
color: $shadow_color;
line-height: 150%;
display: block;
background: #fff;
width: 36px;
height: 36px;
border-radius: 50%;
margin: 0 auto;
position: relative;
top: -7px;
box-shadow: inset 0 0 20px $shadow_color;
}
}
[/css]
נעצב את החלק התחתון של המכשיר:
[css]
.bottom_bg {
position: absolute;
bottom: 0;
width: 100%;
height: 16%;
background: rgba(255,255,255,0.5);
}
[/css]
screen.scss
[css]
.screen {
position: relative;
overflow: hidden;
width: $iphone_width – 22;
margin-bottom: -35px;
padding: 7px 0;
border-width: 50px 10px;
border-color: #fff;
border-style: solid;
background: -webkit-linear-gradient(to bottom, #244279 0%, #6fa2b8 19%, #6fa2b8 30%, #7788c0 59%, #6b7ebf 73%, #40759d 100%);
background: -moz-linear-gradient(to bottom, #244279 0%, #6fa2b8 19%, #6fa2b8 30%, #7788c0 59%, #6b7ebf 73%, #40759d 100%);
background: -ms-linear-gradient(to bottom, #244279 0%, #6fa2b8 19%, #6fa2b8 30%, #7788c0 59%, #6b7ebf 73%, #40759d 100%);
background: -o-linear-gradient(to bottom, #244279 0%, #6fa2b8 19%, #6fa2b8 30%, #7788c0 59%, #6b7ebf 73%, #40759d 100%);
background: linear-gradient(to bottom, #244279 0%, #6fa2b8 19%, #6fa2b8 30%, #7788c0 59%, #6b7ebf 73%, #40759d 100%);
}
[/css]
ניצורסט תכונות לשימוש חוזרעבור האייקונים:
[css]
%icon {
$icon_size: 50px;
position: relative;
width : $icon_size;
height : $icon_size;
margin : $icon_size / 5 $icon_size / 10 $icon_size / 20;
border-radius : $icon_size / 5;
-webkit-box-sizing : border-box;
-moz-box-sizing : border-box;
box-sizing : border-box;
box-shadow : 0 0 0 1px rgba(134, 254, 101, 0.1), 0 0 0 1px rgba(0, 0, 0, 0.05) inset;
}
[/css]
ניצורפונקציהשמחזירה ריווח עליון, בהתאם למיקום האפליקציה במסך.
לארבעת האפליקציות הראשונות ניתן ריווח של 10 פיקסלים.
לארבעת האחרונות ניתן ריווח של 5 פיקסלים.
[css]
@function getTopPaddingByItem($item) {
@if $item <=4 {
@return 10px;
}
@if $item >=21 {
@return 5px;
}
@return 0;
}
[/css]
ניצורמערך צבעיםעבור האייקונים (איברי המערך מופרדים ברווח):
[css]
$icon_colors_from: #ff2666 #333 #ffdb44 #fff #86fe65 #ff5b39;
[/css]
נגדיר לולאה בת4 הפעלותעל כל אחד מ-6 הצבעים שהגדרנו (סה"כ 24 הפעלות – אחת לכל אפליקציה):
[css]
$j: 1;
@for $i from 1 through 4{
@each $icon_color_from in $icon_colors_from {
…
}
}
[/css]
ע"מ לגוון מעט, נשנה את צבע האפליקציה בכל הפעלה של הלולאה, על-ידי 2 פונקציות Sass לשינוי צבעים:
- adjust-hue($color, $degrees)
- darken($color, $amount)
[css]
$j: 1;
@for $i from 1 through 4{
@each $icon_color_from in $icon_colors_from {
$icon_color_from: adjust-hue($icon_color_from, 90 * $j);
$icon_colors_to: darken($icon_color_from, 25%);
}
$j: $j + 1;
}
[/css]
נגדיר class בשם "application" ומספר בסדר עולה (זהו המונה j):
[css]
$j: 1;
@for $i from 1 through 4{
@each $icon_color_from in $icon_colors_from {
$icon_color_from: adjust-hue($icon_color_from, 90 * $j);
$icon_colors_to: darken($icon_color_from, 25%);
.application#{$j} {
padding-top: getTopPaddingByItem($j);
position: relative;
display: inline-block;
z-index: 1;
height: 70px;
}
}
$j: $j + 1;
}
[/css]
בתוך "application", נגדיר 2 classes:
- "icon" – שישתמש בסט ההגדרות %icon שהגדרנו מוקדם יותר.
- "symbol" – סמל האפליקציה, ממוקם במרכז הקוביה.
[css]
$j: 1;
@for $i from 1 through 4{
@each $icon_color_from in $icon_colors_from {
$icon_color_from: adjust-hue($icon_color_from, 90 * $j);
$icon_colors_to: darken($icon_color_from, 25%);
.application#{$j} {
padding-top: getTopPaddingByItem($j);
position: relative;
display: inline-block;
z-index: 1;
height: 70px;
.icon {
@extend %icon;
background: -webkit-linear-gradient(top, $icon_color_from 0%, $icon_colors_to 100%);
background: -moz-linear-gradient(top, $icon_color_from 0%, $icon_colors_to 100%);
background: -ms-linear-gradient(top, $icon_color_from 0%, $icon_colors_to 100%);
background: -o-linear-gradient(top, $icon_color_from 0%, $icon_colors_to 100%);
background: linear-gradient(top, $icon_color_from 0%, $icon_colors_to 100%);
}
.symbol {
position: absolute;
width: 26px;
height: 24px;
top: 50%;
left: 50%;
margin: -25% 0 0 -25%;
background: url(http://www.mcterano.com/blog/tutorials/404/images/symbol#{$j}.png) no-repeat;
}
}
$j: $j + 1;
}
}
[/css]
תוצאות הניסוי
צפו בהדמיית ה-HTML של iOS 7 (עובד בכרום, ספארי ופיירפוקס):
http://jsfiddle.net/mcterano/C8dmF/embedded/result,css,html















פאדיחה רצינית מה שיש לכם פה.
על שמעתם?
אוקיי, אז מסתבר שאפשר להכניס לכם תגיות כגון בתגובות.
טפלו בעניין הזה, ותתחילו להשתמש ב- במאמרים מהסוג הזה. :)
תגיות pre.
אלה התגיות שלא רואים בתגובות הקודמות שלי.
console.log('lol')