如何在 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>

更新於:2020 年 6 月 23 日

510 人觀看

開啟你的 職業

完成課程,獲得認證

開始
廣告
© . All rights reserved.