HTML5-移動開發常用技巧與彈性布局的使用

來源:互聯網
上載者:User

標籤:box   ffffff   元素   邊框   between   device   對齊   定義   中文字型   

一、移動開發常用技巧Viewport基本知識

設定布局Viewport的各種資訊
1、width=device-width; 設定Viewport視口寬度等於裝置寬度
2、initial-scale=1; 網頁預設縮放比為1(網頁在手持功能上,不會進行預設縮放
3、minimum-scale=1 網頁最小縮放比為1
4、maximum-scale=1 網頁最小大縮放比為1
5、user-scalable=no 禁止使用者手動縮放網頁(ios10+ 的裝置失效)

 


在手機站及響應式網站的製作中,網頁必須添加下述Viewport的設定語句

<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />

 

禁止裝置將手機號、郵箱進行識別,取消點擊撥打到電話等事件

<meta name="format-detection" content="telephone=no,email=no"/>

 

ios 添加到主畫面時,WebAPP的標題

<meta name="apple-mobile-web-app-title" content="標題"/>

 

ios添加到主畫面時,啟用WebAPP的全螢幕模式,刪除頂端地址欄和底部工具列

<meta name="apple-mobile-web-app-capable" content="yes"/>

 

ios 添加到主畫面時,WebAPP的頂部狀態列顏色:
black:黑色
white:白色
balck-translucent:黑色半透明。但設定為半透明時網頁將充滿整個螢幕,頂部透明的狀態列將蓋住網頁最上方一小條

<meta name="apple-mobile-web-app-status-bar-style" content="black"/>

 

ios添加到主畫面時WebAPP的表徵圖,href屬性為表徵圖路徑

<link rel="apple-touch-icon-precomposed" href=""/>

 

設定瀏覽器使用最新的IE或chrome去編譯:
>>>這句設定語句不是手機端專用,一般pc網頁均需設定

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
 [手機端字型樣式]

1、一般手機端不支援微軟雅黑字型
2、中文字型一般不設定,使用系統預設即可
3、英文字型一般設定為font-family: helvetica;

font-family: helvetica;
 設定使用者不能選中文本:

1、手機端不能長按選擇
2、pc端不能用滑鼠選擇

-webkit-user-select: none;-moz-user-select: none;

 

設定表單的預設面板,手機、 pc均可使用

appearance: none;-webkit-appearance: none;-moz-appearance: none;

 

設定placeholder的屬性

input::-webkit-input-placeholder {    color: red;}input:-ms-input-placeholder { // IE10+    color: red;}input:-moz-placeholder { // Firefox4-18    color: red;}input::-moz-placeholder { // Firefox19+    color: red;}

 

禁止圖片和超連結長按快顯功能表

-webkit-appearance: none;

 

 

二、響應式布局【使用媒體查詢的三種方式】1、直接在CSS中使用:

@media 類型(長選all/screen) and (條件1) and (條件2) {
  CSS選取器{
    CSS屬性:屬性值;
  }
}

2、使用link連結CSS,media屬性可以設定媒體查詢方式:

<link rel="stylesheet" href="css/02響應式布局.css" media="all and (max-width: 800px)"/>

3、使用@import匯入,直接在url()後面使用空格,即那個媒體查詢規則:

@import url("css/02響應式布局.css") all and (max-width: 800px);

三、彈性布局的使用[flex彈性布局]1、瞭解兩個基本概念

容器:需要添加彈性布局的父元素
項目:彈性版面配置容器中的每一個子項目,稱為項目

 2、彈性布局的使用?

①給父容器添加display:flex/inline-flex;屬性,即可使容器內容採用彈性布局顯示,而不遵循常規文檔流的顯示方式
②容器添加彈性布局後,僅僅是容器內容採用彈性布局,而容器自身在文檔流中的定位方式依然遵循常規文檔流
③display:flex;容器添加彈性布局後,顯示為區塊層級元素
 display:inline-flex;容器添加彈性布局後,顯示為行級元素
④注意,設為 Flex 布局以後,子項目的float、clear和vertical-align屬性將失效。但是position屬性依然生效。

 

3、作用於容器的相關屬性① flex-direction屬性決定主軸的方向(即項目的排列方向)。  row(預設值):主軸為水平方向,起點在左端  row-reverse:主軸為水平方向,起點在右端  column:主軸為垂直方向,起點在上沿。  column-reverse:主軸為垂直方向,起點在下沿。

②flex-wrap屬性定義,如果一條軸線排不下,如何換行

   nowrap(預設):不換行。當容器寬度不夠時,每個項目會被擠壓寬度
   wrap:換行,並且第一行在容器最上方
  wrap-reverse:換行,並且第一行在容器最下方

③ flex-flow   是flex-direction和flex-wrap的縮寫形式,預設值是row nowrap

 

④ justify-content屬性定義了項目在主軸上的對齊。
   >>>此屬性與主軸方向息息相關:主軸方向為:row-起點在左邊,row-reverse-起點在右邊,column-起點在上邊,column-reverse-起點在下邊
  flex-start(預設值):項目位於主軸起點
  flex-end:項目位於主軸終點
  center: 置中
  space-between:左右對齊,項目之間的間隔都相等。(開頭和最後的項目,與父容器的邊緣沒有間隔)
  space-around:每個項目兩側的間隔相等。所以,項目之間的間隔比項目與邊框的間隔大一倍。(開頭和最後的項目,與父容器的邊緣有一定的間隔)

⑤ align-items屬性定義項目在交叉軸上如何對齊。
  flex-start:交叉軸的起點對齊。
  flex-end:交叉軸的終點對齊。
  center:交叉軸的中點對齊。
  baseline: 項目的第一行文字的基準對齊。(文字的行高、字型大小會影響每行的基準)
  stretch(預設值):如果項目未設定高度或設為auto,將佔滿整個容器的高度。

⑥align-content屬性定義了多根軸線的對齊。如果項目只有一根軸線,該屬性不起作用。
  >>>當項目換為多行時,可以使用align-content取代align-items(單行時使用align-items,多行時使用align-content)
  flex-start:與交叉軸的起點對齊。
  flex-end:與交叉軸的終點對齊。
  center:與交叉軸的中點對齊。
  space-between:與交叉軸左右對齊,軸線之間的間隔平均分布。
  space-around:每根軸線兩側的間隔都相等。所以,軸線之間的間隔比軸線與邊框的間隔大一倍。
  stretch(預設值):軸線佔滿整個交叉軸。

4、作用於項目上的屬性

① order屬性定義項目的排列順序。數值越小,排列越靠前,預設為0。
② flex-grow屬性定義項目的放大比例,預設為0,即如果存在剩餘空間,也不放大。
③ flex-shrink屬性定義了項目的縮小比例,預設為1,即如果空間不足,該項目將縮小。
④ flex-basis定義項目佔據的主軸空間。(如果主軸為水平,則設定這個屬性,相當於設定項目的寬度。原width會失效)
⑤ flex屬性是flex-grow, flex-shrink 和 flex-basis的簡寫,預設值為0 1 auto。後兩個屬性可選。
這個屬性有兩個快捷設定: auto=(1 1 auto)/none(0 0 auto)
⑥ align-self:定義單個項目自身在交叉軸上的相片順序,可以覆蓋掉容器上的align-item屬性
屬性值:與align-items相同,預設值為auto,表示繼承父容器的align-items屬性值

 

四、響應式布局demo

HTML代碼:

<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title></title>        <link rel="stylesheet"  href="css/響應式布局demo.css"/>        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />    </head>        <body>        <header id="header">            <ul>                <li>header1</li>                <li>header2</li>                <li>header3</li>                <li>header4</li>                <li>header5</li>            </ul>                        <div>icon</div>        </header>                <section id="main">            <div class="left">left</div>            <div class="center">center</div>            <div class="right">right</div>        </section>                                        <footer id="foot">            footer        </footer>    </body></html>

CSS代碼

*{    margin: 0px;    padding: 0px;    font-size: 48px;    text-align: center;    box-sizing: border-box;}#header,#main,#foot{    height: 100px;    width: 1200px;    background-color: chocolate;    line-height: 100px;    margin: 0 auto;    min-width: 300px;}#header ul{    width: 80%;}#header ul li{    float: left;    width: 20%;    list-style: none;    font-size: 20px;}#header div{    width: 50px;    height: 50px;    background-color: yellow;    float: right;    line-height: 50px;    font-size: 20px;    margin-top: 25px;    margin-right: 25px;    display: none;}#main{    height: 500px;    line-height: 500px;    border-bottom: 10px solid white;    border-top: 10px solid white;}#main .left{    width: 25%;    height: 500px;    background-color: darkcyan;    float: left;}#main .center{    width: 50%;    height: 500px;    background-color: navajowhite;    float: left;    border-left: 10px solid white;    border-right: 10px solid white;}#main .right{    width: 25%;    height: 500px;    background-color: salmon;    float: left;}@media  screen and (max-width: 1200px) {    #header,    #main,    #foot{        width: 100%;    }    }@media only screen and (max-width: 900px) {    #main .right{        display: none;    }    #main .left{        width: 35%;    }    #main .center{        width: 65%;        border-right: hidden;    }}@media only screen and (max-width: 600px) {    #header ul{        display: none;    }    #header div{        display: block;    }    #main{        height: 920px;    }    #main .left{        float: none;        width: 100%;        height: 250px;        line-height: 250px;    }    #main .center{        float: none;        width: 100%;        height: 400px;        line-height: 400px;        border-left: hidden;        border-top: 10px solid white;        border-bottom: 10px solid white;    }    #main .right{        display: block;        float: none;        width: 100%;        height: 250px;        line-height: 250px;    }}

顯示效果:

 

 

HTML5-移動開發常用技巧與彈性布局的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.