1 | ## 项目命名
|
2 | `my_project_name`
|
3 | ## 项目目录结构
|
4 | ```js
|
5 | my_project_name
|
6 | |-- config //存放配置以及保密文件
|
7 | |-- dist //存放打包后的文件,不打包就不要
|
8 | |-- client //存放前端文件
|
9 | | |-- index.html
|
10 | | |-- page_one.html
|
11 | | |-- page_tow.html
|
12 | | |-- common //存放公共文件
|
13 | | | |-- commonStyle.less
|
14 | | | |-- iconfont
|
15 | | | | |-- ...
|
16 | | | | |-- ...
|
17 | | | | |-- ...
|
18 | | |-- js
|
19 | | | |-- index.js
|
20 | | | |-- page_one.js
|
21 | | | |-- page_tow.js
|
22 | | |-- less
|
23 | | | |-- global.less // 入口文件?将全部css打包在一个文件?
|
24 | | | |-- index.less
|
25 | | | |-- page_one.less
|
26 | | | |-- page_tow.less
|
27 | | |-- utils // 自己写的插件或者jq之类
|
28 | | | |-- jquery.js
|
29 | | | |-- opera_dom.js
|
30 | |-- server //存放nodejs文件
|
31 | ```
|
32 | ## html文件命名
|
33 | `account_model.html`
|
34 | ## css文件命名
|
35 | `account_model.css`
|
36 | ## js文件命名
|
37 | `account_model.js`
|
38 |
|
39 | ## css
|
40 | - #### class,id...
|
41 | ```css
|
42 | /* class */
|
43 | .element-content {
|
44 | ...
|
45 | }
|
46 |
|
47 | /* id */
|
48 | #myDialog {
|
49 | ...
|
50 | }
|
51 |
|
52 | /* 变量 */
|
53 | $colorBlack: #000;
|
54 |
|
55 | /* 函数 */
|
56 | @function pxToRem($px) {
|
57 | ...
|
58 | }
|
59 |
|
60 | /* 混合 */
|
61 | @mixin centerBlock {
|
62 | ...
|
63 | }
|
64 |
|
65 | /* placeholder */
|
66 | %myDialog {
|
67 | ...
|
68 | }
|
69 | ```
|
70 | - #### 颜色
|
71 | ```css
|
72 | /* not good */
|
73 | .element {
|
74 | color: #ABCDEF;
|
75 | background-color: #001122;
|
76 | }
|
77 |
|
78 | /* good */
|
79 | .element {
|
80 | color: #abcdef;
|
81 | background-color: #012;
|
82 | }
|
83 | ```
|
84 | - #### 属性简写(除了margin,padding以外,其他不要使用简写)
|
85 | ```css
|
86 | <!-- margin padding -->
|
87 | margin {
|
88 | 10px 20px 10px 3px
|
89 | }
|
90 | padding {
|
91 | 10px 20px 10px 3px
|
92 | }
|
93 |
|
94 | // flex,transition,background
|
95 |
|
96 | // bad 缺乏可读性
|
97 | flex-style {
|
98 | flex: 1 1 300px;
|
99 | }
|
100 | // good
|
101 | flex-style {
|
102 | flex-grow: 1;
|
103 | flex-shrink: 1;
|
104 | flex-basic: 300px;
|
105 | }
|
106 |
|
107 | /* not good */
|
108 | .element {
|
109 | transition: opacity 1s linear 2s;
|
110 | }
|
111 |
|
112 | /* good */
|
113 | .element {
|
114 | transition-delay: 2s;
|
115 | transition-timing-function: linear;
|
116 | transition-duration: 1s;
|
117 | transition-property: opacity;
|
118 | }
|
119 | ```
|
120 | - #### 媒介查询
|
121 | 尽量将媒体查询的规则靠近与他们相关的规则,不要将他们一起放到一个独立的样式文件中,或者丢在文档的最底部,这样做只会让大家以后更容易忘记他们。
|
122 |
|
123 | - #### 杂项
|
124 | - 不允许有空的规则;
|
125 | ```css
|
126 | style {
|
127 |
|
128 | }
|
129 | ```
|
130 |
|
131 | - 去掉数字中不必要的小数点和末尾的0;
|
132 | ```css
|
133 | style {
|
134 | width: 1.0rem; // bad
|
135 | width: 1rem; // good
|
136 | }
|
137 | ```
|
138 |
|
139 | - 属性值'0'后面不要加单位;
|
140 | ```css
|
141 | style {
|
142 | padding: 0px; // bad
|
143 | padding: 0; // good
|
144 | }
|
145 | ```
|
146 |
|
147 | - 同个属性不同前缀的写法需要在垂直方向保持对齐,具体参照右边的写法;(交给自动化工具去做这个)
|
148 | ```css
|
149 | // bad
|
150 | ._list_bxrg2_14 {
|
151 | -webkit-box-direction: normal;
|
152 | -ms-flex-direction: column;
|
153 | flex-direction: column;
|
154 |
|
155 | // good
|
156 | ._list_bxrg2_14 {
|
157 | -webkit-box-direction: normal;
|
158 | -ms-flex-direction: column;
|
159 | flex-direction: column;
|
160 | ```
|
161 | - 无前缀的标准属性应该写在有前缀的属性后面;(参考同上)
|
162 |
|
163 | - 不要在一个文件里出现两个相同的规则;
|
164 | ```css
|
165 | // bad
|
166 | style {
|
167 | color: red;
|
168 | color: red;
|
169 | }
|
170 | // good
|
171 | style {
|
172 | color: red;
|
173 | }
|
174 | ```
|
175 | - 用 border: 0; 代替 border: none;;
|
176 | ```css
|
177 | // bad
|
178 | style {
|
179 | border: 0;
|
180 | }
|
181 | // good
|
182 | style {
|
183 | border: none;
|
184 | }
|
185 | ```
|
186 |
|
187 | - 选择器不要超过4层(在less中如果超过4层应该考虑用嵌套的方式来写);
|
188 | ```css
|
189 | // bad
|
190 | body div span em i {
|
191 | color: red;
|
192 | }
|
193 | // good
|
194 | body {
|
195 | div {
|
196 | span {
|
197 | em {
|
198 | i
|
199 | }
|
200 | }
|
201 | }
|
202 | }
|
203 | ```
|
204 |
|
205 | - 尽量少用'*'选择器。
|
206 | ```css
|
207 | // bad
|
208 | * {
|
209 | margin: 0;
|
210 | padding: 0;
|
211 | }
|
212 | // good
|
213 | body, h1, h2, h3, h4, h5, h6, hr, p,blockquote, dl, dt, dd, ul, ol, li, pre,form, fieldset, legend, button, input, textarea, th, td {
|
214 | margin: 0;
|
215 | padding: 0;
|
216 | }
|
217 | ```
|
218 | ## javaScript
|
219 | - #### 变量(使用驼峰命名 nameString)
|
220 | ```js
|
221 | var myVariable = 'Bob';
|
222 | myVariable = 'Steve';
|
223 | ```
|
224 | - #### 函数(使用驼峰命名 nameString)
|
225 | ```js
|
226 | function addSquares(a,b) {
|
227 | function square(x) {
|
228 | return x * x;
|
229 | }
|
230 | return square(a) + square(b);
|
231 | };
|
232 | // es6
|
233 | const addSquares = (a,b) => {
|
234 | const square = x => x*x;
|
235 | return square(a) + square(b);
|
236 | };
|
237 | ```
|
238 | - #### 常量(大写来区分,不可改变)[参考](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const)
|
239 | ```js
|
240 | const MY_FAV = 7;
|
241 | ```
|
242 |
|
243 | - #### class类(首字母大写)[参考](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes)
|
244 | ```js
|
245 | // es6
|
246 | class RectangleRange {
|
247 | constructor(height, width) {
|
248 | this.height = height;
|
249 | this.width = width;
|
250 | }
|
251 | }
|
252 | function RectangleRange(){}
|
253 | RectangleRange.prototype.fangfa = {
|
254 | ????
|
255 | ????
|
256 | ????
|
257 | }
|
258 | ```
|
259 |
|
260 | - #### 方法(驼峰命名,参考js自有方法)
|
261 | `Object.prototype.hasOwnProperty()`
|
262 | - #### 文档注释
|
263 | ```js
|
264 | /**
|
265 | * @func
|
266 | * @desc 一个带参数的函数
|
267 | * @param {string} a - 参数a
|
268 | * @param {number} b=1 - 参数b默认值为1
|
269 | * @param {string} c=1 - 参数c有两种支持的取值</br>1—表示x</br>2—表示xx
|
270 | * @param {object} d - 参数d为一个对象
|
271 | * @param {string} d.e - 参数d的e属性
|
272 | * @param {string} d.f - 参数d的f属性
|
273 | * @param {object[]} g - 参数g为一个对象数组
|
274 | * @param {string} g.h - 参数g数组中一项的h属性
|
275 | * @param {string} g.i - 参数g数组中一项的i属性
|
276 | * @param {string} [j] - 参数j是一个可选参数
|
277 | */
|
278 | function foo(a, b, c, d, g, j) {
|
279 | ...
|
280 | }
|
281 | ```
|
282 | #### 参考[mdn](https://developer.mozilla.org/zh-CN/) [腾讯alloyteam团队前端代码规范](https://www.kancloud.cn/digest/code-guide/42604) |
\ | No newline at end of file |