一、规范目的
1.1 概述
- 提高团队协作效率
- 便于前端开发以及后期优化维护
- 方便新进的成员快速上手
- 输出高质量的代码
二、基本准则
三、文件规范
2.1文件命名规则
使用场景:在新建网页、图片、脚本、CSS 文件时,根据此规则给文件命名并放入指定位置
1、文件夹名称统一用小写的英文字母、和中划线的组合(home-page),其中不得包含汉字空格和特殊字符。
2、文件名称统一用小写的英文字母、和中划线的组合(home-page),其中不得包含汉字空格和特殊字符。
“按名称排例”的命令时,同一种大类的文件能够排列在一起,以便我们查找、修改、替换、计算负载量等等操作。
2.1.1HTML 的命名原则
\ abouts
\ product
html 文件,文件名统一用
index.html
index.asp。
2.1.2图片的命名原则
1.普通图片命名模块
_类别_功能_状态.png
banner
logo
button
menu
pic
title
banner_sohu.gif banner_sina.gif
menu_aboutus.gif menu_job.gif
title_news.gif logo_police.gif
logo_national.gif pic_people.jpg
2. icon命名
V1.2_20_20.png
V1.3_20_20.png
2.1.3. javascript 的命名原则
javascript 文件名为 ad.js 弹出窗口的 javascript 文件名为 pop.js
2.1.4动态语言文件命名原则
_描述,描述可以有多个单词,用“-”隔开,性质一般是该页面得概要。
register-form.asp
register-post.asp
topic-lock.asp
2.2文件存放位置规范
images
javascript 脚本 script
css 文件 css
2.3 CSS 书写规范
[使用场景:书写 CSS 样式时,根据此规则对 id 和 class 进行命名,并遵循书写规则,避免出现常犯错误,提高效率。]
2.3.1基本原则
CSS 样式可细分为 3 类:重新定义 HTML 样式、链接状态样式、自定义样式。
Ø 1. 重新定义 HTML 样式为设计师重新定义已有的 HTML 标签样式,影响全部的被设定标签样式,用于统一网页中某一标签的样式定义。
“HTML 标签”例:hr { border: 1px dotted #333333 }
Ø 2. 链接状态样式为设计师对链接不同状态设定特殊样式,影响被使用本样式区域中的链接。
2 种:a.nav:link(中间无空格) 、.nav a:link
<a>标签中;
<a>标签的其他标签。
Ø 3. 样式为设计师自定义的新CSS 样式,影响被使用本样式的区域,用于完成网页中局部的样式设定。
“.”+“相应样式效果描述的单词或缩写”例:“ .shadow ”
“.no”+“字号”+“行距”+“颜色缩写”例:“ .no12 ” 、“ .no12-24”
“.no12”类型一般出现在 base.css 文件中,只可选用,不可修改,避免名称含义和实际内容不符。自定义的样式名最好用意义命名。
red{color:red};
.important-news{color:red};
,所以尽量不要用意义不明确的作为自定义命名。
Ø 4. 页面内的样式加载必须用链接方式:
<link rel="stylesheet" type="text/css" href="style/style.css">
Ø 5. 重新定义的最先,伪类其次,自定义样式最后,便于自己和他人阅读。
Ø 6.
style=“color:red;”);
Ø 7. 所有装饰性的图片(如背景图),要定义在 CSS 中;
2.3.2注意细则
1. class与id命名: 大的框架命名比如header/footer/wrapper/left/right之类的由设计框架之人统一命名。其他样式名称由
&数字&-
, 避免使用中文拼音, 尽量使用简易的单词组合; 总之, 命名要语义化, 简明化。
2. 规避 class 与 id 重名 , 不建议使用 id 选择器。
a. 通过从属写法规避, 示例见 d;
b. 取父级元素 id/class 命名部分命名, 示例见 d;
c. 重复使用率高的命名,可加代号加下划线起始, 比如 i-clear;
d. a、b 两条, 适用于在中已建好框架的页面, 如, 要在中已建好框架的页面代码
<div id="mainnav"></div>中加入新的 div 元素,
a 命名法则: <div id="main-nav"><div class="first-nav">...</div></div>,
:[采取这种]
#main-nav
.first-nav{.......}
b 命名法则: <div id="mainnav"><div class="main_firstnav">...</div></div>,
:
.main_firstnav{.......}
3. css 属性书写顺序, 建议遵循
-->自身属性-->文本属性-->其他属性
, 但尽量保证同类属性写在一起。
①
Margin\padding\float\clear\position(相应的top,right,bottom,left)\display\visibility\overflow 等
②
Width\height\background\ border
③
font\color\text-align\text-decoration\text-indent\ white-space\othertext\content 等
④
list-style(列表样式)\vertical-align\cursor\z-index(层叠顺序)\zoom 等
4. 书写代码前, 考虑并提高样式重复使用率;
5. 杜绝使用<meta http-equiv="X-UA-Compatible" content="IE=7" /> 兼容 ie8;
6. 避免兼容性属性的使用, 比如 text-shadow || css3 的相关属性;
7. 减少使用影响性能的属性, 比如 position:absolute || float ;
8. 重要图片加上 alt 属性;重要的元素和截断的元素加上 title;
9. 避免使用!important,!important 的优先级最高;
10.
, 小区块适量注释;
11. 代码缩进与格式: 建议单行书写, 可根据自身习惯, 后期优化统一处理;
12. Margin、padding、border、font 等样式尽可能采用合并写法;
padding 和 Border-width 属性
;
Font 的简写对于缩小代码量很有帮劣,如果没有定义某个属性值的话,将继承父级元素的
2.3.3命名规则
CSS书写规范使用CSS缩写属性
!
CSS选择器命名规范
1.长名称或词组可以使用中横线来为选择器命名。
2.不建议使用“_”下划线来命名CSS选择器,为什么呢?
shift键;
(比如使用_tips的选择器命名,在IE6是无效的)
JavaScript变量命名(JS变量命名是用“_”)
Id
id在JS是唯一的,不能多次使用,而使用class类选择器却可以重复使用,另外id的优先级优先与class,所以id应该按需使用,而不能滥用。
“.is-”前缀。
class关键词:
布局类:header, footer, container, main, content, aside, page, section
包裹类:wrap, inner
区块类:region, block, box
结构类:hd, bd, ft, top, bottom, left, right, middle, col, row, grid, span
列表类:list, item, field
主次类:primary, secondary, sub, minor
大小类:s, m, l, xl, large, small
状态类:active, current, checked, hover, fail, success, warn, error, on, off
导航类:nav, prev, next, breadcrumb, forward, back, indicator, paging, first, last
交互类:tips, alert, modal, pop, panel, tabs, accordion, slide, scroll, overlay,
星级类:rate, star
分割类:group, seperate, divider
等分类:full, half, third, quarter
表格类:table, tr, td, cell, row
图片类:img, thumbnail, original, album, gallery
语言类:cn, en
论坛类:forum, bbs, topic, post
方向类:up, down, left, right
其他语义类:btn, close, ok, cancel, switch; link, title, info, intro, more, icon; form, label, search, contact, phone, date, email, user; view, loading...
.item-img
使用两个中划线表示特殊化,如.item-img.item-img--small表示在.item-img的基础上特殊化
状态类直接使用单词,参考上面的关键词,如.active, .checked
图标以icon-为前缀(字体图标采用.icon-font.i-name方式命名)。
模块采用关键词命名,如.slide, .modal, .tips, .tabs,特殊化采用上面两个中划线表示,如.imgslide--full, .modal--pay, .tips--up, .tabs--simple
js操作的类统一加上js-前缀
不要超过四个class组合使用,如.a.b.c.d
: container/wrap
header
main
sidebar
center
wrapper
content
footer
column
nav
mainbav/globalnav
topnav
subnav
sidebar
rightsidebar
menu
leftsidebar
sidebarIcon
submenu
: title
logo
login
loginbar
regsiter
products
products-prices
editor-review
news_release
:summary
products-review
news-release
/标语:banner
publisher
screenshot
faqs
keyword
blog
forum
search
search-input
search-results
status
search-output
joinus
btn
scroll
tab
list
msg/message
tips
: current
skin
pay
activities
promotion
announcement
company-profile
glories
ranking
equipment
culture
scale
sales-network
technology
enterprise-qualification
operation-principle
development-history
browse_by-category
human-resource-hr
customer_message
your-requirements
enterprise_establishing
online-communication
joinIn-cooperation
business-scope
contact-us
organization
branches
strength-strength
manager-oration
engineering-projects
application-fields
leader-oration
customer-service
sales-information
education-training
quality-certification
products-description
sales-sales
information
homepage
e-business
hot-link
trends
news-trends
sales-hotline
in-construction
add
order
copy -right
trade-news
postal-code-zipcode
company-name
contact-person
certificate
tel
fax
product_name
price
description
brand
specification
manufacuturer
item_no
description
application
on-line-order
general
size
model
technique-data
production-place
forum
bidInviting
achievements
trends
great-event
service
investment
programming
delivery
industry
environment
submit
reset
community
online-inquiry
feedback
general-profile
special-report
note
business
download
faq
amusement-park
: icon
guild
service
hot
news
download
label/branding
shop
vote
breadcrumb/loc
tag
siteinfo_credits
siteinfo-legal
friendlink
siteinfo
partner
copyright
2.4 html 书写规范
[使用场景:书写 html 页面时,根据此规则对 html 内容进行补充和完善。]
2.4.1 head 区代码规范
head 区是指 HTML 代码的<head>和</head>之间的内容。
1. 网页显示字符集
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv ="Content-Type" content ="text/html; charset=utf-8">
<meta http-equiv ="Content-Type" content ="text/html; charset=utf-8">
2. 搜索关键字(网站类型)
< meta name="keywords" content ="xxxx,xxxx,xxx,xxxxx,xxxx,">
3. 网页的 css 规范
4. 网页标题
<link href="../css/style.css" rel="stylesheet" type="text/css">
<title>xxxxxxxxxxxxxxxxxx</title>
1.公司版权注释
<!--- The site is designed by EHM,Inc 07/2005 --->
2. 网页制作者信息
<meta name="author" content="webmaster@maketown.com">
3. 网站简介
<meta name="DESCRIPTION" content ="xxxxxxxxxxxxxxxxxxxxxxxxxx">
4. 设定网页的到期时间。一旦网页过期,必须到服务器上重新调阅。
<meta http-equiv ="expires" content ="Wed, 26 Feb 1997 08:21:57 GMT">
5. 禁止浏览器从本地机的缓存中调阅页面内容。
<meta http-equiv ="Pragma" content ="no-cache">
6. 用来防止别人在框架里调用你的页面。
<meta http-equiv ="Window-target" content ="_top">
7. 自动跳转。
<meta http-equiv ="Refresh" content ="5;url=http://www.yahoo.com">
5 指时间停留 5 秒
8. 网页搜索机器人向导。用来告诉搜索机器人哪些页面需要索引,哪些页面不需要索引。
<meta name="robots" CONTENT="none">
CONTENT 的参数有 all,none,index,noindex,follow,nofollow。默认是 all。
9. 收藏夹图标 <link rel = "Shortcut Icon" href="favicon.ico">
10. 所有的 javascript 的调用尽量采取外部调用.
<script language="JavaScript" src="script/xxxxx.js"></script>
2.4.2 body 区代码规范
1. 附<body>标签:
<body>标签不属于 head 区,这里强调一下,为了保证浏览器的兼容性,必须设置页面背景<body bgcolor="#FFFFFF">(此处为示意,样式需写在 CSS 文件中)
2. 在浏览器里,当我们点击空链接时,它会自动将当前页面重置到首端,从而影响用户正
“javascript:void(null)”代替原来的“#”标记。
<a href="javascript:void(null)">链接</a>
2.5 JavaScript 书写规范
[使用场景:书写 js 脚本时,根据此规则对变类、函数、变量命名,并遵循书写规则,避免出现常犯错误,提高脚本效率。]
1. 书写过程中, 每行代码结束必须有分号; 原则上所有功能均根据 XXX 项目需求原生开发,以避免网上 down 下来的代码造成的代码污染(沉冗代码 || 与现有代码冲突 || ...);
2. 库引入: 原则上仅引入 jQuery 库,以及组件组开发的相关库。 若需引入第三方库, 须与团队其他人员讨论决定;
3. 变量命名: 驼峰式命名. 原生JavaScript 变量要求是纯英文字母, 首字母须小写, 如iTaoLun; jQuery 私有变量要求首字符为'_', 其他与原生JavaScript规则相同, 如:
_iTaoLun; 另, 要求变量集中声明, 避免全局变量.
4.
: 首字母大写,如 ITaoLun;
5.
: 首字母小写驼峰式命名. 如 iTaoLun();
6.
, 尽可能利用英文单词或其缩写;
7. 尽量避免使用存在兼容性及消耗资源的方法或属性, 比如 eval() & innerText;
8. 后期优化中, JavaScript 非注释类中文字符须转换成 unicode 编码使用, 以避免编码错误时乱码显示;
9. 代码结构明了, 加适量注释. 提高函数重用率;
10. 注重与 html 分离, 减小 reflow, 注重性能.
2.6 图片规范
[使用场景:引用添加图片文件时,根据此规则对图片格式进行选择以及命名存放。]
1. 所有页面元素类图片均放入 images 文件夹, 测试用图片放于 images/demoimg 文件夹;
2. 图片格式仅限于 gif || png || jpg;
3. 命名全部用小写英文字母 || 数字 || _ 的组合,其中不得包含汉字 || 空格 || 特殊字符;尽量用易懂的词汇, 便于团队其他成员理解; 另, 命名分头尾两部分, 用下划线隔开, 比如
ad_left01.gif || btn_submit.gif;
4. 在保证视觉效果的情况下选择最小的图片格式与图片质量, 以减少加载时间;
5. 尽量避免使用半透明的 png 图片(若使用, 请参考 css 规范相关说明);
6. 运用css sprite技术集中小的背景图或图标, 减小页面http请求, 但注意, 请务必在对应的 sprite psd 源图中划参考线, 并保存至 images 目录下.
2.7 注释规范
[使用场景:书写代码时,根据此规则对代码进行适当注释说明,方便自己或他人阅读和理
]
2.7.1 html 注释
<!--这儿是注释-->, '--'只能在注释的始末位置,不可置入注释文字区域。
2.7.2 css 注释
1. 行间注释:直接写于属性值后面。
.search{ border:1pxsolid#fff;/*定义搜索输入框边框*/
background:url(../images/icon.gif)no-report#333;/*定义搜索框的背景*/ }
2. 整段注释:分别在开始及结束地方加入注释。
/*=====搜索条=====*/
.search{ border:1pxsolid#fff;
background:url(../images/icon.gif)no-repeat#333;}
/*=====搜索条结束=====*/
2.7.3 JavaScript 注释
JavaScript 注释, 单行注释使用'//这儿是单行注释' ,多行注释使用 /* 这儿有多行注释 */;