移动端开发规范规范

首先了解下移动web带来的问题

  • 设备更新换代快——低端机遗留下问题、高端机带来新挑战
  • 浏览器厂商不统一——兼容问题多
  • 网络更复杂——弱网络,页面打开慢
  • 低端机性能差——页面操作卡顿
  • HTML5新技术多——学习成本不低
  • 未知问题——坑多 因此本章节主要是制定一套移动端开发的规范,以减少大家在开发界面时走的弯路。

htm页面结构

标准的html结构

<!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>
<link rel="stylesheet" href="index.css">
</head>

<body>
这里开始内容
</body>

</html>

其中,不同的Mete说明

  • H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 ```

  • 忽略将页面中的数字识别为电话号码

    <meta name="format-detection" content="telephone=no" />
    
  • 忽略Android平台中对邮箱地址的识别

    <meta name="format-detection" content="email=no" />
    
  • 当网站添加到主屏幕快速启动方式,可隐藏地址栏,仅针对ios的safari

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

css规范说明

css的规范说明如下

  • 为减少请求数量,请尽量将多个css合并成一个css中,进行引用。
  • 如果实在无法合并成一个css,则将必须要引入的css按顺序依次引入,最后引入当前页面所对应的css。
  • 如果样式是自己重新写的,并未用任何的开源框架(比如bootstrap等),需要引入normalize.css,源码如下:
/*! normalize.css v3.0.3 | MIT License | github.com/necolas/normalize.css */

/**
 * @file:      normalize-zh.css v3.0.3
 * @author:    alsiso
 * @update:    2015年7月24日 08:25:13;
 * @copyright: 基于 normalize.css 源码解读与注释翻译 | MIT License     
*/


/**
 * 1. 设置所有的字体为'sans-serif'
 * 2. 防止 iOS 横屏字号放大,同时保证在PC上 zoom 功能正常
 */

html {
  font-family: sans-serif; /* 1 */
  -ms-text-size-adjust: 100%; /* 2 */
  -webkit-text-size-adjust: 100%; /* 2 */
}

/**
 * 修复默认边距,统一各浏览器效果
 */

body {
  margin: 0;
}

/* HTML5 显示属性定义
   ========================================================================== */
/**
 * 修复IE 8/9,HTML5新元素不能正确显示的问题,定义为 block的元素
 * 修复IE 10/11,'details' or 'summary' 定义为 block 的元素
 * 修复IE 11,'main'定义为 block 的元素
 */

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
main,
menu,
nav,
section,
summary {
  display: block;
}

/**
 * 1. 修复IE 8/9,HTML5新元素不能正确显示的问题,定义为inline-block元素
 * 2. 修复Chrome, Firefox, 和Opera的'progress'元素没有以baseline垂直对齐
 */

audio,
canvas,
progress,
video {
  display: inline-block; /* 1 */
  vertical-align: baseline; /* 2 */
}

/**
 * 对不支持'controls'属性的浏览器,'audio'元素给以隐藏
 * 移除iOS5设备中多余的高度
 */

audio:not([controls]) {
  display: none;
  height: 0;
}

/**
 * 修复 IE 7/8/9,Firefox 3 和 Safari 4 中 「hidden」属性不起作用的问题
 * 在IE、Safari、Firefox 22- 中隐藏「template」元素
 */

[hidden],
template {
  display: none;
}

/* 链接
   ========================================================================== */

/**
 * 去掉 IE 10+ 点击链接时的灰色背景
 */

a {
  background-color: transparent;
}

/**
 * 去掉点击时的焦点框,同时保证使用键盘可以显示焦点框
 */

a:active,
a:hover {
  outline: 0;
}

/* 语义化文字标签修复
   ========================================================================== */

/**
 * 修正「abbr」元素在 Firefox 外其他浏览器没有下划线的问题
 */

abbr[title] {
  border-bottom: 1px dotted;
}

/**
 * Firefox3+,Safari4/5 和 Chrome 中统一设置为粗体
 */

b,
strong {
  font-weight: bold;
}

/**
 * 修正 Safari5 和 Chrome 中没有样式的问题
 */

dfn {
  font-style: italic;
}

/**
 * 对 h1 样式进行重置
 */

h1 {
  font-size: 2em;
  margin: 0.67em 0;
}

/**
 * 修正 IE6-11 中没有样式的问题
 */

mark {
  background: #ff0;
  color: #000;
}

/**
 * 统一 small 的字体大小
 */

small {
  font-size: 80%;
}

/**
 * 防止所有浏览器中的<sub>和<sup>影响行高
 */

sub,
sup {
  font-size: 75%;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
}

sup {
  top: -0.5em;
}

sub {
  bottom: -0.25em;
}

/* 嵌入元素
   ========================================================================== */

/**
 * 去除 IE6-9 和 Firefox 3 中 a 内部 img 元素默认的边框
 */

img {
  border: 0;
}

/**
 * 修复 IE9 中的「overflow」的怪异表现
 */

svg:not(:root) {
  overflow: hidden;
}

/* 内容分组
   ========================================================================== */

/**
 * 修复 IE 8/9、Safari中margin失效
 */

figure {
  margin: 1em 40px;
}

/**
 * 修正 Firefox 和其他浏览器之间的差异
 */

hr {
  box-sizing: content-box;
  height: 0;
}

/**
 * 标签应当包含滚溢出
 */

pre {
  overflow: auto;
}

/**
 * 统一代码的字体设置
 */

code,
kbd,
pre,
samp {
  font-family: monospace, monospace;
  font-size: 1em;
}

/* 表单
   ========================================================================== */

/**
 * 1. 修正所有浏览器中颜色不继承的问题
 * 2. 修正所有浏览器中字体不继承的问题
 * 3. 修正 Firefox 3+, Safari5 和 Chrome 中外边距不同的问题
 */

button,
input,
optgroup,
select,
textarea {
  color: inherit; /* 1 */
  font: inherit; /* 2 */
  margin: 0; /* 3 */
}

/**
 * 统一 IE 8/9/10/11 `overflow`属性为`visible`
 */

button {
  overflow: visible;
}

/**
 * 统一各浏览器`text-transform`不会继承的问题
 */

button,
select {
  text-transform: none;
}

/**
 * 1. 避免 Android 4.0.* 中的 WebKit bug ,该bug会破坏原生的
   `audio`和`video`的控制器
 * 2. 更正 iOS 中无法设置可点击的`input`的问题
 * 3. 统一其他类型的`input`的光标样式
 */

button,
html input[type="button"], /* 1 */
input[type="reset"],
input[type="submit"] {
  -webkit-appearance: button; /* 2 */
  cursor: pointer; /* 3 */
}

/**
 * 重置按钮禁用时光标样式
 */

button[disabled],
html input[disabled] {
  cursor: default;
}

/**
 * 移除 Firefox 4+ 的内边距
 */

button::-moz-focus-inner,
input::-moz-focus-inner {
  border: 0;
  padding: 0;
}

/**
 * 统一设置`input`行高为`normal`
 */

input {
  line-height: normal;
}

/**
 *
 * 1. 修正 IE 8/9 box-sizing 被设置为「content-box」的问题
 * 2. 移除 IE 8/9 中多余的内边距
 */

input[type="checkbox"],
input[type="radio"] {
  box-sizing: border-box; /* 1 */
  padding: 0; /* 2 */
}

/**
 * 修正 Chrome 中 input [type="number"] 在特定高度和`font-size`时,
 * 下面一个箭头光标变成`cursor: text`
 */

input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  height: auto;
}

/**
 * 1. 修正 Safari 5 和 Chrome 中`appearance`被设置为`searchfield`的问题
 * 2. 修正 Safari 5 和 Chrome 中`box-sizing`被设置为`border-box`的问题
 */

input[type="search"] {
  -webkit-appearance: textfield; /* 1 */
  box-sizing: content-box; /* 2 */
}

/**
 * 移除原生默认样式,统一search的输入框样式
 */

input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-decoration {
  -webkit-appearance: none;
}

/**
 * 定义一致的边框、外边距和内边距
 */

fieldset {
  border: 1px solid #c0c0c0;
  margin: 0 2px;
  padding: 0.35em 0.625em 0.75em;
}

/**
 * 1. 修正 IE 6-9 中颜色不能继承的问题
 * 2. 重置内边距
 */

legend {
  border: 0; /* 1 */
  padding: 0; /* 2 */
}

/**
 * 1. 移除 IE6-11 中默认的垂直滚动条
 */

textarea {
  overflow: auto;
}

/**
 * 统一设置optgroup元素font-weight始终为bold
 */
optgroup {
  font-weight: bold;
}

/* 表格
   ========================================================================== */

/**
 * 合并单元格边框,重置内边距
 */

table {
  border-collapse: collapse;
  border-spacing: 0;
}

td,
th {
  padding: 0;
}
  • 上线发布的css版本必须压缩(可用atom工具或者前端脚手架工具)。
  • 其余规范请参考3.1节。

js

js相应的开发规范说明如下:

  • 尽量不要讲js放在head标签引用,防止页面阻塞
  • js的引用顺序要注意,框架层的js必须先引用。
  • 尽量不要书写内联js代码,不易于js代码的管理,除非是页面初始化等首屏加载内容。

rem(可选)

设计时以750px宽度为准,采用网易的rem适配规则进行适配,使用如下:

  • 在页面内放置内联js代码,以做屏幕的自适应处理

    <script type="text/javascript">
      // 用来初始化root font-size
      var uAgent = window.navigator.userAgent;
      var isIOS = uAgent.match(/iphone/i);
      var isYIXIN = uAgent.match(/yixin/i);
      var is2345 = uAgent.match(/Mb2345/i);
      var ishaosou = uAgent.match(/mso_app/i);
      var isSogou = uAgent.match(/sogoumobilebrowser/ig);
      var isLiebao = uAgent.match(/liebaofast/i);
      var isGnbr = uAgent.match(/GNBR/i);
    
      function resizeRoot() {
        var wWidth = (screen.width > 0) ? (window.innerWidth >= screen.width || window.innerWidth == 0) ? screen.width :
          window.innerWidth : window.innerWidth,
          wDpr, wFsize;
        var wHeight = (screen.height > 0) ? (window.innerHeight >= screen.height || window.innerHeight == 0) ?
          screen.height : window.innerHeight : window.innerHeight;
        if (window.devicePixelRatio) {
          wDpr = window.devicePixelRatio;
        } else {
          wDpr = isIOS ? wWidth > 818 ? 3 : wWidth > 480 ? 2 : 1 : 1;
        }
        if (isIOS) {
          wWidth = screen.width;
          wHeight = screen.height;
        }
        if (wWidth > wHeight) {
          wWidth = wHeight;
        }
        wFsize = wWidth > 1080 ? 144 : wWidth / 7.5;
        wFsize = wFsize > 32 ? wFsize : 32;
        window.screenWidth_ = wWidth;
        if (isYIXIN || is2345 || ishaosou || isSogou || isLiebao || isGnbr) { //YIXIN 和 2345 这里有个刚调用系统浏览器时候的bug,需要一点延迟来获取
          setTimeout(function() {
            wWidth = (screen.width > 0) ? (window.innerWidth >= screen.width || window.innerWidth == 0) ?
              screen.width : window.innerWidth : window.innerWidth;
            wHeight = (screen.height > 0) ? (window.innerHeight >= screen.height || window.innerHeight ==
              0) ? screen.height : window.innerHeight : window.innerHeight;
            wFsize = wWidth > 1080 ? 144 : wWidth / 7.5;
            wFsize = wFsize > 32 ? wFsize : 32;
            document.getElementsByTagName('html')[0].style.fontSize = wFsize + 'px';
            document.getElementById("fixed").style.display = "none";
          }, 500);
        } else {
          document.getElementsByTagName('html')[0].style.fontSize = wFsize + 'px';
        }
      }
      resizeRoot();
    </script>
    
  • 制作设计稿时,以750px宽度为准,实际的rem值即为像素值/100。例如,设计稿的样式为15px,则开发时写的宽度即为0.15rem。

总结

综上所述,整个移动端手机页面的标准格式如下

<!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>
<link rel="stylesheet" href="public/css/main.css">
<link rel="stylesheet" href="css/main.css">
  <script type="text/javascript">
    // 用来初始化root font-size
    var uAgent = window.navigator.userAgent;
    var isIOS = uAgent.match(/iphone/i);
    var isYIXIN = uAgent.match(/yixin/i);
    var is2345 = uAgent.match(/Mb2345/i);
    var ishaosou = uAgent.match(/mso_app/i);
    var isSogou = uAgent.match(/sogoumobilebrowser/ig);
    var isLiebao = uAgent.match(/liebaofast/i);
    var isGnbr = uAgent.match(/GNBR/i);

    function resizeRoot() {
      var wWidth = (screen.width > 0) ? (window.innerWidth >= screen.width || window.innerWidth == 0) ? screen.width :
        window.innerWidth : window.innerWidth,
        wDpr, wFsize;
      var wHeight = (screen.height > 0) ? (window.innerHeight >= screen.height || window.innerHeight == 0) ?
        screen.height : window.innerHeight : window.innerHeight;
      if (window.devicePixelRatio) {
        wDpr = window.devicePixelRatio;
      } else {
        wDpr = isIOS ? wWidth > 818 ? 3 : wWidth > 480 ? 2 : 1 : 1;
      }
      if (isIOS) {
        wWidth = screen.width;
        wHeight = screen.height;
      }
      if (wWidth > wHeight) {
        wWidth = wHeight;
      }
      wFsize = wWidth > 1080 ? 144 : wWidth / 7.5;
      wFsize = wFsize > 32 ? wFsize : 32;
      window.screenWidth_ = wWidth;
      if (isYIXIN || is2345 || ishaosou || isSogou || isLiebao || isGnbr) { //YIXIN 和 2345 这里有个刚调用系统浏览器时候的bug,需要一点延迟来获取
        setTimeout(function() {
          wWidth = (screen.width > 0) ? (window.innerWidth >= screen.width || window.innerWidth == 0) ?
            screen.width : window.innerWidth : window.innerWidth;
          wHeight = (screen.height > 0) ? (window.innerHeight >= screen.height || window.innerHeight ==
            0) ? screen.height : window.innerHeight : window.innerHeight;
          wFsize = wWidth > 1080 ? 144 : wWidth / 7.5;
          wFsize = wFsize > 32 ? wFsize : 32;
          document.getElementsByTagName('html')[0].style.fontSize = wFsize + 'px';
          document.getElementById("fixed").style.display = "none";
        }, 500);
      } else {
        document.getElementsByTagName('html')[0].style.fontSize = wFsize + 'px';
      }
    }
    resizeRoot();
  </script>
</head>

<body>
这里开始内容
  <script type="text/javascript" src="public/js/zepto.min.js"></script>
  <script type="text/javascript" src="js/main.min.js"></script>
</body>

</html>

results matching ""

    No results matching ""