腾讯代码规范参考
页面规范
HTML
HTML标签
- 标签必须合法且闭合、嵌套正确,标签名需小写
- 标签语法无错误,需要符合语义化
- 标签的自定义属性以
data-
开头,如:<a href="#" data-num='18'></a>
- 除非有特定的功能、组件要求等,禁止随意使用id来定义元素样式
链接
- 给
<a>
标签加上title属性
<a>
标签的href
属性必须写上链接地址,暂无的加上javascript:alert('敬请期待!')
- 非本专题的页面间跳转,采用打开新窗口模式:
target="_blank"
https协议自适应
将调用静态域名
ossweb-img.qq.com
以及 game.gtimg.cn
的外部请求,写法上一律去掉协议http:
部分,采用自适应的写法。具体方法如下:<style> //CSS背景图片 .bg{background: url(//game.gtimg.cn/images/cf/cp/a20161021sqjs/hd.jpg) no-repeat;} </style> //链接URL <a href="//cf.qq.com/main.shtml">进入官网</a> //图片SRC <img src="//game.gtimg.cn/images/cf/web201610/logo.png"> //JS链接 <script src="//ossweb-img.qq.com/images/js/title.js"></script>
css
选择器
- CSS类名命名参考注释与命名
- 禁止使用层级过深的选择器,最多3级。
- 除非有特定的功能、组件要求等,禁止随意使用id来定义元素样式
- 除非是样式reset需要,禁止对纯元素选择器设置特定样式,避免样式污染
reset示例
- pc端
body,dl,dd,ul,ol,h1,h2,h3,h4,h5,h6,p,form,header,section,article,footer{margin:0;} body,button,input,select,textarea{font:12px/1.5 tahoma,'\5FAE\8F6F\96C5\9ED1',sans-serif} h1,h2,h3,h4,h5,h6{font-size:100%} em,b{font-style:normal} a{text-decoration:none} a:hover{text-decoration:underline} img{border:0} body{padding-top:42px} button,input,select,textarea{font-size:100%;outline:none} table{border-collapse:collapse;border-spacing:0} td,th,ul,ol{padding:0}
- 移动端
有较多文字的文章类页面:
/* 移动端常用reset.css (文字版本) */ /* reset */ html,body,div,p,ul,li,dl,dt,dd,em,i,span,a,img,input,h1,h2,h3,h4,h5 {margin:0;padding:0} a,img,input {border:none;} body{font: 14px/1.75 -apple-system, "Helvetica Neue", Helvetica, Arial, sans-serif;} a {text-decoration:none;} ul,li{list-style: none}
如果页面无文字,或者不希望文字被长按选中,可使用下面的reset;适合于大多数专题页面
/* 移动端常用reset.css (无文字版本) */ /* reset */ html,body,div,p,ul,li,dl,dt,dd,em,i,span,a,img,input,h1,h2,h3,h4,h5 {margin:0;padding:0} a,img,input {border:none;} body{font: 14px/1.75 -apple-system, "Helvetica Neue", Helvetica, Arial, sans-serif;-webkit-tap-highlight-color: rgba(0,0,0,0);} a {text-decoration:none;} ul,li{list-style: none} a, img {-webkit-touch-callout: none; /* 禁止长按链接与图片弹出菜单 */} html, body { -webkit-user-select: none; /* 禁止选中文本(如无文本选中需求,此为必选项) */ user-select: none; }
命名
样式命名
- class、id都需小写
- 命名使用英文,禁止使用特殊字符
- 样式名不能包含
ad
、guanggao
、ads
、gg
是广告含义的关键词,避免元素被网页拓展、插件屏蔽
- 名称间隔使用
符号
- 涉及数据、交互等需要联调的部分,禁止通过id选择器定义样式,以免开发过程中id名变化,引起页局错乱
- 类名命名需要语义化,参考下面的示例:
.wrap{} //外层容器 .mod-box{} //模块容器 .btn-start{} //开始 .btn-download-ios{} //ios下载 .btn-download-andriod{} //安卓下载 .btn-head-nav1{} //头部导航链 .btn-news-more{} //更多新闻 .btn-play{} //播放视频 .btn-ico{} //按钮ico .btn-lottery{} //开始抽奖 .side-nav{} //侧栏导航 .side-nav-btn1{} //侧栏导航-链接1 .btn-more{} //更多
图片命名
- 图片名称必须小写,禁止使用特殊字符、中文
- 使用英文或拼音缩写,禁止特殊字符
- 名称间隔使用
符号
- 命名需要能体现图片的大概用途
- 禁止文件名和实际图片内容不符。反面例子:图片名为'weixin-qrcode',图片内容却是头像。
bg.jpg //背景图片 mod-bg.jpg //模块背景 sprites.png //精灵图 btn-start.png //开始按钮 ico-play.png //修饰性图片
文件命名
所有文件名统一使用小写,首页命名为
index.html
html内页,有明显分类的,参考使用以下示例命名,无明确意义的,可用page01.html、page02.html,禁止使用特殊字符,统一使用小写字母常用命名:专题名称 | 描述 |
index.html | 引导页&首页 |
main.html | 首页 |
news.html | 资讯页 |
newsdetail.html | 资讯详情页 |
raiders.html | 攻略页 |
raidersdetail.html | 攻略页详情页 |
download.html | 下载页面 |
actlist.html | 活动列表页面 |
video.html | 视频 |
cdkey.html | CDKEY兑换页 |
wallpaper.html | 壁纸页面 |
rem布局
- viewport
- <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
- 初始字号
css中设定html根元素的初始字体大小为100px
html{font-size:100px;}
- 单位转换
不管你拿到的设计稿宽度是640px还是750px,多大都一样。和我们平时做PC页面的做法基本一样,只需要把单位px换算成rem,所有设计稿的的元素大小全除以100单位换成rem,例如设计稿上个某个文字的大小为30px,直接写font-size:0.3rem。
- 示例代码
用
onorientationchange
函数来检测屏幕旋转,在一些APP或游戏内嵌页面会有该函数不会执行、orientation获取不到的情况。所以如果是游戏app内嵌页建议使用 resize
事件,检查宽高变化来检测屏幕是否旋转。<script> //屏幕适应 (function (win, doc) { if (!win.addEventListener) return; var html = document.documentElement; function setFont() { var html = document.documentElement; var k = 640; html.style.fontSize = html.clientWidth / k * 100 + "px"; } setFont(); setTimeout(function () { setFont(); }, 300); doc.addEventListener('DOMContentLoaded', setFont, false); win.addEventListener('resize', setFont, false); win.addEventListener('load', setFont, false); })(window, document); </script>
京东代码规范参考
css代码风格
单个css选择器或新申明开启新行
css属性值需要用到引号时,统一使用单引号
属性书写顺序
建议遵循以下顺序:
- 布局定位属性:display / position / float / clear / visibility / overflow
- 自身属性:width / height / margin / padding / border / background
- 文本属性:color / font / text-decoration / text-align / vertical-align / white- space / break-word
- 其他属性(CSS3):content / cursor / border-radius / box-shadow / text-shadow / background:linear-gradient …
CSS3浏览器私有前缀写法
CSS3 浏览器私有前缀在前,标准前缀在后
.jdc { -webkit-border-radius: 10px; -moz-border-radius: 10px; -o-border-radius: 10px; -ms-border-radius: 10px; border-radius: 10px; }
媒体查询
设备尺寸参考 :Mobile devices
媒体查询类型浏览器支持情况:CSS3 Media Queries overview
常用查询语句
判断设备横竖屏
/* 横屏 */ @media all and (orientation :landscape) { } /* 竖屏 */ @media all and (orientation :portrait) { }
判断设备宽高
/* 设备宽度大于 320px 小于 640px */ @media all and (min-width:320px) and (max-width:640px) { }
判断设备像素比
/* 设备像素比为 1 */ @media only screen and (-webkit-min-device-pixel-ratio: 1), only screen and (min-device-pixel-ratio: 1) { } /* 设备像素比为 1.5 */ @media only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min-device-pixel-ratio: 1.5) { } /* 设备像素比为 2 */ @media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2) { }
常用设备设置
iPhones
/* ----------- iPhone 4 and 4S ----------- */ /* Portrait and Landscape */ @media only screen and (min-device-width: 320px) and (max-device-width: 480px) and (-webkit-min-device-pixel-ratio: 2) { } /* Portrait */ @media only screen and (min-device-width: 320px) and (max-device-width: 480px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: portrait) { } /* Landscape */ @media only screen and (min-device-width: 320px) and (max-device-width: 480px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: landscape) { } /* ----------- iPhone 5 and 5S ----------- */ /* Portrait and Landscape */ @media only screen and (min-device-width: 320px) and (max-device-width: 568px) and (-webkit-min-device-pixel-ratio: 2) { } /* Portrait */ @media only screen and (min-device-width: 320px) and (max-device-width: 568px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: portrait) { } /* Landscape */ @media only screen and (min-device-width: 320px) and (max-device-width: 568px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: landscape) { } /* ----------- iPhone 6 ----------- */ /* Portrait and Landscape */ @media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (-webkit-min-device-pixel-ratio: 2) { } /* Portrait */ @media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: portrait) { } /* Landscape */ @media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: landscape) { } /* ----------- iPhone 6+ ----------- */ /* Portrait and Landscape */ @media only screen and (min-device-width: 414px) and (max-device-width: 736px) and (-webkit-min-device-pixel-ratio: 3) { } /* Portrait */ @media only screen and (min-device-width: 414px) and (max-device-width: 736px) and (-webkit-min-device-pixel-ratio: 3) and (orientation: portrait) { } /* Landscape */ @media only screen and (min-device-width: 414px) and (max-device-width: 736px) and (-webkit-min-device-pixel-ratio: 3) and (orientation: landscape) { }
Galaxy Phones
/* ----------- Galaxy S3 ----------- */ /* Portrait and Landscape */ @media screen and (device-width: 320px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 2) { } /* Portrait */ @media screen and (device-width: 320px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 2) and (orientation: portrait) { } /* Landscape */ @media screen and (device-width: 320px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 2) and (orientation: landscape) { } /* ----------- Galaxy S4 ----------- */ /* Portrait and Landscape */ @media screen and (device-width: 320px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 3) { } /* Portrait */ @media screen and (device-width: 320px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 3) and (orientation: portrait) { } /* Landscape */ @media screen and (device-width: 320px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 3) and (orientation: landscape) { } /* ----------- Galaxy S5 ----------- */ /* Portrait and Landscape */ @media screen and (device-width: 360px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 3) { } /* Portrait */ @media screen and (device-width: 360px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 3) and (orientation: portrait) { } /* Landscape */ @media screen and (device-width: 360px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 3) and (orientation: landscape) { }
HTC Phones
/* ----------- HTC One ----------- */ /* Portrait and Landscape */ @media screen and (device-width: 360px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 3) { } /* Portrait */ @media screen and (device-width: 360px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 3) and (orientation: portrait) { } /* Landscape */ @media screen and (device-width: 360px) and (device-height: 640px) and (-webkit-device-pixel-ratio: 3) and (orientation: landscape) { }
iPads
/* ----------- iPad mini ----------- */ /* Portrait and Landscape */ @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (-webkit-min-device-pixel-ratio: 1) { } /* Portrait */ @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: portrait) and (-webkit-min-device-pixel-ratio: 1) { } /* Landscape */ @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: landscape) and (-webkit-min-device-pixel-ratio: 1) { } /* ----------- iPad 1 and 2 ----------- */ /* Portrait and Landscape */ @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (-webkit-min-device-pixel-ratio: 1) { } /* Portrait */ @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: portrait) and (-webkit-min-device-pixel-ratio: 1) { } /* Landscape */ @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: landscape) and (-webkit-min-device-pixel-ratio: 1) { } /* ----------- iPad 3 and 4 ----------- */ /* Portrait and Landscape */ @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (-webkit-min-device-pixel-ratio: 2) { } /* Portrait */ @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: portrait) and (-webkit-min-device-pixel-ratio: 2) { } /* Landscape */ @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: landscape) and (-webkit-min-device-pixel-ratio: 2) { }
图片命名
命名顺序
图片命名建议以以下顺序命名:
图片业务(可选) +(mod_)图片功能类别(必选)+ 图片模块名称(可选) + 图片精度(可选)
- 图片业务:
- pp_:拍拍
- wx_:微信
- sq_:手Q
- jd_:京东商城
- …
- 图片功能类别:
- mod_:是否公共,可选
- icon:模块类固化的图标
- logo:LOGO类
- spr:单页面各种元素合并集合
- btn:按钮
- bg:可平铺或者大背景
- …
- 图片模块名称:
- goodslist:商品列表
- goodsinfo:商品信息
- userava tar:用户头像
- …
- 图片精度:
- 普清:@1x
- Retina:@2x | @3x
- …
如下面例子:
公共模块: wx_mod_btn_goodlist@2x.png wx_mod_btn_goodlist.png mod_btn_goodlist.png 非公共模块: wx_btn_goodlist@2x.png wx_btn_goodlist.png btn_goodlist.png
交叉业务协作
业务交叉协作的时候,为了避免图片命名冲突,建议图片名加上业务和模块前辍,如拍拍侧和手Q侧的业务交叉合作时,侧栏导航icon雪碧图命名:
推荐: pp_icon_mod_sidenav.png 不推荐: icon_mod_sidenav.png
处理高清图片的时候,命名应该加上图片相应的精度说明
推荐: jdc_logo@1x.png jdc_logo@2x.png 不推荐: jdc_logo.png jdc_logo_retina.png
ClassName命名
ClassName的命名应该尽量精短、明确,必须以字母开头命名,且全部字母为小写,单词之间统一使用下划线“_” 连接
命名原则
基于姓氏命名法(继承 + 外来)
ClassName | 含义 |
about | 关于 |
account | 账户 |
arrow | 箭头图标 |
article | 文章 |
aside | 边栏 |
audio | 音频 |
avatar | 头像 |
bg,background | 背景 |
bar | 栏(工具类) |
branding | 品牌化 |
crumb,breadcrumbs | 面包屑 |
btn,button | 按钮 |
caption | 标题,说明 |
category | 分类 |
chart | 图表 |
clearfix | 清除浮动 |
close | 关闭 |
col,column | 列 |
comment | 评论 |
community | 社区 |
container | 容器 |
content | 内容 |
copyright | 版权 |
current | 当前态,选中态 |
default | 默认 |
description | 描述 |
details | 细节 |
disabled | 不可用 |
entry | 文章,博文 |
error | 错误 |
even | 偶数,常用于多行列表或表格中 |
fail | 失败(提示) |
feature | 专题 |
fewer | 收起 |
field | 用于表单的输入区域 |
figure | 图 |
filter | 筛选 |
first | 第一个,常用于列表中 |
footer | 页脚 |
forum | 论坛 |
gallery | 画廊 |
group | 模块,清除浮动 |
header | 页头 |
help | 帮助 |
hide | 隐藏 |
hightlight | 高亮 |
home | 主页 |
icon | 图标 |
info,information | 信息 |
last | 最后一个,常用于列表中 |
links | 链接 |
login | 登录 |
logout | 退出 |
logo | 标志 |
main | 主体 |
menu | 菜单 |
meta | 作者、更新时间等信息栏,一般位于标题之下 |
module | 模块 |
more | 更多(展开) |
msg,message | 消息 |
nav,navigation | 导航 |
next | 下一页 |
nub | 小块 |
odd | 奇数,常用于多行列表或表格中 |
off | 鼠标离开 |
on | 鼠标移过 |
output | 输出 |
pagination | 分页 |
pop,popup | 弹窗 |
preview | 预览 |
previous | 上一页 |
primary | 主要 |
progress | 进度条 |
promotion | 促销 |
rcommd,recommendations | 推荐 |
reg,register | 注册 |
save | 保存 |
search | 搜索 |
secondary | 次要 |
section | 区块 |
selected | 已选 |
share | 分享 |
show | 显示 |
sidebar | 边栏,侧栏 |
slide | 幻灯片,图片切换 |
sort | 排序 |
sub | 次级的,子级的 |
submit | 提交 |
subscribe | 订阅 |
subtitle | 副标题 |
success | 成功(提示) |
summary | 摘要 |
tab | 标签页 |
table | 表格 |
txt,text | 文本 |
thumbnail | 缩略图 |
time | 时间 |
tips | 提示 |
title | 标题 |
video | 视频 |
wrap | 容器,包,一般用于最外层 |
wrapper | 容器,包,一般用于最外层 |