你可能想避免使用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