CSS实战--实现侧边栏滑进滑出

效果

这里写图片描述

用的csdn做的例子, 本篇博客主要讲的是侧边栏滑进滑出, 背景是图片……, 不要在意.

代码

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
//index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>home</title>
<script>
var documentElement = document.documentElement; //获取html元素
documentElement.style.fontSize = documentElement.getBoundingClientRect().width / 16 + 'px'; //设置为视窗宽度的1/16
</script>
<link rel="stylesheet" type="text/css" href="./index.css">
</head>
<body>
<!-- 网页主体容器 -->
<div class="body">
<!-- 左上角头像 -->
<a href="javascript:void(0)" class="avatar-small"></a>
<!-- 方便演示内容图片代替主页 -->
<img class="main" src="./img/1.PNG">
</div>
<!-- 网页遮罩 -->
<div class="mask"></div>
<!-- 侧边栏 -->
<div class="nav">
<ul>
<li>
<a>
<img src="./img/avatar.jpg" class="avatar-big" alt="">
</a>
</li>
<li>
<a>我的课程</a>
</li>
<li>
<a>我的订单</a>
</li>
<li>
<a>我的收藏</a>
</li>
<li>
<a>个人中心</a>
</li>
</ul>
</div>
<script type="text/javascript" src="./index.js"></script>
</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
//index.css
body {
margin: 0;
}
ul, li {
list-style: none;
padding: 0;
margin: 0;
}
.avatar-small {
position: absolute;
z-index: 2;
left: 0;
top: 0;
width: 2rem;
height: 2rem;
}
.main{
width: 100vw;
height: 100vh;
}
.mask {
display: none;
position: fixed;
z-index: 10;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, .1);
}
.nav {
position: absolute;
z-index: 11;
left: -7rem;
top: 0;
width: 7rem;
height: 100%;
background: #555;
}
.nav {
transition: left linear .1s;
}
.nav a {
display: block;
padding: 1em 0;
border-bottom: 1px solid #888;
font-size: 16px;
color: #eee;
text-align: center;
}
.nav .avatar-big {
width: 3rem;
border-radius: 50%;
box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//index.js
let btn = document.getElementsByClassName('avatar-small')[0],
mask = document.getElementsByClassName('mask')[0],
nav = document.getElementsByClassName('nav')[0];
btn.addEventListener('click', function () {
mask.style.display = 'block';
nav.style.left = 0;
}, false);
mask.addEventListener('click', function () {
mask.style.display = 'none';
nav.style.left = '-7rem';
}, false)
越来越多的平台(微信公众平台,新浪微博,简书,百度打赏等)支持打赏功能,付费阅读时代越来越近,特此增加了打赏功能,支持微信打赏和支付宝打赏。坚持原创技术分享,您的支持将鼓励我继续创作!