Mobile Web Development Notes
- Mobile Web Development Notes
- Basic concepts
- Pixel units
- CSS pixels and device pixels css pixels
- ppidpi
- Devicepiexelratio
- Text Size Control
- Viewport
- Responsive layout
- Principle
- Implementation method
- Layout scenarios
- Percent Layout Fluid Layout
- Flexible flexible layouts
- Flex-box layout
- Image processing
- General Settings
- Responsive picture
- Media Enquiry
- HTML5
- New form Type
- New input type for HTML5
- CSS3
- Device control
- Device interaction
- Performance optimization
Reference:
Getting Started with mobile web development
Mobile Web Development Tips
Basic concept pixel unit CSS pixels with device pixels css pixels
The abstract unit used by the browser, used primarily to draw content on a Web page. Device pixels: Displays the smallest physical unit of the screen, with each DP containing its own color and brightness. The equivalent CSS pixels on the phone screen, which is not fixed, depending on a lot of properties. After analysis and summary, we can draw such a formula:
1 CSS pixels = (devicePixelRatio)^2 device pixels
(^2 is the meaning of the square, as for what Devicepixelratio is, will be explained later).
ppi/dpi
The PPI, sometimes called dpi, represents the number of pixels (pixel) per inch , the higher the value, which means that the display can display the image at a higher density. (Note: The pixels here refer to device pixels.) Figuring out what the PPI means, we can easily understand how the PPI is calculated, we need to figure out the diagonal equivalent of the cell phone screen, and then we can get the PPI by the diagonal (the length of the diagonal of the phone screen, which is what we normally call screen size). Accurate calculation of the publicity can be referred to. Interestingly, the PPI for the iphone 4, based on the formula, is 330, a little higher than the official Apple announcement of 326.
Devicepiexelratio
Text Size Control
px, rem固定大小 - px多屏适配,统一修改 - rem相对长度单位。相对于根元素(即html元素)font-size计算值的倍数rem - font size of the root element (W3C)
Viewport
Reference: In-depth understanding of mobile front-end development viewport
<meta name="viewport" content="width=device-width, height=device-height, inital-scale=1.0, maximum-scale=1.0, user-scalable=no;"/>
In Apple's specification, Meta viewport has 6 attributes, as follows:
-width of Width-viewport [Pixel_value |device-width]
-Height of Height-viewport [pixel_value |device-height]
-Initial-scale-Initial zoom ratio [float_value]christer1229
-Maximum-scale-Allows the user to zoom to the maximum scale
-Minimum-scalbujue-Minimum zoom value allowed for the user
-User-scalable-whether the user can scale manually [yes | No
Responsive layout Principles
- Mobile First
- Progressive enhancement
Implementation method
- Media Enquiry
- Using JS
- Using a third-party framework
Layout scheme percent layout (fluid layout)
Sets the width of the element's original fixed width except the parent container to a width, uses the percentage, and the text uses EM.
- Advantages:
The Mobile layout page is more user friendly because it adapts to the user's settings.
The white space around the page is the same in all resolutions and browsers, and is visually beautiful.
If well designed, the flow layout avoids horizontal scrollbars at small resolutions.
- Disadvantages:
Designers are more difficult to control what users see and may omit some errors because they look good at a particular resolution;
Images, videos, and other content that are set to width may require a variety of widths to accommodate users of different resolutions;
In a particularly large resolution, the content will be pulled to grow a long line, becoming difficult to read;
There's going to be a distorted picture.
Flex (Flexible) layout
Replace PX with REM
Principle: Set a maximum width, the device screen exceeds this width, the REM size no longer changes, otherwise, the REM size is set to a fraction of the screen width, the length unit value in the page is replaced by REM px, so that the width of the page changes can be scaled.
REM is actually the font size of the root element of the page, and the code for changing REM values via JS is as follows:
;( function(win) ) { vardoc = win.document;varDocel = doc.documentelement;//Get document elements varTid function refreshrem() { varwidth = Docel.getboundingclientrect (). width;//Get device width if(Width >540) {width =540; }varrem = width/Ten;//Set REM to One-tenth of the screen widthDocEl.style.fontSize = rem +' px '; } win.addeventlistener (' Resize ', function() {//When the screen width changesCleartimeout (TID);//Clear the previous timerTid = SetTimeout (Refreshrem, -);change rem size after//300ms},false); Win.addeventlister (' Pageshow ', function(e) {//When the page is displayed if(e.persisted) {cleartimeout (TID); Tid = SetTimeout (Refreshrem, -); } },false); Refreshrem ();}) (window);
Reference: Mobile pages use REM for adaptation
Flex-box layout
Flex-box, reference:
Flex Layout Tutorial: Syntax Chapter
Advantages: Convenient layout, high flexibility;
Cons: compatibility issues;
Picture Processing general Settings:
img{ max-width: 100%;}
Or:
img { width: 100%; max-width: 300px;}//限制最宽不超过300px;
Responsive Picture:
Reference:
Responsive picture Srcset New interpretation sizes Property W Descriptor
Srcset and sizes
That is, according to the screen density reality corresponding size picture
src: Specify resources (this will be loaded when the browser does not recognize Srcset and size)
srcset: The value of Srcset is the URL list of the available image versions, and the width of each image is indicated by the W descriptor.
sizes: Use length to match media queries, the browser detects every media query until it matches. Use the length of the query mate as the last piece of the "Select a resource" puzzle: Image render width or relative to view.
eg.
<img src="small.jpg" srcset="large.jpg 1024w, 640w, 320w" sizes="(min-width: 36em) 33.3vw, 100vw" alt="A rad wolf" />
Media Enquiry
- You can
<link> specify the device type for the style sheet by using the Media property of the label
<link rel="stylesheet"type="text/css" media="screen" href="screen-style.css"><link rel="stylesheet"type="text/css" media="screen and (orientation:portrait)" href="style.css"//纵向屏幕<link rel="stylesheet"type="text/css" media="screen and (max-width:960px)" href="style.css"//宽度小于900px时
- In CSS, you can also use the
@Media screen and (max-width: 960px){Body{ background: #000; }}@Media (max-width: 960px){Body{ background: #000; }}@Media screen and (min-width:960px) and (max-width:px){Body{ background:yellow; }}
- @import method
@import can reference a style file and can also be used to reference a media type. There are two main ways @import introduce media types, one is to call another style file in a style through @import, and the other is to be introduced in the label <style></style> , but this usage is not supported in ie6~7, such as when another style file is called in the style file, You can specify the corresponding media type.
@importurl(reset.css) screen; @importurl(printprint;
The <style> Media type method is introduced in the label.
<head><style type="text/css"> @importurl(style.css) all;</style></head>
For easy access, all the features available for media query detection are listed below
-width: viewport widths.
-Height: viewport height.
-Device-width: The width of the rendered surface (for us, the width of the device screen).
-Device-height: The height of the rendered surface (for us, the height of the device screen).
-Orientation: Check whether the device is in landscape or portrait orientation.
-Aspect-ratio: Aspect ratio based on viewport width and height. A 16:9 scale display can define the ASPECT-RATIO:16/9 as such.
-Device-aspect-ratio: Similar to Aspect-ratio, based on the aspect ratio of the device rendering plane width and height.
-Color: The number of bits per color. For example, Min-color:16 detects if the device has a 16-bit color.
-Color-index: Number of colors in the Color index table of the device. The value must be a non-negative integer.
-Monochrome: Detects the number of bits used per pixel in the monochrome frame buffer. The value must be a non-negative integer, such as Monochrome:2.
-resolution: Used to detect screen or printer resolution, such as min-re a way to detect whether IE is loaded, using DoScroll method call, details visible http://javascript.nwbox.com/IEContentLoaded/.
The principle is that for IE in non-IFRAME, only continuously through can execute doscroll to determine whether the DOM is loaded. In the above interval of 50 milliseconds to try to execute doscroll, note that because the page does not load complete, call DoScroll will cause an exception, so use Try-catch to catch the exception.
Conclusion: So in general, when the page DOM is not loaded, an exception is generated when the DoScroll method is called. Then we use the reverse, if not abnormal, then the page Dom loading is complete. solution:300dpi. You can also accept measurements of pixel points per centimeter, such as MIN-RESOLUTION:118DPCM.
-Scan: The way the TV is scanned, the value can be set to progressive (progressive scan) or interlace (interlaced scanning). such as 720p HD TV (>devide-width refers to the screen height, width refers to the width of the render window height
The P of 720p indicates a progressive scan) matching the scan:progressive, while the 1080i HD TV (I in 1080i indicates that it is interlaced) matches the scan:interlace.
-Grid: Used to detect whether the output device is a grid device or a bitmap device.
-In all of the above features, you can use the Min and Max prefixes to create a query scope in addition to scan and grid.
HTML5 New Form Types
- Placeholder
Simply by adding the placeholder attribute to the INPUT element, its property value is displayed as placeholder text by default, and the text disappears automatically when the input box takes focus. Placeholder text is displayed again when the input box loses focus and there is no input value.
-
Required
In a browser that supports HTML5, appending a boolean-type attribute required to the input element (that is, you can choose to append or not append the property) indicates that the form field is required. If there is no information on the required entry when the form is submitted, the browser displays a warning message. How the warning message is displayed, including the display content and style, depends on the browser and the input box type.
Input elements of range, color, button, and hidden types cannot use required because these input types have almost all default values.
-
Autofocus
The Autofocus property of HTML5 allows a form field to be focused (or checked) by default for user input when loading is complete.
For example, multiple form fields are appended with the autofocus property, and in Chrome (V16) The last form field that uses the autofocus attribute is focused, while Firefox (V9), in contrast, focuses on the first form field that uses autofocus.
-
AutoComplete
You can also disable AutoComplete for the entire form by setting properties on the form itself (not fieldset). The sample code is as follows:
<form id="redemption" method="post" autocomplete="off">
- List (and the corresponding DataList element)
The List property, along with the corresponding DataList element, allows the user to display a set of alternate values as they begin entering values in the input box.
The value in the List property (awards) is also the ID of the DataList element. This allows the DataList to be associated with the input. Although it is not required to package option in Select, it makes it easier to provide a downgrade scheme for older browsers.
New input type for HTML5
- Type= "Email"-the browser that supports it expects the user's input to match the format of the email address.
- Type= "Number"--support for this feature
The browser expects to enter a number. This input type also provides a control button by default, allowing the user to simply click Up or down to change the value.
You may notice in the above code that we also set the minimum and maximum range of allowable inputs, as follows:
type="number"min="1929"max="2015"
Out-of-range numbers are treated in a special way (should).
-type= "url"--you guessed it, the URL input type is used to enter the URL address.
-Type= "Tel" is another type of input used to collect contact information. Tel indicates that the form field expects to enter a phone number-type=e= "search"-and the normal text input box behaves basically the same, rendering only a slight difference in the individual browsers.
You can use the Pattern property to define data for form fields with regular expressions
-type= "COLOR"--generates a color selector in the browser that supports the feature, allowing the user to select a hexadecimal color value.
-Date and time input type
As with the color type, there are currently few browsers that provide native support for date, and most browsers render them as standard text input boxes by default. The super-great opera has realized this feature
The range input type generates a slider bar.
CSS3
Notes
Device control
- Turn on the numeric keypad
<input type="tel">
- Hide the Address bar
setTimeout(function(){ window.scrollTo(0, 1); }, 0);
- Add a website to your home screen using the Add to Home button on Iphone,ipad,itouch Safari
<link rel="apple-touch-icon" href="apple-touch-icon-iphone.png" />
<link rel="apple-touch-icon" sizes="72x72" href="apple-touch-icon-ipad.png" />
<link rel="apple-touch-icon" sizes="114x114" href="apple-touch-icon-iphone4.png" />
Device interaction
- Input fixed failure problem
Reference:
Fixed invalid problem solution for fixed + Input under iOS when calling the keyboard
"Tips" virtual keyboard with fixed to the mobile side of the pain!
Fixed layout solution for Web Mobile
Reason for the occurrence:
The mobile virtual keyboard appears when the text box (text Class) gets the focus, but the text box does not have the focus to pop up the keyboard;
The condition for the virtual keyboard to be closed is: The text box is out of focus
All in all, we think it might not work when the virtual keyboard appears or disappears
On a mobile device, if the text box is above, click No problem:
At the bottom of the device, the entire block is moved up to show the input area
This time a few tricky questions arose:
① virtual keyboard appears on the page is not known, this sentence is understood: No keyboard events, no way to get the keyboard height
② keyboard is "affixed" on the viewport, the surface will not have "any" effect on the DOM, but this time some positioning elements of the performance has become "weird."
After the soft keyboard is aroused, the fixed element of the page will be invalidated (that is, it cannot be floated, or it can be understood as absolute positioning), so when the page is more than one screen and scrolling, the invalid fixed element will follow the scroll.
This is the fixed element and input box bug on IOS. This is not limited to the Type=text input box, which is called when the soft keyboard (such as time-date selection, select Selection, and so on) is aroused and will encounter the same problem.
Solution:
Causes the fixed parent element to not scroll, limiting the scrolling region to the content portion of the footer and header siblings.
Performance optimization
Reference: Mobile Development Specification Overview
To consider Android low-end machine with 2G network scenario under performance Note!
Required checks before publishing
所有图片必须有进行过压缩考虑适度的有损压缩,如转化为80%质量的jpg图片考虑把大图切成多张小图,常见在banner图过大的场景
Load performance optimization to open fast enough
数据离线化,考虑将数据缓存在 localStorage初始请求资源数 < 4图片使用CSS Sprites 或 DataURI外链 CSS 中避免 @import 引入考虑内嵌小型的静态资源内容初始请求资源gzip后总体积 < 50kb静态资源(HTML/CSS/JS/Image)是否优化压缩?避免打包大型类库确保接入层已开启Gzip压缩(考虑提升Gzip级别,使用CPU开销换取加载时间) 注意!尽量使用CSS3代替图片初始首屏之外的静态资源(JS/CSS)延迟加载初始首屏之外的图片资源按需加载(判断可视区域)单页面应用(SPA)考虑延迟加载非首屏业务模块开启Keep-Alive链路复用
Operational performance optimization to achieve smooth enough operation
避免 iOS 300+ms 点击延时问题 注意!缓存 DOM 选择与计算避免触发页面重绘的操作Debounce连续触发的事件(scroll / resize / touchmove等),避免高频繁触发执行 注意!尽可能使用事件代理,避免批量绑定事件使用CSS3动画代替JS动画避免在低端机上使用大量CSS3渐变阴影效果,可考虑降级效果来提升流畅度HTML结构层级保持足够简单尽能少的使用CSS高级选择器与通配选择器Keep it simple
Online Performance Testing Assessment Tool User Guide
访问 Google PageSpeed 在线评定网站在地址栏输入目标URL地址,点击分析按钮开始检测按 PageSpeed 分析出的建议进行优化,优先解决红色类别的问题
Mobile Web Development Notes