如何用 JavaScript DOM 將背景影像設定為固定影像?
要透過 JavaScript 固定背景影像,請使用backgroundAttachment屬性。該屬性允許您設定不會滾動的影像。
示例
您可以嘗試執行以下程式碼來了解如何使用 JavaScript 中的backgroundAttachment屬性 -
<!DOCTYPE html>
<html>
<body>
<button onclick="display()">Click to Set background</button>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<script>
function display() {
document.body.style.background = "url('https://tutorialspoint.tw/html5/images/html5-mini-logo.jpg') no-repeat right top";
document.body.style.backgroundAttachment = "fixed";
}
</script>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP