前端自省-1

  1. link是HTML标签,@import是css提供的
  2. link标签映入样式时,页面加载时候同时加载,是同步堵塞加载,@import在页面加载完成是加载
  3. link除了可以引用样式还可以引入图片等资源,@import只能引入样式文件
  4. link是XHTML标签没有兼容问题,@import是在CSS2.1提出的,不兼容ie5以下
  5. link可以通过js操作DOM动态引入,@import不可以

为什么不建议使用@import

  1. @import兼容性问题
  2. @import 加载顺序问题,css解析延迟,会加长页面留白期
  3. @import混合js文件时,在IE中引发资源文件的下载顺序被打乱,即使排列在@import后边的js文件也会优先下载
  4. link混合@import会破坏文件的并行下载,而仅仅使用link时,可确保样式在浏览器里被并行下载,并且按顺序下载

圣杯布局和双飞翼布局

作用

两边定宽,中间自适应的三栏布局

区别

  • 圣杯布局为了中间的div内容不被遮挡,将中间div设置了padding-left,padding-right,左右两边使用相对布局position:relative,并配合rightleft属性
  • 双飞翼布局直接在中间div中新建子div放置内容,通过margin-left,margin-right为左右栏留出位置

圣杯布局

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>圣杯布局</title>
</head>
<style>
*{
margin: 0;
padding: 0;
}
#header {
background-color: greenyellow;
text-align: center;
line-height: 50px;
height: 50px;
}
#container {
/* 保持中间栏的位置 */
padding-left: 200px;
padding-right: 150px;
}
#center {
width: 100%;
height: 100px;
float: left;
background-color: turquoise;
}
#left {
float: left;
width: 200px;
height: 100px;
background-color: red;
/* 通过负边距将left恢复到一行 */
margin-left: -100%;
/* 通过定位将left恢复原位置 */
position: relative;
left: -200px;
}
#right {
float: left;
width: 150px;
height: 100px;
background-color: orange;
margin-left: -150px;
position: relative;
right: -150px;
}
#footer {
clear: both;
height: 50px;
background-color: #666666;
text-align: center;
line-height: 50px;
}
</style>
<body>
<div id="header">header</div>
<div id="container">
<div id="center" class="column">center</div>
<div id="left" class="column">left</div>
<div id="right" class="column">right</div>
</div>
<div id="footer">footer</div>
</body>
</html>

双飞翼布局

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>双飞翼</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}

#header {
height: 50px;
background: #666;
text-align: center;
}

#footer {
clear: both;
height: 50px;
background-color: #666;
}
#center {
float: left;
width: 100%;
height: 100px;
background-color: blue;
}
#inside {
margin-left: 200px;
margin-right: 150px;
height: 100px;
}
#left {
float: left;
width: 200px;
height: 100px;
background: red;
margin-left: -100%;
}
#right {
float: left;
width: 150px;
height: 100px;
background: orange;
margin-left: -150px;
}
</style>

<body>
<div id="header">header</div>
<div id="center" class="column">
<div id="inside">center</div>
</div>
<div id="left" class="column">left</div>
<div id="right" class="column">right</div>
<div id="footer">footer</div>
</body>

</html>

flex布局

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
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>三栏</title>
</head>
<style>
.container {
display: flex;
width: 100%;
height: 100px;
}
.left,
.right {
width: 100px;
background: orange;
}
.middle {
flex: 1;
background: #666;
}
</style>
<body>
<div class="container">
<section class="left">left</section>
<section class="middle">middle</section>
<section class="right">right</section>
</div>
</body>
</html>

数组长度为5且元素的随机数在2-32间不重复的值

a) 生成一个长度为5的空数组arr。
b) 生成一个(2-32)之间的随机整数rand。
c) 把随机数rand插入到数组arr内,如果数组arr内已存在与rand相同的数字,则重新生成随机数rand并插入到arr内[需要使用递归实现,不能使用for/while等循环]
d) 最终输出一个长度为5,且内容不重复的数组arr。

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
// 长度为5的空数组
let arr = new Array(5);
// 随机数
let randNumber = randomNumber(2,32);
let index = 0;
// 递归修改数组
randomArr(arr,randNumber);
console.log(arr)


/**
* 获取范围内随机数
* @param {Number} min
* @param {Number} max
*/
function randomNumber(min,max) {
return Math.floor(Math.random()*(max-min+1)+min);
}

function randomArr(arr,num) {
if(arr.indexOf(num) < 0) {
arr[index] = num;
index++;
}

if(index >= arr.length) {
return arr;
}else {
num = randomNumber(2,32);
randomArr(arr,num);
}
}