移动端h5技巧总结

虽说目前有很多移动端的框架, 但对于一些小项目来说, 未免会有些重, 那手写移动端网站时都有哪些技巧与必备的常识呢? 现总结如下:

meta标签

  • 添加viewport标签

    1
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />

    详细属性:

    1
    2
    3
    4
    5
    6
    width  ----  viewport的宽度(width=device-width意思是:宽度等于设备宽度)
    height ------ viewport的高度(height=device-height意思是:高度等于设备宽度)
    initial-scale ----- 初始的缩放比例
    minimum-scale ----- 允许用户缩放到的最小比例
    maximum-scale ----- 允许用户缩放到的最大比例
    user-scalable ----- 用户是否可以手动缩放
  • 禁止将数字变为电话号码

    1
    <meta name="format-detection" content="telephone=no" />

    一般情况下,IOS和Android系统都会默认某长度内的数字为电话号码,即使不加也是会默认显示为电话的,so,取消这个很有必要!

  • iphone设备中的safari私有meta标签

    1
    <meta name="apple-mobile-web-app-capable" content="yes" />

    它表示:允许全屏模式浏览,隐藏浏览器导航栏

  • iphone的私有标签

    1
    <meta name="apple-mobile-web-app-status-bar-style" content="black">

    它指定的iphone中safari顶端的状态条的样式
    默认值为default(白色),可以定为black(黑色)和black-translucent(灰色半透明)

  • 另外还有一个个性化的link标签,它支持用户将网页创建快捷方式到桌面时,其图标变为我们自己定义的图标。比如手机腾讯网上的标签:

    1
    <link rel="apple-touch-icon-precomposed" href="http://3gimg.qq.com/wap30/info/info5/img/logo_icon.png">

    不过腾讯对这个png图标的命名并不规范,常规我们要求文件名应为 apple-touch-icon.png 或 apple-touch-icon-precomposed.png ,前者的命名iOS会为这个图标自动添加圆角、阴影和高亮覆盖层,后者则不会添加这些效果。

rem神器

关于rem的详解请戳这里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//根据屏幕大小计算根font-size, 实现全适配
<script>
(function (doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
docEl.style.fontSize = 20 * (clientWidth / 320) + 'px';
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);
</script>

手机网站框架代码

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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>手机网站</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<meta name="format-detection" content="telephone=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="author" content="oulafen, oulafen.com" />
<style>
body{font-size:62.5%;font-family:"Microsoft YaHei",Arial; overflow-x:hidden; overflow-y:auto;}
</style>
</head>

<body>
<div>
大家好!我是oulafen!
</div>
</body>

<script>
(function (doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
docEl.style.fontSize = 20 * (clientWidth / 320) + 'px';
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);
</script>
</html>

手机端的调试

一般我们采用的:在浏览器调试+真机测试,因为浏览器毕竟只是一个模拟工具,实际开发的话,我们还得用真机去测试。

而在浏览器上测试,可以chrome(谷歌浏览器)的开发者工具:有个手机样的小图标,点击就能模拟手机测试。

持续更新中…