所以,基本上我想要做的就是使用JavaScript來獲取「身體」的高度和「div class = main-container」的高度,那麼如果內容在主容器足夠大,它使得「主容器」的高度大於我想要的位置:固定的「主體」高度; 「.footer-section」中的屬性改爲position:relative;因此它不會與內容重疊,而是會在頁面末尾「消失」,並且只有在向下滾動時纔可見。 我不知道如果我在JavaScript或CSS或兩者都做錯了什麼?控制與javascript重疊內容的css頁腳
我扔在一起,這裏的jsfiddle:https://jsfiddle.net/udsv4t4y/1/
從這裏開始是JavaScript:
function resizeFunction() {
var x = document.getElementsByTagName("body").offsetHeight;
var y = document.getElementsByClassName("main-container").offsetHeight;
var z = document.getElementsByClassName("footer-section");
if (x < y) {
z.className += "responsive";
} else {
z.className = "footer-section";
}
}
這裏是我一起工作的HTML:
<body onresize="resizeFunction" onload="resizeFunction">
<div class="main-container">
<div class="row"></div>
<div class="col-12">
Lorem ipsum dolor sit amet, (cut out due to length)
</div>
<div class="row"></div>
<div class="col-12">
<div class="footer-section"></div>
</div>
</div>
</body>
和CSS:
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: auto;
font-family: "Tahoma", sans-serif;
font-size: 16px;
color: #454545;
background-color: #fff;
}
.main-container {
min-height: 100%;
width: 100%;
margin: 0;
}
.footer-section {
position: fixed;
bottom: 0;
height: 60px;
width: 100%;
background: #428cd9;
}
.footer-section.responsive {
position: relative;
bottom: 0;
height: 60px;
width: 100%;
background: #428cd9;
}
.row::after {
content: "";
clear: both;
display: block;
}
[class*="col-"] {
float: left;
}
.col-12 {width: 100%;}
啊!完美的作品! :) 太感謝了。 – Daniel
如果頁腳的高度不固定,並且下降小於60像素,這將表現出有點奇怪。 – Deep