如果我明白你想達到什麼目的,有一個解決方法可以實現類似的結果。
首先,您實際上無法讓您的TopBar表現得像位置固定的流動集團元素。所以,讓我們做靜態。
「固定」的行爲將被定身性
body {
/* NOTICE the vertical flex box, makes the height adjust
automaticaly (no need to toggle)*/
display: flex;
flex-direction: column;
max-height: 100vh; /* Restrain the body to the window height */
overflow-y: hidden; /* Prevent scrollbar on body */
}
.TopBar {
display: block; /* Blocks have 100% widht by default, don't need to
specify */
padding: 10px;
position: static; /* No need to specify, it's the default value */
/* Helpers to make the TopBar easier to track */
background-color: #000000;
opacity: 0.5;
/* This is not part of the solution, it's only to make the height inversely proportional to window width
e.g. make it grow while the other decrease
*/
height: calc(200px - 10vw);
}
/*
Just under the TopBar, lets place a container element that will act
as scrolling window
*/
.container {
height: 100vh; /* max-height is superfluous because of the overflow. */
/* This will simply make the scrolling on the container instead of the body */
overflow-y: scroll;
}
.TopBar {
/* 1st Element */
}
.TopBar > div {
color: white;
}
/* simply to display some text */
p {
width: 50%;
margin: 1em auto;
}
放置容器裏面瞧你的轉盤來提供!無需位置和Z指數擺弄。 TopBar和容器正在流動,前者將「推」後者。
這就是說,一些媒體查詢調整不會傷害。根據你的圖片,你的TopBar中的元素是內聯的(內聯或內聯塊)。你應該考慮讓他們「封鎖」。柔性盒也值得考慮。
希望這有助於
是的,你說得對,position:fixed和其他屬性會在頁面獲得更多元素時引發很多問題。我還沒有真正學會如何使用@media,但讓我看看。其實我的意圖很直接,我在索引頁面有多個元素(img,div),當我向下滾動時,導航欄應始終保持在頂部,以便用戶隨時隨地訪問它在頁面上。除媒體之外,真的沒有其他選擇嗎?謝謝。 – BSS
可能還有其他選擇,但@media幾乎肯定是最好的選擇 - 很簡單,因爲在不同的瀏覽器大小下,通常會讓一些佈局看起來不同。媒體查詢確實很容易掌握,他們給了你很大的靈活性,可以根據不同的瀏覽器尺寸定製你的設計,所以他們非常值得學習。 – Spudley
嗨Spudley,你介意幫助查看附件谷歌驅動器中的源文件。我無法找出#topnav可以像位置一樣行爲的原因:粘滯或固定,當css有position:relative .... [單擊此處訪問HTML文件](https://drive.google .com/drive/folders/0BzbdjY-H_HzZTC1Rci1nY0F4VFU?usp = sharing) – BSS