移动端页面开发总结

移动端页面开发资源总结及技巧

工作中常用到的移动端问题记录下来,以便后用。内容不完善,以后遇到问题会一直更新的。

一、meta标签

1、移动端页面设置视口宽度等于设备宽度,并禁止缩放(viewport视窗)

1
<meta name='viewport' content='width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no' />

2、移动端页面设置视口宽度等于定宽(如640px),并禁止缩放,常用于微信浏览器页面(通常移动端设置为640*1136是最为稳妥的方式)

1
<meta name='viewport' content='width=640,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no' />

3、禁止将页面中的数字识别为电话号码(format-detection格式检测)

1
<meta name='format-detection' content='telephont=no' />

4、忽略android平台中对邮箱地址的识别

1
<meta name='format-detection' content='email=no' />

5、当网站添加到主屏幕快速启动方式,可隐藏地址栏,仅针对ios的safari

1
2
<meta name='apple-mobile-web-app-capable' content='yes' />
<!-- ios7.0版本以后,safari已看不到效果 -->

6、将网站添加到主屏幕快速启动方式,仅针对ios的safari顶端状态条的样式

1
2
<meta name='apple-mobile-web-app-status-bar-style' content='black' />
<!-- 可选default、black、black-translucent -->

viewport 模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<meta content="email=no" name="format-detection">
<title>title</title>
<link rel="stylesheet" href="index.css">
</head>

<body>
content...
</body>

</html>

二、CSS样式技巧(CSS层叠样式表,描述HTML/XML文档的呈现,元素被渲染的问题。)

1、禁止ios和android用户选中文字

1
.css{-webkit-user-select:none}

2、禁止ios长按时触发系统的菜单,禁止ios&android长按时下载图片(callout调出)

1
.css{-webkit-touch-callout:none}

3、webkit去除表单的默认样式(appearance外观)

1
.css{-webkit-appearance:none;}

4、去除表单输入框placeholder的样式

