A summary of mobile Web adaptation screen scenarios

Source: Internet
Author: User

Basic points of knowledge

device pixel : device pixel is also called physical pixel (physical pixel), the device can control the smallest unit of display, we can think of these pixels as a point on the monitor. The physical pixels of the iPhone5 are 640x1136.

PS: Under the normal screen, 1 css pixels correspond to 1 physical pixels (1:1).

Under the ultra-high pixel density screen (Retina display), 1 css pixels correspond to 4 physical pixels (1:4). (This refers specifically to the Apple Ultra HD screen)

Therefore, in the mobile side of the need to set DPR to ensure that the ultra-high-definition screen display image is not distorted.

logical pixels (logical pixel) device-independent units used to measure pixels logically. CSS pixels are logical pixels , and css pixels are the concept of Web programming . The logical pixels of the iPhone5 are 320x568.

device independent pixels (density-independent pixel) Dip, Unit DP, device-independent unit used to measure pixels logically .

PS: Logical pixel ≈ device independent pixels. (device independent pixels are made by Android)

Unit pixel ratio (device pixel ratio)

Device pixel ratio = device pixel/device independent pixel//in one Direction, x direction or y direction

Iphone5 For example: 640x1136/320x568 = 2

The screen pixel density (pibel Per Inch) is referred to as PPI, which is DPI and is generally used to measure the fine degree of computer monitors, televisions and handheld electronic devices. Typically, the higher the pixel value per inch, the finer the image the screen can display.

Screen pixel density = screen width (or height) pixels/inches wide screen (or high) inches

About the relationship between design and development

Web designers create designs based on device pixels .

The front-end engineer makes the Web page in units of pixels compared to the converted logical pixels .

For example: the width of the element in the design diagram is 100px, then the width will be 100PX/2 = 50px. (for reference only, take the size of the iphone6 design as an example)

A summary of the adaptation scheme

1. The width of fixed viewport is equal to the width of the device and adaptive.

<meta name= "viewport" content= "width=device-width,initial-scale=1" > (for Responsive websites)

PS: I see a lot of people are directly fixed width. such as: <meta name= "viewport" content= "Width=640,user-scalable=no" >

2. Dynamically generate TARGET-DENSITYDPI value, fixed viewport width value.

functionADAPTVP (a) {functionC () {if(B.uwidth = a.uwidth? a.uwidth:640, b.dwidth = a.dwidth? a.dwidth:window.screen.width | | window.screen.availWidth, b . Ratio = Window.devicepixelratio? Window.devicepixelratio:1, b.useragent = navigator.useragent, B.bconsole = A.bconsole? A.bconsole:! 1, A.mode)returnB.mode = A.mode,void0; varc = B.useragent.match (/android/i); if(c) {B.mode= "android-2.2"; varD = B.useragent.match (/android\s (\d+.\d+)/i); D&& (d = parsefloat (d[1)), 2.2 = = D | | 2.3 = = d? B.mode = "android-2.2": 4.4 > d? B.mode = "android-dpi": D >= 4.4 && (b.mode = b.dwidth > b.uwidth?) "android-dpi": "Android-scale")        }    }    functiond () {varc = "", D=!1; Switch(b.mode) { Case"Apple": C= "Width=" + B.uwidth + ", User-scalable=no";  Break;  Case"android-2.2": A.dwidth|| (B.dwidth = 2 = = B.ratio? 720:1.5 = = b.ratio? 480:1 = B.ratio?); varE = Window.screen.width | |Window.screen.availWidth; * = = e? B.dwidth = B.ratio * e:640 > E && (b.dwidth = e), B.mode = "android-dpi", d =!0;  Case"ANDROID-DPI":                varf = * B.uwidth/b.dwidth *B.ratio; C= "target-densitydpi=" + F + ", width=" + B.uwidth + ", User-scalable=no", D && (B.mode = "android-2.2");  Break;  Case"Android-scale": C= "Width=" + B.uwidth + ", User-scalable=no"        }        varg = Document.queryselector ("meta[name= ' viewport ')") | | Document.createelement ("meta"); G.name= "Viewport", g.content =C; varh = document.getElementsByTagName ("Head"); H.length> 0 && h[0].appendchild (g)}functione () {varA = "";  for(Keyinchb) A + = key + ":" + B[key] + ";"; Alert (a)}if(a) {varb ={uwidth:0, Dwidth:0, ratio:1, Mode:"Apple", useragent:NULL, Bconsole:!1        }; C (), D (), B.bconsole&&e ()}}

PS: I really do not know how the source code is coming from the project is extracted from. This scheme has not been recommended for use . (not the perfect solution)

3. Dynamic Initial-scale,maximum-scale,minimum-scale value, fixed viewport width value.

