使用position: fixed
。
function toggleDiv() {
var div = document.getElementById('togglable');
if (div.style.display !== 'none') {
div.style.display = 'none';
}
else {
div.style.display = 'block';
}
};
#container-parent {
position: absolute;
background-color: #eee;
height: 95%;
width: 90%;
overflow: scroll;
}
#container {
height: 500px;
}
#togglable {
position: fixed;
top: 10%;
height: 80%;
left: 25%;
width: 50%;
background-color: red;
}
#spacer {
height: 90%;
}
<div id="container-parent">
<div id="container">
<input type=button onclick="toggleDiv()">
<div id="togglable"></div>
<div id="spacer">
<!-- I don't know how you have got your buttons attached but the point is that `position: fixed` positions the togglable div relative to the viewport -->
</div>
<input type=button onclick="toggleDiv()">
</div>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句