以下来源于--拉钩教育学习内容
开发移动端
- 兼容移动端主流浏览器,处理Webkit内核浏览器即可
- 移动端调试的方法
- Chrome DevTools(谷歌浏览器)的模拟手机调试
- 搭建 本地web服务器,手机和服务器一个局域网内,通过手机访问服务器
- 使用外网服务器,直接IP或域名访问
开发准备
- 视口(viewport) 就是浏览器显示页面内容的屏幕区域。视口可以分为布局视口、视觉视口和理想视口。
- 布局视口:一般移动设备的浏览器都默认设置了一个布局视口,用于解决早期的PC端页面在手机上显示的问题(iOS,Abdroid基本都将这个视口分辨率设置为980px,所以PC上的网页大多都能在手机上呈现,只不过元素看上去很小,一般默认可以通过手动缩放网页)
- 视觉视口:用户正在看到的网站的区域(可以通过缩放去操作视觉视口,但不会影响布局视口,布局视口扔保持原来的宽度)
- 理想视口:为了使网站在移动端有最理想的浏览和阅读宽度而设定。(需要手动添加<meta>视口标签通知浏览器操作,目的是:布局视口的宽度应该与理想视口的宽度一致,简单理解就是设备有多宽,我们布局的视口就多宽)
//标准的viewport参数设置 <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no,maximum=1.0,minimum=1.0">
属性 | 解释说明 |
---|---|
width | 宽度设置的是viewport宽度,可以设置device-width特殊值 |
initial-scale | 初始缩放比,大于0的数字 |
maximum-scale | 最大缩放比,大于0的数字 |
minimum-scale | 最小缩放比,大于0的数字 |
user-scalable | 用户是否可以缩放,yes或no(1或0) |
-
开发选择
- 移动端主流方案
单独制作移动端页面(主流) 响应式页面兼容移动端(其次) 京东商城手机版、淘宝触屏版、苏宁易购手机版、携程网手机版… 三星手机官网(www.samsung.com/cn/)… - 通常情况下,网址域名前面加m(mobile)可以打开移动端。通过判断设备,如果是移动端设备打开,则跳到移动端页面。
- 现在市场主流的选择还是单独制作移动端页面
-
常见布局 技术选型-------见另一篇 常见布局
单独制作移动端页面 | 响应式页面兼容移动端 |
---|---|
流式布局(百分比布局)、flex弹性布局(强烈推荐)、less+rem+媒体查询、混合布局 | 媒体查询、bootstrap |
开发
以单独制作移动端页面为例:
- 制作HTML页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no,maximum=1.0,minimum=1.0">
<title>Demo</title>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/index.css">
</head>
<body>
</body>
</html>
-
CSS初始化
-
初始化清除默认样式的css–normalize.css推荐使用,官网地址http://necolas.github.io/normalize.css/
/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */ /* Document ========================================================================== */ /** * 1. Correct the line height in all browsers. * 2. Prevent adjustments of font size after orientation changes in iOS. */ html { line-height: 1.15; /* 1 */ -webkit-text-size-adjust: 100%; /* 2 */ } /* Sections ========================================================================== */ /** * Remove the margin in all browsers. */ body { margin: 0; } /** * Render the `main` element consistently in IE. */ main { display: block; } /** * Correct the font size and margin on `h1` elements within `section` and * `article` contexts in Chrome, Firefox, and Safari. */ h1 { font-size: 2em; margin: 0.67em 0; } /* Grouping content ========================================================================== */ /** * 1. Add the correct box sizing in Firefox. * 2. Show the overflow in Edge and IE. */ hr { box-sizing: content-box; /* 1 */ height: 0; /* 1 */ overflow: visible; /* 2 */ } /** * 1. Correct the inheritance and scaling of font size in all browsers. * 2. Correct the odd `em` font sizing in all browsers. */ pre { font-family: monospace, monospace; /* 1 */ font-size: 1em; /* 2 */ } /* Text-level semantics ========================================================================== */ /** * Remove the gray background on active links in IE 10. */ a { background-color: transparent; } /** * 1. Remove the bottom border in Chrome 57- * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari. */ abbr[title] { border-bottom: none; /* 1 */ text-decoration: underline; /* 2 */ text-decoration: underline dotted; /* 2 */ } /** * Add the correct font weight in Chrome, Edge, and Safari. */ b, strong { font-weight: bolder; } /** * 1. Correct the inheritance and scaling of font size in all browsers. * 2. Correct the odd `em` font sizing in all browsers. */ code, kbd, samp { font-family: monospace, monospace; /* 1 */ font-size: 1em; /* 2 */ } /** * Add the correct font size in all browsers. */ small { font-size: 80%; } /** * Prevent `sub` and `sup` elements from affecting the line height in * all browsers. */ sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; } sub { bottom: -0.25em; } sup { top: -0.5em; } /* Embedded content ========================================================================== */ /** * Remove the border on images inside links in IE 10. */ img { border-style: none; } /* Forms ========================================================================== */ /** * 1. Change the font styles in all browsers. * 2. Remove the margin in Firefox and Safari. */ button, input, optgroup, select, textarea { font-family: inherit; /* 1 */ font-size: 100%; /* 1 */ line-height: 1.15; /* 1 */ margin: 0; /* 2 */ } /** * Show the overflow in IE. * 1. Show the overflow in Edge. */ button, input { /* 1 */ overflow: visible; } /** * Remove the inheritance of text transform in Edge, Firefox, and IE. * 1. Remove the inheritance of text transform in Firefox. */ button, select { /* 1 */ text-transform: none; } /** * Correct the inability to style clickable types in iOS and Safari. */ button, [type="button"], [type="reset"], [type="submit"] { -webkit-appearance: button; } /** * Remove the inner border and padding in Firefox. */ button::-moz-focus-inner, [type="button"]::-moz-focus-inner, [type="reset"]::-moz-focus-inner, [type="submit"]::-moz-focus-inner { border-style: none; padding: 0; } /** * Restore the focus styles unset by the previous rule. */ button:-moz-focusring, [type="button"]:-moz-focusring, [type="reset"]:-moz-focusring, [type="submit"]:-moz-focusring { outline: 1px dotted ButtonText; } /** * Correct the padding in Firefox. */ fieldset { padding: 0.35em 0.75em 0.625em; } /** * 1. Correct the text wrapping in Edge and IE. * 2. Correct the color inheritance from `fieldset` elements in IE. * 3. Remove the padding so developers are not caught out when they zero out * `fieldset` elements in all browsers. */ legend { box-sizing: border-box; /* 1 */ color: inherit; /* 2 */ display: table; /* 1 */ max-width: 100%; /* 1 */ padding: 0; /* 3 */ white-space: normal; /* 1 */ } /** * Add the correct vertical alignment in Chrome, Firefox, and Opera. */ progress { vertical-align: baseline; } /** * Remove the default vertical scrollbar in IE 10+. */ textarea { overflow: auto; } /** * 1. Add the correct box sizing in IE 10. * 2. Remove the padding in IE 10. */ [type="checkbox"], [type="radio"] { box-sizing: border-box; /* 1 */ padding: 0; /* 2 */ } /** * Correct the cursor style of increment and decrement buttons in Chrome. */ [type="number"]::-webkit-inner-spin-button, [type="number"]::-webkit-outer-spin-button { height: auto; } /** * 1. Correct the odd appearance in Chrome and Safari. * 2. Correct the outline style in Safari. */ [type="search"] { -webkit-appearance: textfield; /* 1 */ outline-offset: -2px; /* 2 */ } /** * Remove the inner padding in Chrome and Safari on macOS. */ [type="search"]::-webkit-search-decoration { -webkit-appearance: none; } /** * 1. Correct the inability to style clickable types in iOS and Safari. * 2. Change font properties to `inherit` in Safari. */ ::-webkit-file-upload-button { -webkit-appearance: button; /* 1 */ font: inherit; /* 2 */ } /* Interactive ========================================================================== */ /* * Add the correct display in Edge, IE 10+, and Firefox. */ details { display: block; } /* * Add the correct display in all browsers. */ summary { display: list-item; } /* Misc ========================================================================== */ /** * Add the correct display in IE 10+. */ template { display: none; } /** * Add the correct display in IE 10. */ [hidden] { display: none; }
-
特殊样式,在首页或者公共页面添加
/*CSS3盒子模型 */ box-sizing:border-box; -webkit- box-sizing:border-box; /*点击高亮我们需要清除清除 设置为transparent 完成透明*/ *{ -webkit-tap-highlight-color: transparent; } /*在移动端浏览器默认的外观在iOS上加上这个属性才能给按钮和输入框自定义样式*/ input{ -webkit-appearance: none; } /*禁用长按页面时的弹出菜单*/ img,a { -webkit-touch-callout: none; }
-
-
使用盒模型,多种布局方法及H5,CSS3进行开发
常见问题
- 移动浏览器基本以webkit内核为准(可以使用H5和CSS3),因此需要考虑webkit兼容问题(添加-webkit-)。
- CSS3盒模型 box-sizing
- 传统模型宽度: 盒子宽=CSS设置的width+border+padding
- CSS3盒模型: 盒子宽=CSS设置的width(包含border和padding)
- 移动端可以全部使用CSS3盒模型;PC端如果需要完全兼容,使用传统,否则使用CSS3盒模型
- 二倍图
- 物理像素&物理像素比
- 物理像素点指的是屏幕显示的最小颗粒,是物理真实存在的。
- PC端页面,1个px等于1个物理像素点,但是移动端不尽相同。(早期手机屏幕相等)
- 一个px能显示的物理像素点的个数,称为物理像素比或屏幕像素比
- Retina(视网膜屏幕)是一种显示技术,可以把更多的物理像素点压缩搭至一块屏幕从而达到更高的分辨率。
- 多倍图
-
在标准的viewport设置中,使用倍图来提高图片质量,解决在高清设备中的模糊问题
-
通常使用二倍图,因为iPhone 6\7\8 的影响,但是现在还存在3倍图4倍图的情况,实际看开发公司需求
-
背景图注意缩放
/*在iphone 8*/ img{ /*原始图片100*100*/ width:50px; height:50px; } .box{ /*原始图片100*100*/ background-size:50px 50px; }
-
- 物理像素&物理像素比