为什么我的左侧栏出来了后,底部会不见了?

为什么我的左侧栏出来了后,底部会不见了?

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>slidebar</title>
<meta name='viewport' content='width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no'>
<style>
*{
margin:0;
padding:0;
}
body{
transition: all 200ms ease-in-out;
}
body.active{
transform: translate(150px,0);
transition: all 200ms ease-in-out;
}
.header{
width: 100%;
height: 45px;
position: fixed;
top: 0;
left: 0;
border-bottom: 1px solid red;
line-height: 45px;
}
header div{
width: 50px;
text-align: center;
line-height: 45px;
/*background: red;*/
cursor: pointer;
margin-left: 30px;
}
.header div span{
width: 25px;
height: 2px;
background:#999;
display: inline-block;
box-shadow: 0 7px 0 #999 ,0 -7px 0 #999;
cursor: pointer;
}
 
.slidebar{
width: 150px;
height: 100vh;
position: absolute;
background: #999;
top: 0;
left: 0;
transition: all 200ms ease-in-out;
transform: translate(-150px,0);
z-index: 100;
}
.footer{
width: 100%;
height: 45px;
position: fixed;
left: 0;
bottom: 0;
border-top: 1px solid red;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
</head>
<body>
<header class="header">
<div id='slide-btn'><span></span></div>
</header>
<section class="slidebar"></section>
<footer class="footer"></footer>
<script>
document.addEventListener('DOMContentLoaded',function(){
(function(){
var slideBtn = document.querySelector('#slide-btn');
var body_ = document.querySelector('body');
slideBtn.onclick = function(){
console.log(1);
body_.classList.toggle("active");
}
 
})();
})
</script>
</body>
</html>


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

是因为body中的内容都是定位的元素,没有内容撑起来,但是给body添加了transform之后,里面设置定位的元素都会相对于body来定位,相当于是position:relative;,但是没有relative的本质。body没有高度,而footer设置了bottom为0,那么底部就会在顶部显示,如下:

http://img1.sycdn.imooc.com//climg/5bcd93ea0001103703310052.jpg

所以需要给html,body设置高度为100%。自己可以举个例子测试一下。

好帮手慕星星 2018-10-22 14:53:11

给html和body设置一个高度即可:

http://img1.sycdn.imooc.com//climg/5bcd73710001b44307120263.jpg

视频中能够直接设置成功,是因为引入一个reset.css,里面有提前定义好的样式,可以下载源码查看。

自己可以测试一下代码,祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
组件化思想开发电商网页 18版
  • 参与学习           人
  • 提交作业       467    份
  • 解答问题       4826    个

本路径带你通过系统学习HTML5、JavaScript、jQuery的进阶知识,不仅如此,还会学习如何利用组件化的思想来开发网页,知识点+案例,使得所学可以更好的得到实践。

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码