为什么我的左侧栏出来了后,底部会不见了?
<!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>1
收起
正在回答
2回答
是因为body中的内容都是定位的元素,没有内容撑起来,但是给body添加了transform之后,里面设置定位的元素都会相对于body来定位,相当于是position:relative;,但是没有relative的本质。body没有高度,而footer设置了bottom为0,那么底部就会在顶部显示,如下:

所以需要给html,body设置高度为100%。自己可以举个例子测试一下。
组件化思想开发电商网页 18版
- 参与学习 人
- 提交作业 467 份
- 解答问题 4826 个
本路径带你通过系统学习HTML5、JavaScript、jQuery的进阶知识,不仅如此,还会学习如何利用组件化的思想来开发网页,知识点+案例,使得所学可以更好的得到实践。
了解课程

恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星