overflow-anchor
overflow-anchor CSS 屬性提供了一種選擇退出瀏覽器滾動錨定行為的方法,該行為會調整滾動位置以最大程度地減少內容偏移。
在任何支援滾動錨定的瀏覽器中,滾動錨定行為預設啟用。因此,通常只有在文件或文件的某個部分出現滾動錨定問題並需要關閉此行為時,才需要更改此屬性的值。
試一試
overflow-anchor: auto;
overflow-anchor: none;
<section class="default-example" id="default-example">
<div class="whole-content-wrapper">
<button id="playback" type="button">Start lottery</button>
<p>Magic numbers for today are:</p>
<div id="example-element"></div>
</div>
</section>
.whole-content-wrapper {
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
}
#example-element {
height: 100%;
border: 2px dashed dodgerblue;
padding: 0.75em;
text-align: left;
overflow: scroll;
}
#playback {
font-size: 1em;
width: 10em;
height: 4em;
font-weight: bold;
margin: 1em auto;
background-color: aliceblue;
border: solid 2px dodgerblue;
border-radius: 5px;
}
#playback:hover {
border-color: lightseagreen;
}
#playback:active {
filter: brightness(0.9);
}
const example = document.getElementById("example-element");
const button = document.getElementById("playback");
let intervalId;
function setInitialState() {
example.innerHTML = "";
Array.from({ length: 10 }, (_, i) => i).forEach(addContent);
example.scrollTop = example.scrollHeight;
}
function addContent() {
console.log("adding content");
const magicNumber = Math.floor(Math.random() * 10000);
example.insertAdjacentHTML(
"afterbegin",
`<div class="new-content-container">New Magic Number: ${magicNumber}</div>`,
);
}
button.addEventListener("click", () => {
if (example.classList.contains("running")) {
example.classList.remove("running");
button.textContent = "Start lottery";
clearInterval(intervalId);
} else {
example.classList.add("running");
button.textContent = "Stop lottery";
setInitialState();
intervalId = setInterval(addContent, 1000);
}
});
語法
css
/* Keyword values */
overflow-anchor: auto;
overflow-anchor: none;
/* Global values */
overflow-anchor: inherit;
overflow-anchor: initial;
overflow-anchor: revert;
overflow-anchor: revert-layer;
overflow-anchor: unset;
值
正式定義
正式語法
overflow-anchor =
auto |
none
示例
防止滾動錨定
要防止文件中的滾動錨定,請使用 overflow-anchor 屬性。
css
* {
overflow-anchor: none;
}
規範
| 規範 |
|---|
| CSS 滾動錨定模組級別 1 # 排除 API |
瀏覽器相容性
載入中…