github:https://github.com/bradtraversy/50projects50days

一、代码

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Expanding Cards</title>
</head>
<body>
<div class="container">
<div class="panel active" style="background-image: url('https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')">
<h3>Explore The World</h3>
</div>
<div class="panel" style="background-image: url('https://images.unsplash.com/photo-1572276596237-5db2c3e16c5d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')">
<h3>Wild Forest</h3>
</div>
<div class="panel" style="background-image: url('https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1353&q=80')">
<h3>Sunny Beach</h3>
</div>
<div class="panel" style="background-image: url('https://images.unsplash.com/photo-1551009175-8a68da93d5f9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80')">
<h3>City on Winter</h3>
</div>
<div class="panel" style="background-image: url('https://images.unsplash.com/photo-1549880338-65ddcdfd017b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')">
<h3>Mountains - Clouds</h3>
</div>

</div>

<script src="script.js"></script>
</body>
</html>

CSS

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
*{
padding: 0px;
margin: 5px;
box-sizing: border-box;
}

body{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

.container{
display: flex;
width: 80vw;
}

.panel{
flex: 0.5;
background-repeat: no-repeat;
background-position: center;
background-size: cover;
height: 80vh;
position: relative;
border-radius: 30px;
cursor: pointer;
}
.panel h3{
position: absolute;
color: aliceblue;
bottom: 20px;
left: 10px;
opacity: 0;
}

.panel.active{
flex: 8;
transition: all 1s;
}
.panel.active h3{
opacity: 1;
transition: opacity 0.5s;
}

Javascript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const panels=document.querySelectorAll('.panel')

panels.forEach(item=>{
item.addEventListener('click',()=>{
removeActiveClasses();
item.classList.add('active');
})
})

function removeActiveClasses(){
panels.forEach(item=>{
item.classList.remove('active');
})
}

二、知识点心得

(1)box-sizing属性

box-sizing属性决定了盒模型大小的计算方式,默认值为content-box,任何边框和内边距的宽度都会被增加到最后绘制出来的元素宽度中,如果修改为border-box,那么盒子的大小计算将只包含width的值

(2)vh、vw单位

它们表示相对视口高度,视口就是看到的窗口大小

(3)flex布局

Flex 是 Flexible Box 的缩写,意为”弹性布局”,用来为盒状模型提供最大的灵活性。容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end。默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size。

下面这段代码可以让内容居中显示

1
2
3
4
5
6
body{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

(4)子绝父相

将子元素设置为绝对定位就需要将他的父元素设置为相对定位,因为绝对定位是相对于上一个相对定位的,body为最初的相对定位

(5)cursor属性

cursor属性决定了鼠标指针悬停在元素上时显示相应样式

样式大全:https://developer.mozilla.org/zh-CN/docs/Web/CSS/cursor

(6)transition属性

transition属性可以让元素样式切换平滑

(7)JavaScript的思路

先将每个元素的active属性都删除,如果有可以顺利删除,如果没有也没什么问题,然后再将所点击的元素添加active,Element.classList可以获取标签中的class属性