移動端 html經常用到的一些html 和css )

來源:互聯網
上載者:User

因為自己也寫了半年的移動端前端.發現此文章總結的很好.

移動過來 http://adamlu.com/?p=633

針對webkit的HTML, CSS和Javascript

前面有一篇文章介紹了HTML5的一些新特性以及技巧, 現再來總結一些更多的針對webkit的HTML, CSS和Javascript方面的特性.

HTML, 從HTML文檔的開始到結束排列:

<meta name=”viewport” content=”width=device-width, initial-scale=1.0″/> 讓內容的寬度自適應為裝置的寬度, 在做Mobile Web時必須加的一條

<meta name=”format-detection” content=”telephone=no”]]> 禁用手機號碼連結(for iPhone)

<link rel=”apple-touch-icon” href=”icon.png”/> 設定你網頁的表徵圖, 尺寸為57X57 px

<!– iOS 2.0+: tell iOS not to apply any glare effects to the icon –>

<link rel=”apple-touch-icon-precomposed” href=”icon.png”/>

<!– iOS 4.2+ icons for different resolutions –>

<link rel=”apple-touch-icon” sizes=”72×72″ href=”touch-icon-ipad.png” />

<link rel=”apple-touch-icon” sizes=”114×114″ href=”touch-icon-iphone4.png” />

<link rel=”apple-touch-startup-image” href=”startup.png”> 全屏啟動時候的啟動畫面映像, 尺寸320X460 px

<meta name=”apple-mobile-web-app-capable” content=”yes” /> 是否允許全螢幕顯示, 只有在案頭啟動時可用

<meta name=”apple-mobile-web-app-status-bar-style” content=”black” /> 控制全屏時頂部狀態列的外觀, 預設白色

<input autocorrect=”off” autocomplete=”off” autocapitalize=”off”>  取消自動完成, 自動大寫單詞字母(適用於Mobile上)

<input type=”text” x-webkit-speech /> 語音輸入

<input type=”file” accept = “image/*; capture=camera” /> 檔案上傳, 從相機捕獲媒體, 下同

<input type=”file” accept = “video/*; capture=camcorder” />

<input type=”file” accept = “audio/*; capture=microphone” />

<a href=”sms:18005555555,18005555556″]]> 傳送簡訊給多個人 的連結

<a href=”sms:18005555555?body=Text%20goes%20here”]]> 傳送簡訊附帶內容 的連結

<a href=”tel:18005555555″]]>Call us at 1-800-555-5555</a]]> 撥打到電話 的連結

CSS:

-webkit-tap-highlight-color: transparent; Mobile上點選連結高亮的時候設定顏色為透明

-webkit-user-select: none; 設定為無法選擇文本

-webkit-touch-callout: none; 長按時不觸發系統的菜單, 可用在圖片上加這個屬性禁止下載圖片

:-webkit-full-screen canvas {} 全螢幕模式時的樣式(for Desktop)

div p :matches(em, b, strong) {} 使用mathes來匹配多個選取器

@media only screen and (max-width: 480px) {} 指定Mobile裝置或者小螢幕案頭螢幕

@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 300dpi) { 指定高解析度螢幕裝置

header { background-image: url(header-highres.png); }

}

@media (-webkit-max-device-pixel-ratio: 1.5),(max-resolution: 299dpi) { 指定低解析度螢幕裝置

header { background-image: url(header-lowres.png); }

}

background-repeat:  space; background-repeat: round; 這兩種CSS3的背景屬性值得研究

width: calc(100%-40px); 計算寬度

text-decoration: #FF8800  wavy ine-through; 波浪型連結

text-rendering: optimizeLegibility; 用這個屬性之後會收緊字元間的距離

font-variant-ligatures:  common-ligatures; 設定CSS連字

transform:  rotate(90); 旋轉90度

transform-origin: center center; transform-origin可以改變變換的位置

-webkit-appearance: none; -webkit-appearance可以改變按鈕或者其它控制項看起來類似本地的控制項

美化表單校正時的提示樣式

::-webkit-validation-bubble {}::-webkit-validation-bubble-message {}::-webkit-validation-bubble-arrow {}::-webkit-validation-bubble-arrow-clipper {}

當提示出現時類似於下面的結構

<div -webkit-validation-bubble><div -webkit-validation-bubble-arrow></div><div -webkit-validation-bubble-arrow-clipper></div><div -webkit-validation-bubble-message>Error Message</div></div>

自訂webkit瀏覽器的捲軸, 見Google Reader等在Chrome/Safari下的效果, 下面是一個執行個體, 這個捲軸的樣式代碼如下:

Customized WebKit Scrollbar /* Let´s get this party started */::-webkit-scrollbar {width: 12px;}/* Track */::-webkit-scrollbar-track {-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);-webkit-border-radius: 10px;border-radius: 10px;}/* Handle */::-webkit-scrollbar-thumb {-webkit-border-radius: 10px;border-radius: 10px;background: rgba(255,0,0,0.8);-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5);}::-webkit-scrollbar-thumb:window-inactive {background: rgba(255,0,0,0.4);}

-webkit-background-composite: plus-darker; -webkit-background-composite用來設定一個元素的背景或顏色的組合樣式

-webkit-text-stroke:  1px rgba(0,0,0,0.5); -webkit-text-stroke可以用來給文字添加描邊

-webkit-mask-image:  url(/path/to/mask.png); 定義一個圖片用來遮罩元素

-webkit-box-reflect:  below 5px; 定義了一個元素的反射

:local-link {font-weight: normal;}  local-link可以定義相對位址的連結樣式

Javascript:

window.scrollTo(0,0); 隱藏地址欄

window.matchMedia(); 匹配媒體

navigator.connection; 決定手機是否運行在WiFi/3G等網路

window.devicePixelRatio; 決定螢幕解析度(iPhone 4值為2, 而Nexus One值為1.5)

window.navigator.onLine; 取得網路連接狀態

window.navigator.standalone; 決定iPhone是否處於全屏狀態

touch事件 (iOS, Android 2.2+): touchstart, touchmove, touchend, touchcancel

gesture事件 (Apple only, iOS 2+):  gesturestart, gesturechange, gesturend give access to predefined gestures (rotation, scale, position)

window.addEventListener("orientationchange", function(e){//window.orientation(0 is portrait, 90 and -90 are landscape)}, false);window.addEventListener("deviceorientation", function(e){//e.alpha//e.beta//e.gamma}, false);window.addEventListener("devicemotion", function(e){//e.accelerationIncludingGravity.x//e.accelerationIncludingGravity.y//e.accelerationIncludingGravity.z}, false);

requestAnimationFrame() 新的動畫函數

element.webkitRequestFullScreen() 調用全屏函數

閱讀更多:

http://www.slideshare.net/franksvalli/mobile-html-css-and-javascript

http://css-tricks.com/9130-custom-scrollbars-in-webkit/

https://developer.mozilla.org/zh-CN/docs

相關文章

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.