webkit開發,app移動前端知識點

來源:互聯網
上載者:User

標籤:des   style   http   io   ar   color   os   使用   sp   

如果你是一名前端er,又想在行動裝置上開發出自己的應用,那怎麼實現呢?幸好,webkit核心的瀏覽器能協助我們完成這一切。接觸 webkit webApp的開發已經有一段時間了,現把一些技巧分享給大家 :

1. viewport:
也就是可視地區。對於案頭瀏覽器,我們都很清楚viewport是什麼,就是出去了所有工具列、狀態列、捲軸等等之後用於看網頁的地區,
這是真正有效地區。由於行動裝置螢幕寬度不同於傳統web,因此我們需要改變viewport;

實際上我們可以操作的屬性有4 個:

width -             //  viewport 的寬度 (範圍從200 到10,000,預設為980 像素)height -            //  viewport 的高度 (範圍從223 到10,000)initial-scale -     //  初始的縮放比例 (範圍從>0 到10)minimum-scale -    //   允許使用者縮放到的最小比例maximum-scale -    //   允許使用者縮放到的最大比例user-scalable -    //   使用者是否可以手動縮 (no,yes)

那麼到底這些設定如何讓Safari 知道?其實很簡單,就一個meta,形如:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">   //編碼<meta id="viewport" name="viewport" content="width=320; initial-scale=1.0;maximum-scale=1.0; user-scalable=no;"/><meta name=”apple-mobile-web-app-capable” content=”yes” />  // 離線應用的另一個技巧<meta name=”apple-mobile-web-app-status-bar-style” content=black” />  // 隱藏狀態列<meta content="black" name="apple-mobile-web-app-status-bar-style" /> //指定的iphone中safari頂端的狀態條的樣式<meta content="telephone=no" name="format-detection" />       //告訴裝置忽略將頁面中的數字識別為電話號碼<meta name="Author" contect="Mr.He"/ >

在設定了initial-scale=1 之後,我們終於可以以1:1 的比例進行頁面設計了。關於viewport,還有一個很重要的概念是:iphone 的safari 瀏覽器完全沒有捲軸,而且不是簡單的“隱藏捲軸”,是根本沒有這個功能。iphone 的safari 瀏覽器實際上從一開始就完整顯示了這個網頁,然後用viewport 查看其中的一部分。當你用手指拖動時,其實拖的不是頁面,而是viewport。瀏覽器行為的改變不止是捲軸,互動事件也跟普通案頭不一樣。(請參考:指尖的下JS 系列文章)

2. link:

<link rel=”apple-touch-startup-image” href=”startup.png” /> // 設定開始頁面圖片<link rel=”apple-touch-icon” href=”iphon_tetris_icon.png”/> // 在設定書籤的時候可以顯示好看的表徵圖<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">    // 肖像模式樣式<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css"   // 風景模式樣式//豎屏時使用的樣式<style media="all and (orientation:portrait)" type="text/css">#landscape { display: none; }</style>//橫屏時使用的樣式<style media="all and (orientation:landscape)" type="text/css">#portrait { display: none; }</style>

3. 事件 : (請參考:指尖的下JS 系列文章)

// 手勢事件touchstart            //當手指接觸螢幕時觸發touchmove           //當已經接觸螢幕的手指開始移動後觸發touchend             //當手指離開螢幕時觸發touchcancel// 觸摸事件gesturestart          //當兩個手指接觸螢幕時觸發gesturechange      //當兩個手指接觸螢幕後開始移動時觸發gestureend// 旋轉螢幕事件onorientationchange// 檢測觸控螢幕幕的手指何時改變方向orientationchange// touch事件支援的相關屬性touchestargetToucheschangedTouchesclientX    // X coordinate of touch relative to the viewport (excludes scroll offset)clientY    // Y coordinate of touch relative to the viewport (excludes scroll offset)screenX    // Relative to the screenscreenY     // Relative to the screenpageX     // Relative to the full page (includes scrolling)pageY     // Relative to the full page (includes scrolling)target     // Node the touch event originated fromidentifier     // An identifying number, unique to each touch event

4. 旋轉螢幕事件:onorientationchange
添加旋轉螢幕事件偵聽,可隨時發現旋轉螢幕狀態(左旋、右旋還是沒旋)。例子:

// 判斷螢幕是否旋轉function orientationChange() {switch(window.orientation) {case 0:alert("肖像模式 0,screen-width: " + screen.width + "; screen-height:" + screen.height);break;case -90:alert("左旋 -90,screen-width: " + screen.width + "; screen-height:" + screen.height);break;case 90:alert("右旋 90,screen-width: " + screen.width + "; screen-height:" + screen.height);break;case 180:alert("風景模式 180,screen-width: " + screen.width + "; screen-height:" + screen.height);break;};<br>};// 添加事件監聽addEventListener(‘load‘, function(){orientationChange();window.onorientationchange = orientationChange;});

5. 隱藏地址欄 & 處理事件的時候,防止捲軸出現:

// 隱藏地址欄  & 處理事件的時候 ,防止捲軸出現addEventListener(‘load‘, function(){setTimeout(function(){ window.scrollTo(0, 1); }, 100);});

6. 雙手指滑動事件:

// 雙手指滑動事件addEventListener(‘load‘,  function(){ window.onmousewheel = twoFingerScroll;},false              // 相容各瀏覽器,表示在冒泡階段呼叫事件處理常式 (true 捕獲階段));function twoFingerScroll(ev) {var delta =ev.wheelDelta/120;              //對 delta 值進行判斷(比如正負) ,而後執行相應操作return true;};

7. 判斷是否為iPhone:

// 判斷是否為 iPhone :function isAppleMobile() {return (navigator.platform.indexOf(‘iPad‘) != -1);};

8. localStorage:
例子 :(注意資料名稱 n 要用引號引起來)

var v = localStorage.getItem(‘n‘) ? localStorage.getItem(‘n‘) : "";   // 如果名稱是  n 的資料存在 ,則將其讀出 ,賦予變數  v  。localStorage.setItem(‘n‘, v);                                           // 寫入名稱為 n、值為  v  的資料localStorage.removeItem(‘n‘);                                           // 刪除名稱為  n  的資料

9. 使用特殊連結:
如果你關閉自動識別後 ,又希望某些電話號碼能夠連結到 iPhone 的撥號功能 ,那麼可以通過這樣來聲明電話連結 ,

<a href="tel:12345654321">打電話給我</a><a href="sms:12345654321">發簡訊</a>或用於儲存格:<td onclick="location.href=‘tel:122‘">

10. 自動大寫與自動修正
要關閉這兩項功能,可以通過autocapitalize 與autocorrect 這兩個選項:

<input type="text" autocapitalize="off" autocorrect="off" />

11. WebKit CSS:
①“盒模型”的具體描述性質的包圍盒塊內容,包括邊界,填充等等。

-webkit-border-bottom-left-radius: radius;-webkit-border-top-left-radius: horizontal_radius vertical_radius;-webkit-border-radius: radius;      //容器圓角-webkit-box-sizing: sizing_model; 邊框常量值:border-box/content-box-webkit-box-shadow: hoff voff blur color; //容器陰影(參數分別為:水平X 方向位移量;垂直Y 方向位移量;高斯模糊半徑值;陰影顏色值)-webkit-margin-bottom-collapse: collapse_behavior; 常量值:collapse/discard/separate-webkit-margin-start: width;-webkit-padding-start: width;-webkit-border-image: url(borderimg.gif) 25 25 25 25 round/stretch round/stretch;-webkit-appearance: push-button;   //內建的CSS 表現,暫時只支援push-button

②“視覺格式化模型”描述性質,確定了位置和大小的塊元素。

direction: rtlunicode-bidi: bidi-override; 常量:bidi-override/embed/normal

③“視覺效果”描述屬性,調整的視覺效果塊內容,包括溢出行為,調整行為,能見度,動畫,變換,和過渡。

clip: rect(10px, 5px, 10px, 5px)resize: auto; 常量:auto/both/horizontal/none/verticalvisibility: visible; 常量: collapse/hidden/visible-webkit-transition: opacity 1s linear; 動畫效果 ease/linear/ease-in/ease-out/ease-in-out-webkit-backface-visibility: visibler; 常量:visible(預設值)/hidden-webkit-box-reflect: right 1px; 鏡向反轉-webkit-box-reflect: below 4px -webkit-gradient(linear, left top, left bottom,from(transparent), color-stop(0.5, transparent), to(white));-webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)));;   //CSS 遮罩/蒙板效果-webkit-mask-attachment: fixed; 常量:fixed/scroll-webkit-perspective: value; 常量:none(預設)-webkit-perspective-origin: left top;-webkit-transform: rotate(5deg);-webkit-transform-style: preserve-3d; 常量:flat/preserve-3d; (2D 與3D)

④“產生的內容,自動編號,並列出”描述屬性,允許您更改內容的一個組成部分,建立自動編號的章節和標題,和操縱的風格清單的內容。

content: “Item” counter(section) ” “;This resets the counter.First section>two sectionthree sectioncounter-increment: section 1;counter-reset: section;

⑤“分頁媒體”描述效能與外觀的屬性,控制印刷版本的網頁,如分頁符的行為。

page-break-after: auto; 常量:always/auto/avoid/left/rightpage-break-before: auto; 常量:always/auto/avoid/left/rightpage-break-inside: auto; 常量:auto/avoid

⑥“顏色和背景”描述屬性控制背景下的區塊層級元素和顏色的常值內容的組成部分。

-webkit-background-clip: content; 常量:border/content/padding/text-webkit-background-origin: padding; 常量:border/content/padding/text-webkit-background-size: 55px; 常量:length/length_x/length_y

⑦ “字型”的具體描述性質的文字字型的選擇範圍內的一個因素。報告還描述屬性用於下載字型定義。

unicode-range: U+00-FF, U+980-9FF;

⑧“文本”描述屬性的特定文字樣式,間距和自動滾屏。

text-shadow: #00FFFC 10px 10px 5px;text-transform: capitalize; 常量:capitalize/lowercase/none/uppercaseword-wrap: break-word; 常量:break-word/normal-webkit-marquee: right large infinite normal 10s; 常量:direction(方向) increment(迭代次數) repetition(重複) style(樣式) speed(速度);-webkit-marquee-direction: ahead/auto/backwards/down/forwards/left/reverse/right/up-webkit-marquee-incrementt: 1-n/infinite(無窮次)-webkit-marquee-speed: fast/normal/slow-webkit-marquee-style: alternate/none/scroll/slide-webkit-text-fill-color: #ff6600; 常量:capitalize, lowercase, none, uppercase-webkit-text-security: circle; 常量:circle/disc/none/square-webkit-text-size-adjust: none; 常量:auto/none;-webkit-text-stroke: 15px #fff;-webkit-line-break: after-white-space; 常量:normal/after-white-space-webkit-appearance: caps-lock-indicator;-webkit-nbsp-mode: space; 常量: normal/space-webkit-rtl-ordering: logical; 常量:visual/logical-webkit-user-drag: element; 常量:element/auto/none-webkit-user-modify: read- only; 常量:read-write-plaintext-only/read-write/read-only-webkit-user-select: text; 常量:text/auto/none

⑨“表格”描述的布局和設計效能表的具體內容。

-webkit-border-horizontal-spacing: 2px;-webkit-border-vertical-spacing: 2px;-webkit-column-break-after: right; 常量:always/auto/avoid/left/right-webkit-column-break-before: right; 常量:always/auto/avoid/left/right–webkit-column-break-inside: logical; 常量:avoid/auto-webkit-column-count: 3; //分欄-webkit-column-rule: 1px solid #fff;style:dashed,dotted,double,groove,hidden,inset,none,outset,ridge,solid

