在我的设计中,我希望在重新调整浏览器(firefox)的大小时保持按钮在视线范围内。按钮位于相对 DIV 中,当高度太小时,它将 DIV 设置为绝对值。但是在重新调整窗口大小时,DIV 会闪烁并随机消失/出现。
这是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<style>
body {
/*overflow: hidden;*/
border: 2px dotted lightblue;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: 0;
padding: 0;
}
.maxheight {
max-height: 100%;
/*overflow: hidden;*/
}
.scrollable {
overflow: auto;
height: 100%;
background-color: yellow;
border: 1px solid black;
position: relative;
max-height: 100%;
bottom: 0px;
}
.buttons {
background-color: red;
border: 1px solid black;
position: relative;
bottom: 0px;
}
</style>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
$(window).resize(function () {
var pageHeight = $("#page").height();
var buttons = $(".buttons");
var elementHeight = $(buttons).height()
var elementTop = $(buttons).position().top;
var total = pageHeight - (elementHeight + elementTop);
if (pageHeight - (elementHeight + elementTop) < 0) {
$(buttons).css({
'position': 'absolute',
'width': $(".scrollable").width()
});
} else {
$(buttons).css({
'position': 'relative',
'width': $(".scrollable").width()
});
}
});
</script>
</head>
<body id="page">
<div class="maxheight">
<h2>title</h2>
<div class="scrollable">
This is content.<br />
This is content.<br />
This is content.<br />
This is content.<br />
This is content.<br />
This is content.<br />
This is content.<br />
This is content.<br />
This is content.<br />
This is content.<br />
This is content.<br />
This is content.<br />
This is content.
</div>
<div class="buttons">
<button type="button" value="test">test</button>
<button type="button" value="test">test</button>
</div>
</div>
</body>
</html>
小提琴:http: //jsfiddle.net/GxDES/