Mobile self-adaptive layout scheme attempt

Source: Internet
Author: User

Problem

A few of the most troubling issues when you first touch the Mobile H5 page are:

    1. 6 or 6p is clearly a 1px border how to become 2px or 3px spicy thick!
    2. Picture, Div, etc. how to compare the adaptive design diagram

Then slowly know the 1th is due to the retina screen device pixel than the problem caused by the 2nd known unit REM.

Objective

Do not want to use REM to calculate the size of the design manuscript, design 750 of the dimensions of the dimension can be used directly in the sass, the font does not use REM scaling, because:

Obviously, below the retina screen of iphone3g and iPhone4, we want to see the same text size. That said, we don't want the text to be smaller on the retina screen, and we want to see more text on the big screen, and most of the font files now come with some lattice sizes, usually 16px and 24px, so we don't want to have a wonderful size like 13px and 15px.

Another goal is to dynamically set the page when it is not set viewport, and to not let the page have a "flash" process (that is, the state that has just been loaded is not set to the state of the setting); When the page changes dynamically, the size changes accordingly;

Try

When writing the H5 page, write the meta tag as little as possible, and I'll do it:

This is simple, and the Code of implementation is, of course, placed in the head. Because there is no written viewport, so the judgment plus

if (!metael) {    Metael = doc.createelement (' meta ');    Metael.setattribute (' name ', ' viewport ');    Metael.setattribute (' content ', ' width=device-width,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, User-scalable=no ');    if (docel.firstelementchild) {        docEl.firstElementChild.appendChild (Metael);    } else {        var wrap = Doc.createelement (' div ');        Wrap.appendchild (Metael);        Doc.write (wrap.innerhtml);    }}

Then zoom according to the devicepixelratio of the page

var scale = parsefloat ((1/DPR). toFixed (2)), if (DPR! = 1) {    metael.setattribute (' content ', ' width=device-width, Initial-scale= ' + scale + ', maximum-scale= ' + scale + ', minimum-scale= ' + scale + ', User-scalable=no ');}

Set the root font size

Document Widthfunction Setdocumentfontsize () {    var width = docel.getboundingclientrect (). width;    DocEl.style.fontSize = width/10 + ' px ';} Setdocumentfontsize ();

Page Resize Monitoring

var timer;win.addeventlistener (' Resize ', function () {    cleartimeout (timer);    Timer = setTimeout (function () {        setdocumentfontsize ();    }, +);}, False);

For fonts that do not use REM, you need to set the DATA-DPR property for DocumentElement

Document DATA-DPR set font-size pxvar DPR = window.devicepixelratio;if (devicepixelratio >= 3 && (!DPR | | dp R >= 3)) {                    DPR = 3;} else if (Devicepixelratio >= 2 && (!DPR | | DPR >= 2)) {    DPR = 2;} else {    DPR = 1;} Docel.setattribute (' DATA-DPR ', DPR);

Sass Related Settings

Written entirely according to the design manuscript size

Basefontsize $baseFontSize: @function px2rem ($px) {    @return $px/$baseFontSize * 1REM;} @function px2rem2 ($px) {    @return Px2rem ($px);}

Font size Change

Font size calculation @mixin FONT-DPR ($font-size) {    font-size: $font-size;    [data-dpr= "2"] & {        font-size: $font-size * 2;    }    [data-dpr= "3"] & {        font-size: $font-size * 3;    }}

Example: a simple style setting

. Test {    @include FONT-DPR (10px);    WIDTH:PX2REM2 (+);    margin:0 Auto;    border:1px #dcdcdc Solid;} img {    display:block;    WIDTH:PX2REM2 (355);    margin:0 Auto;}

Examples of effects:

As can be seen, the 6p DPR is 3, the font corresponding to 30px, the page scaled 3 times times, formally I want.

Page demo Source

<! DOCTYPE html>

Welcome to follow my blog: http://ymblog.net

Mobile self-adaptive layout scheme attempt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.