⑩“使用者介面”描述屬性,涉及到使用者介面元素在瀏覽器中,如滾動文字區,捲軸,等等。報告還描述屬性,範圍以外的網頁內容,如游標的標註樣式和顯示當您按住觸摸觸摸
目標,如在iPhone上的連結。

-webkit-box-align: baseline,center,end,start,stretch 常量:baseline/center/end/start/stretch-webkit-box-direction: normal;常量:normal/reverse-webkit-box-flex: flex_valuet-webkit-box-flex-group: group_number-webkit-box-lines: multiple; 常量:multiple/single-webkit-box-ordinal-group: group_number-webkit-box-orient: block-axis; 常量:block-axis/horizontal/inline-axis/vertical/orientation–webkit-box-pack: alignment; 常量:center/end/justify/start

12. 動畫過渡
這是 Webkit 中最具創新力的特性:使用過渡函數定義動畫。

-webkit-animation: title infinite ease-in-out 3s;animation 有這幾個屬性:-webkit-animation-name: //屬性名稱,就是我們定義的keyframes-webkit-animation-duration:3s //期間-webkit-animation-timing-function: //過渡類型:ease/ linear(線性) /ease-in(慢到快)/ease-out(快到慢) /ease-in-out(慢到快再到慢) /cubic-bezier-webkit-animation-delay:10ms //動畫延遲(預設0)-webkit-animation-iteration-count: //迴圈次數(預設1),infinite 為無限-webkit-animation-direction: //動畫方式:normal(預設 正向播放); alternate(交替方向,第偶數次正向播放,第奇數次反向播放)

這些同樣是可以簡寫的。但真正讓我覺的很爽的是keyframes,它能定義一個動畫的轉變過程供調用,過程為0%到100%或from(0%)到to(100%)。簡單點說,只要你有想法,你想讓元素在這個過程中以什麼樣的方式改變都是很簡單的。

-webkit-transform: 類型(縮放scale/旋轉rotate/傾斜skew/位移translate)scale(num,num) 放大倍率。scaleX 和 scaleY(3),可以簡寫為:scale(* , *)rotate(*deg) 轉動角度。rotateX 和 rotateY,可以簡寫為:rotate(* , *)Skew(*deg) 傾斜角度。skewX 和skewY,可簡寫為:skew(* , *)translate(*,*) 座標移動。translateX 和translateY,可簡寫為:translate(* , *)。

實現類比彈出訊息框(Alert)的例子:
①定義過渡(在<style type="text/css">段中描述keyframes):

@-webkit-keyframes DivZoom{0% { -webkit-transform: scale(0.01) }60% { -webkit-transform: scale(1.05) }80% { -webkit-transform: scale(0.95) }100% { -webkit-transform: scale(1.00) }}.sZoom { -webkit-animation: DivZoom 0.5s ease-in-out }

(很容易看懂,將元素從縮小的0.01 倍--很小但不能為0 倍,放大到1.05 倍,再縮小到0.95倍,最後到1 倍即正常大小。整個過渡過程事件為0.5 秒,動畫方式為ease-in-out,即慢到快再到慢,預設只進行1 次過渡。這正是大家經常看到的 iPhone 彈出的提示資訊的動畫效果!)
②定義元素(在<body>段中):

<div id="layerH" style="-webkit-border-radius:12px; border:2px solid #FFF;-webkit-box-shadow: 0px 2px 4px #888;position: absolute; left: 24px; top: 106px;<br>width: 256px; height: 268px; padding-left: 8px; padding-right: 8px;color: #FFFFFF; text-shadow: 1px 1px 1px #000; text-align: center;background-color: RGBA(32,48,96,0.9);background-image:url(‘BG-Msg.png‘); background-repeat:no-repeat;z-index: 1; visibility: hidden; "><p><span style="font-size: 16pt; font-weight: bold">使用說明</span></p><hr noshade size="1"><div id="HelpText" style="height: 120px">解說文字</div><hr noshade size="1"><form name="formV" method="POST"><input type="button" value="確認" name="B1"style="width: 100%; height: 40px; font-size: 14pt; ont-weight: bold;color: #FFFFFF; text-shadow: 0px -1px 1px #000;"onclick=" layerH.style.visibility=‘hidden‘"></form></div>