/** * MobileWeb general function Assistant, including commonly used UA judgment, page adaptation, search parameter to key value pairs. * This JS should be introduced as early in the head as possible, reducing repainting. * * The Fixscreen method is adapted according to two conditions, and the method is executed automatically. * 1. Fixed width: Corresponding meta tag notation--<meta name= "viewport" content= "width=750" > * This method extracts the width value and actively adds the scale related attribute value. * Note: If Initial-scale is specified in the META tag, the method will not be processed (i.e. not executed). * 2. REM: Do not write meta tags, the method is automatically generated according to DPR, and in the HTML tag add DATA-DPR and font-size two attribute values. * This method constraint: IOS system Maximum DPR = 3, other system DPR = 1, the maximum width per DPR page (that is, page width/dpr) = 750,rem conversion ratio is 16. * Corresponding to the development of CSS, any elastic size using REM units, the REM default width is the width of the visual version/16; * Scss $ppr (pixel per rem) variable notation-$ppr: 750px/16/1rem; * Element size notation--html {font-size: $PPR *1rem;} body {width:750px/$ppr;}. */Window.mobileutil= (function(Win, doc) {varUA =navigator.useragent, Isandroid=/android|adr/gi.test (UA), Isios=/iphone|ipod|ipad/gi.test (UA) &&!isandroid,//It is said that some of the Chinese machine's UA will contain both Android iphone charactersIsMobile = Isandroid | | Isios;//rough judgment.    return{isandroid:isandroid, Isios:isios, Ismobile:ismobile, Isnewsapp:/newsapp\/[\d\.] +/gi.test (UA), Isweixin:/micromessenger/gi.test (UA), isqq:/qq\/\d/gi.test (UA), Isyixin:/yixin/gi.test (UA), Isweibo:/weibo/gi.test (UA), Istxweibo:/t (?: x|encent) microblog/gi.test (UA), Tapevent:ismobile? ' Tap ': ' click ',        /** * Zoom page*/Fixscreen:function() {            varMetael = Doc.queryselector (' meta[name= "viewport"]), METACTT= Metael? Metael.content: ", Matchscale= Metactt.match (/initial\-scale= ([\d\.] +)/), MatchWidth= Metactt.match (/width= ([^,\s]+)/); if(!metael) {//REM                varDocel =doc.documentelement, MaxWidth= DOCEL.DATASET.MW | | 750,//Maximum page width per DPRDPR = Isios? Math.min (Win.devicepixelratio, 3): 1, Scale= 1/DPR, Tid; Docel.removeattribute (' DATA-MW '); DOCEL.DATASET.DPR=DPR; Metael= Doc.createelement (' meta '); Metael.name= ' Viewport '; Metael.content=Fillscale (scale);                DocEl.firstElementChild.appendChild (Metael); varRefreshrem =function() {                    varwidth =docel.getboundingclientrect (). width; if(Width/dpr >maxwidth) {Width= MaxWidth *DPR; }                    varREM = WIDTH/16; DocEl.style.fontSize= rem + ' px ';                }; Win.addeventlistener (' Resize ',function() {cleartimeout (TID); Tid= SetTimeout (Refreshrem, 300); }, false); Win.addeventlistener (' Pageshow ',function(e) {if(e.persisted) {cleartimeout (TID); Tid= SetTimeout (Refreshrem, 300); }                }, false);            Refreshrem (); } Else if(IsMobile &&!matchscale && (matchwidth && matchwidth[1]! = ' device-width ')) {//Fixed Width                varwidth = parseint (matchwidth[1]), IW= Win.innerwidth | |width, ow= Win.outerwidth | |IW, SW= Win.screen.width | |IW, saw= Win.screen.availWidth | |IW, IH= Win.innerheight | |width, oh= Win.outerheight | |ih, ish= Win.screen.height | |IH, Sah= Win.screen.availHeight | |IH, W=math.min (Iw,ow,sw,saw,ih,oh,ish,sah), scale= w/width; if(Scale < 1) {metael.content= Metactt + ', ' +Fillscale (scale); }            }            functionFillscale (scale) {return' initial-scale= ' + scale + ', maximum-scale= ' + scale + ', minimum-scale= ' + scale + ', User-scalable=no '; }        },        /** * Turn href parameter into key value pair * @param href {string} specified href, default to the current page href * @returns {object} key value pair */Getsearch:function(href) {href= href | |Win.location.search; vardata = {},reg =NewREGEXP ("([^?=&]+) (= ([^&]*))?", "G" ); href&& Href.replace (Reg,function($ A, $ $, $) {data[$1] = $ $;            }); returndata; }    };}) (window, document);//default Direct Adaptation pageMobileutil.fixscreen ();

PS: This scenario is identical to Scenario 2 (not the perfect solution)

4. Dynamically generate Initial-scale,maximum-scale,minimum values, layout wide use rem , and then HTML tags set font-size, using Fone-size to control rem width, and also device pixel ratio to set DPR to fit the screen density display of different mobile devices.

Hand-scouring scheme: lib-flexible

Summary: Getting UA does not get the user's browser information, because many mobile browsers can set up custom UA (such as mobile phone Board QQ browser)

It is highly recommended to use the hand-scouring scheme for mobile web adaptation. Some of the other programs are basically not perfect for matching.

A summary of mobile Web adaptation screen scenarios

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.