你可能想避免使用position: fixed
因为它是已知造成在移动设备上的性能问题,尤其是当有任何形式的参与转换或翻译。
我在这种情况下的常用方法是使用绝对或有时相对定位的元素,然后动态调整需要适合calc()的周围元素的尺寸。这适用于所有设备,并且不会导致性能损失。 calc()
也是extremely well supported。
HTML
<div class="wrapper">
<div class="content">
<h1>Heading!</h1>
<p>...</p>
<h1>Another heading!</h1>
<p>...</p>
<h1>Yey! Another heading!</h1>
<p>...</p>
</div>
<div class="banner">
<img src="https://placehold.it/600x120" alt="ads" />
</div>
</div>
CSS
body {
margin: 0;
}
.wrapper {
background: #981e32;
width: 600px;
height: calc(100vh - 120px); /* viewport height - fixed banner height */
overflow-y: auto; /* makes sure there are scrollbars when needed */
}
.banner {
position: absolute;
bottom: 0;
max-height: 120px;
}
.content {
padding: 1em;
color: white;
}
.banner img {
display: block; /* this prevents inline elements from messing up height calculations */
}
只是裸记住,我做了示范着想一些假设。您可以自由调整代码以适应您的需求。
DEMO
可能,这可以帮助你。 http://stackoverflow.com/questions/19760770/contain-an-image-whose-size-is-dynamic-inside-a-div – Piyush