1
2
input::-webkit-input-placeholder{color:#000}
input:focus::-webkit-input-placeholder:{color:#fff}

5、去除android、button、input标签被点击时产生的边框&去除ios a标签被点击时产生的半透明灰色背景

1
a,button,input{-webkit-tap-highlight-color:rgba(0,0,0,0)}

6、iOS使用-webkit-text-size-adjust禁止调整字体大小

1
body{-webkit-text-size-adjust:100% !important;}

7、android上去掉语音输入按钮(speech语音演讲)

1
input::-webkit-input-speech-button{display:none;}

8、移动端定义字体(移动端没有微软雅黑字体)

1
body{font-family:Helvetica;}

9、webkit内核浏览器的文字大小调整功能(webkit内核最小字体12px)

1
2
-webkit-text-size-adjust:none;
-webkit-transform:scale(0.9);

10、去掉 input[type=search] 搜索框默认的×号

1
2
3
4
5
6
input[type='search']{
-webkit-appearance:none;
input::-webkit-search-cancel-button{
display:none;
}
}

三、其他技巧

1、手机拍照和上传图片

1
2
3
4
选择照片:
<input type='file' accept='image/*' />
选择视频:
<input type='file' accept='video/*' />

2、取消input在ios下,输入的时候英文首字母的默认大写(autocorrect自动更正capitalize以大写字母写)

1
<input type='text' autocapitalize='off' autocorrect='off' />

3、打电话发短信

1
2
<a href='tel:10086'>打电话给:10086</a>
<a href='sms:10086'>发短信给:10086</a>

四、CSS reset

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* Colisy  */
@charset "utf-8";
/* reset */
html{
-webkit-text-size-adjust:none;
-webkit-user-select:none;
-webkit-touch-callout: none
font-family: Helvetica;
}
body{font-size:12px;}
body,h1,h2,h3,h4,h5,h6,p,dl,dd,ul,ol,pre,form,input,textarea,th,td,select{margin:0; padding:0; font-weight: normal;text-indent: 0;}
a,button,input,textarea,select{ background: none; -webkit-tap-highlight-color:rgba(255,0,0,0); outline:none; -webkit-appearance:none;}
em{font-style:normal}
li{list-style:none}
a{text-decoration:none;}
img{border:none; vertical-align:top;}
table{border-collapse:collapse;}
textarea{ resize:none; overflow:auto;}
/* end reset */

五、常用的公共CSS style

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@charset "UTF-8";
/* public */

/* 清除浮动 */
.clear{zoom:1}
.clear:after{content:"";display:block;clear:both;}

/* 定义盒模型为怪异盒模型 */
.boxSiz{
box-sizing: border-box;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
-ms-box-sizing:border-box;
-o-box-sizing:border-box;
}

/* 强制换行 */
.toWrap{
word-break: break-all; /* 只对英文起作用,以字母作为换行依据 */
word-wrap: break-word; /* 只对英文起作用,以单词作为换行依据 */
white-space: pre-wrap; /* 只对中文起作用,强制换行*/
}

/* 禁止换行 */
.noWrap{
white-space: nowrap;
}

/* 文字超出部分显示省略号 */
.noWrapEllipsis{
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

/* 多行显示省略号,@line是行数(https://www.html5rocks.com/zh/tutorials/flexbox/quick/)*/
.ellipsisLn{
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
/* line-clamp是一个不规范属性,必须与-webkit-结合。限制一个块元素文本显示的行数。与 display:-webkit-box;-webkit-box-orient必须结合*/
-webkit-line-clamp:@line;
}

/* 1px 边框解决方案,示例中设置上边框,可以调整 top、right、bottom、left 的值分别设置上下左右边框 */
#box2:after{
content: " ";
position: absolute;
left: 0;
top: 0;
right: 0;
height: 1px;
border-top: 1px solid #000;
color: #C7C7C7;
transform-origin: 0 0;
transform: scaleY(0.5);
}

/* 文字两端对齐,text-justify对齐行文本进行对齐和分隔,只有IE中起作用()*/
.text-justify{
text-align: justify;
text-justify:inter-ideograph;
}

/* 定义盒模型为 flex 布局兼容写法并让内容水平垂直居中*/
.flex-center{
display: box;
display: -webkit-box;
display: -ms-box;
display: -o-box;
display: -moz-box;

box-pack:center;
-webkit-box-pack:center;
-o-box-pack:center;
-ms-box-pack:center;
-moz-box-pack:center;

box-align:center;
-webkit-box-align:center;
-ms-box-align:center;
-moz-box-align:center;
-o-box-align:center;
}

/* End public */

六、flex布局(2009年盒模型,只兼容老式的手机,目前使用2012年的flex)

1、定义弹性盒模型兼容写法

1
2
3
4
5
6
7
8
9
/*
box
inline-box
*/
display: box;
display: -webkit-box;
display: -ms-box;
display: -o-box;
display: -moz-box;

2、box-orient 定义盒模型内伸缩项目的布局方向

1
2
3
4
5
6
7
8
9
/*
vertical column
horizontal row
*/
box-orient: horizontal;
-webkit-box-orient: horizontal;
-ms-box-orient: horizontal;
-o-box-orient: horizontal;
-moz-box-orient: horizontal;

3、box-direction 定义盒模型内伸缩项目的正序(normal默认值)、倒叙(reverse)

1
2
3
4
/* firefox */
-moz-box-direction:reverse;
/* chrome、safari、opera */
-webkit-box-direction:reverse;

4、box-pack 对盒子水平富裕空间的管理

1
2
3
4
5
6
7
8
9
10
11
/*
start
end
center
justify
*/
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-o-box-pack: center;
box-pack: center;

5、box-align 对盒子垂直富裕空间的管理

1
2
3
4
5
6
7
8
9
10
/*
start
end
center
*/
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-o-box-align: center;
box-align: center;

6、box-ordinal-group定义伸缩项目的具体位置(ordinal序数)

1
2
3
4
5
6
.box div:nth-of-type(1){
/* Safari 和 Chrome */
-webkit-box-ordinal-group:1;
/* Firefox */
-moz-box-ordinal-group:1;
}

7、box-flex定义伸缩项目占空间的份数

1
2
3
4
5
6
.box div:nth-of-type(1){
/* Safari 和 Chrome */
-webkit-box-flex:1;
/* Firefox */
-moz-box-flex:1;
}