③啟動動畫(在 javascript 定義的函數中)

function pHelp(){layerH.style.visibility = ‘visible‘layerH.style.cssText = "-webkit-animation-delay: " + Math.random() + "ms"layerH.className = ‘sZoom‘}

(這個啟動函數就很好理解了。但是為什麼要使用-webkit-animation-delay 這句呢?因為當一個元素過渡顯示完成後,若其樣式沒有變化,下一次將無法進行過渡動畫顯示。我們巧妙的利用其動畫延遲時間定義,使其有所變化,就避免了上述問題。其中使用隨機數函數Math.random(),產生一個大於0 小於1 的隨機數。當然,延遲零點幾毫秒,使用者是不會察覺的。)

補充:
1. 鎖定 viewport

ontouchmove="event.preventDefault()" //鎖定viewport,任何螢幕操作不移動使用者介面(彈出鍵盤除外)。

2. 被點擊元素的外觀變化,可以使用樣式來設定:

-webkit-tap-highlight-color: 顏色

3. 偵測iPhone/iPod
開發特定裝置的移動網站,首先要做的就是裝置偵測了。下面是使用Javascript偵測iPhone/iPod的UA,然後轉向到專屬的URL。

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {if (document.cookie.indexOf("iphone_redirect=false") == -1) {window.location = "http://m.example.com";}}

雖然Javascript是可以在水果裝置上啟動並執行,但是使用者還是可以禁用。它也會造成用戶端重新整理和額外的資料轉送,所以下面是伺服器端偵測和轉向:

if(strstr($_SERVER[‘HTTP_USER_AGENT‘],‘iPhone‘) || strstr($_SERVER[‘HTTP_USER_AGENT‘],‘iPod‘)) {header(‘Location: http://yoursite.com/iphone‘);exit();}

4. 阻止旋轉螢幕時自動調整字型大小

html, body, form, fieldset, p, div, h1, h2, h3, h4, h5, h6 {-webkit-text-size-adjust:none;}

5. iPhone才識別的CSS
如果不想裝置偵測,可以用CSS媒體查詢來專為iPhone/iPod定義樣式。

@media screen and (max-device-width: 480px) {}

6. 縮小圖片
網站的大圖通常寬度都超過480像素,如果用前面的代碼限制了縮放,這些圖片在iPhone版顯示顯然會超過螢幕。好在iPhone機能還夠,我們可以用CSS讓iPhone自動將大圖片縮小顯示。

@media screen and (max-device-width: 480px){img{max-width:100%;height:auto;}}

7. 類比:hover偽類
因為iPhone並沒有滑鼠指標,所以沒有hover事件。那麼CSS :hover偽類就沒用了。但是iPhone有Touch事件,onTouchStart 類似 onMouseOver,onTouchEnd 類似 onMouseOut。所以我們可以用它來類比hover。使用Javascript:

var myLinks = document.getElementsByTagName(‘a‘);for(var i = 0; i < myLinks.length; i++){myLinks[i].addEventListener(’touchstart’, function(){this.className = “hover”;}, false);myLinks[i].addEventListener(’touchend’, function(){this.className = “”;}, false);}

然後用CSS增加hover效果:

a:hover, a.hover { /* 你的hover效果 */ }

這樣設計一個連結,感覺可以更像按鈕。並且,這個類比可以用在任何元素上。

行動裝置的尺寸

ipod touch 4/iphone4/iphone4s

豎屏
width/height 320/356
橫屏
width/height 480/208

iphone5

豎屏
width/height 320/444
橫屏
width/height 568/208

ipad mini

豎屏
width/height 768/928
橫屏
width/height 1024/672

New Pad

豎屏
width/height 768/928
橫屏
width/height 1024/672

webkit的現成解決方案

1、下拉重新整理
Pull-to-refresh
2、Carousel
Carousel
3、縮放
WKTouch
4、投影片
swipeslide

webkit開發,app移動前端知識點

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.