h5 页面自适应,完美,弹 - V2EX
blackMountain

h5 页面自适应,完美,弹

  •  
  •   blackMountain Jun 14, 2017 5437 views
    This topic created in 3278 days ago, the information mentioned may be changed or developed.

    目前知道的 h5 页的自适应方法有两种,但都不是很完美。

    一种是快被被淘汰的:

    <meta name="viewport" cOntent="width=750,target-densitydpi=device-dpi,user-scalable=no"/>

    这个的好处是所有元素都可以用 px 来规定宽高,因为页面会随着设备的尺寸自动缩放。 但是这种方法很坑,因为写出来的页面是固定比例的,在不同设备下页面比例之外的部分都是一片白色,非常难看。

    第二种方法是现在最常用的:

    <meta name="viewport" cOntent="width=device-width, initial-scale=1.0, minimum-scale=1.0,maximum-scale=1.0">

    页面宽度等于设备宽度,高度 100%就好了。

    这种方法的缺点在于,因为页面宽高不确定,页面里元素只能用百分比来规定尺寸。有些情况百分比不是那么方便: 比如要让 div 中的字体垂直居中,但是 div 高度是用百分比写的没办法用 line-height。 又比如 iphone5 这种奇葩机型,按比例写出来的页面肯定会崩溃的。

    是不是我孤陋寡闻,其实还有一种完美解决自适应问题的方法呢。

    12 replies    2017-06-15 15:41:00 +08:00
    horizon
        1
    horizon  
       Jun 14, 2017
    垂直居中用 flex 啊。。为啥还要用百分比。。
    blackMountain
        2
    blackMountain  
    OP
       Jun 14, 2017
    @horizon 不是阿,写了一个 div,然后让 div 中的文字垂直居中,这个怎么用 flex 阿
    est
        3
    est  
       Jun 14, 2017
    @blackMountain table 大法好
    xss
        4
    xss  
       Jun 14, 2017
    不是非常懂前端
    但是垂直居中这种需求我一般是
    display: table
    还是 table-cell 来着?
    horizon
        5
    horizon  
       Jun 14, 2017
    rabbbit
        6
    rabbbit  
       Jun 14, 2017
    .div {display: flex;align-items: center;}
    试试这个?
    Biwood
        7
    Biwood  
       Jun 14, 2017
    div {
    display: table-cell;
    vertical-align: middle;
    }

    或者

    div {
    display: flex;
    align-items: center;
    }

    都可以
    lupkcd
        8
    lupkcd  
       Jun 14, 2017
    来 大兄弟 试试这句代码 `<meta name="viewport" cOntent="width=640,target-densitydpi=device-dpi,maximum-scale=1.0, user-scalable=no">`
    还有各种 rem 方案等
    chairuosen
        9
    chairuosen  
       Jun 14, 2017
    rem 方案啊
    dtw5t
        10
    dtw5t  
       Jun 14, 2017
    flex+rem
    blackMountain
        11
    blackMountain  
    OP
       Jun 15, 2017
    @rabbbit @Biwood 恩,谢谢,学到了一种新的垂直居中方法,以前都用 line-height 等于元素的高
    meepo3927
        12
    meepo3927  
       Jun 15, 2017
    有成熟方案的, 请搜 flexible.js
    About     Help     Advertise     Blog     API     FAQ     Solana     3174 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 48ms UTC 02:38 PVG 10:38 LAX 19:38 JFK 22:38
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86